This commit is contained in:
as2252258@163.com
2021-04-23 03:12:00 +08:00
parent 8b57f44f7e
commit 325ab04f20
26 changed files with 775 additions and 1493 deletions
+64 -69
View File
@@ -20,88 +20,83 @@ class OnWorkerStart extends Callback
{
/**
* @param Server $server
* @param int $worker_id
*
* @return mixed
* @throws Exception
*/
public function onHandler(Server $server, int $worker_id): void
{
putenv('state=start');
putenv('worker=' . $worker_id);
/**
* @param Server $server
* @param int $worker_id
*
* @return mixed
* @throws Exception
*/
public function onHandler(Server $server, int $worker_id): void
{
putenv('state=start');
putenv('worker=' . $worker_id);
$content = System::readFile(storage('runtime.php'));
$content = System::readFile(storage('runtime.php'));
$annotation = Snowflake::app()->getAnnotation();
$annotation->setLoader(unserialize($content));
if ($worker_id < $server->setting['worker_num']) {
$this->onWorker($server, $annotation);
} else {
$this->onTask($server, $annotation);
}
}
$annotation = Snowflake::app()->getAnnotation();
$annotation->setLoader(unserialize($content));
if ($worker_id < $server->setting['worker_num']) {
$this->onWorker($server, $annotation);
} else {
$this->onTask($server, $annotation);
}
}
/**
* @param Server $server
* @param int $worker_id
* @return bool
*/
private function isWorker(Server $server, int $worker_id): bool
{
return $worker_id < $server->setting['worker_num'];
}
/**
* @param Server $server
* @param int $worker_id
* @return bool
*/
private function isWorker(Server $server, int $worker_id): bool
{
return $worker_id < $server->setting['worker_num'];
}
/**
* @param Server $server
* @param Annotation $annotation
* @throws ConfigException
* @throws Exception
*/
public function onTask(Server $server, Annotation $annotation)
{
putenv('environmental=' . Snowflake::TASK);
/**
* @param Server $server
* @param Annotation $annotation
* @throws ConfigException
* @throws Exception
*/
public function onTask(Server $server, Annotation $annotation)
{
putenv('environmental=' . Snowflake::TASK);
$annotation->runtime(directory('app'), [
CONTROLLER_PATH,
LISTENER_PATH,
SOCKET_PATH,
TASK_PATH,
]);
name($server->worker_pid, 'Task#' . $server->worker_id);
$annotation->runtime(directory('app'), [
CONTROLLER_PATH,
LISTENER_PATH,
SOCKET_PATH,
TASK_PATH,
]);
name($server->worker_pid, 'Task#' . $server->worker_id);
Snowflake::setTaskId($server->worker_pid);
Snowflake::setTaskId($server->worker_pid);
fire(Event::SERVER_TASK_START);
}
fire(Event::SERVER_TASK_START);
}
/**
* @param Server $server
* @param Annotation $annotation
* @throws Exception
*/
public function onWorker(Server $server, Annotation $annotation)
{
try {
name($server->worker_pid, 'Worker#' . $server->worker_id);
/**
* @param Server $server
* @param Annotation $annotation
* @throws Exception
*/
public function onWorker(Server $server, Annotation $annotation)
{
name($server->worker_pid, 'Worker#' . $server->worker_id);
$time = microtime(true);
$annotation->runtime(CONTROLLER_PATH);
$this->debug('use time.' . (microtime(true) - $time));
$annotation->runtime(directory('app'), CONTROLLER_PATH);
$time = microtime(true);
$annotation->runtime(CONTROLLER_PATH);
$this->debug('use time.' . (microtime(true) - $time));
$annotation->runtime(directory('app'), CONTROLLER_PATH);
Snowflake::setWorkerId($server->worker_pid);
putenv('environmental=' . Snowflake::WORKER);
Snowflake::setWorkerId($server->worker_pid);
putenv('environmental=' . Snowflake::WORKER);
fire(Event::SERVER_WORKER_START, [getenv('worker')]);
} catch (\Throwable $exception) {
$this->addError($exception, 'throwable');
write($exception->getMessage(), 'worker');
}
}
fire(Event::SERVER_WORKER_START, [getenv('worker')]);
}
}