This commit is contained in:
2021-05-17 17:41:09 +08:00
parent 4327db27f8
commit b62896e354
2 changed files with 79 additions and 71 deletions
+77 -71
View File
@@ -11,8 +11,6 @@ use Snowflake\Event;
use Snowflake\Exception\ConfigException;
use Snowflake\Runtime;
use Snowflake\Snowflake;
use Swoole\Coroutine;
use Swoole\Coroutine\System;
use Swoole\Server;
/**
@@ -23,84 +21,92 @@ 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
{
$this->setConfigs($worker_id);
Config::sets(sweep());
$annotation = Snowflake::app()->getAnnotation();
$annotation->setLoader(unserialize(file_get_contents(storage(Runtime::CACHE_NAME))));
if ($worker_id >= $server->setting['worker_num']) {
$annotation->runtime(APP_PATH, [CONTROLLER_PATH, TASK_PATH, LISTENER_PATH]);
$this->onTask($server, $annotation);
} else {
$annotation->runtime(CONTROLLER_PATH);
$annotation->runtime(directory('app'), [CONTROLLER_PATH]);
$this->onWorker($server, $annotation);
}
}
$annotation = Snowflake::app()->getAnnotation();
$annotation->setLoader(unserialize(file_get_contents(storage(Runtime::CACHE_NAME))));
if ($worker_id >= $server->setting['worker_num']) {
$this->onTask($server, $annotation);
} else {
$this->onWorker($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 $worker_id
* @throws Exception
*/
private function setConfigs($worker_id)
{
putenv('state=start');
putenv('worker=' . $worker_id);
$serialize = file_get_contents(Runtime::CONFIG_NAME);
Config::sets(unserialize($serialize));
}
/**
* @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,
TASK_PATH,
]);
name($server->worker_pid, 'Task#' . $server->worker_id);
Snowflake::setTaskId($server->worker_pid);
fire(Event::SERVER_TASK_START);
}
/**
* @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 Exception
*/
public function onWorker(Server $server, Annotation $annotation)
{
name($server->worker_pid, 'Worker#' . $server->worker_id);
/**
* @param Server $server
* @param Annotation $annotation
* @throws ConfigException
* @throws Exception
*/
public function onTask(Server $server, Annotation $annotation)
{
putenv('environmental=' . Snowflake::TASK);
$time = microtime(true);
$annotation->runtime(CONTROLLER_PATH);
$this->debug('use time.' . (microtime(true) - $time));
$annotation->runtime(directory('app'), CONTROLLER_PATH);
$annotation->runtime(APP_PATH, [CONTROLLER_PATH, TASK_PATH, LISTENER_PATH]);
Snowflake::setWorkerId($server->worker_pid);
putenv('environmental=' . Snowflake::WORKER);
name($server->worker_pid, 'Task#' . $server->worker_id);
fire(Event::SERVER_WORKER_START, [getenv('worker')]);
}
Snowflake::setTaskId($server->worker_pid);
fire(Event::SERVER_TASK_START);
}
/**
* @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]);
Snowflake::setWorkerId($server->worker_pid);
putenv('environmental=' . Snowflake::WORKER);
fire(Event::SERVER_WORKER_START, [getenv('worker')]);
}
}
+2
View File
@@ -23,6 +23,7 @@ class Runtime extends Command
const CACHE_NAME = '.runtime.cache';
const CONFIG_NAME = '.config.cache';
/**
@@ -36,6 +37,7 @@ class Runtime extends Command
$runtime = storage(static::CACHE_NAME);
Snowflake::writeFile(storage(static::CONFIG_NAME), serialize(sweep()));
Snowflake::writeFile($runtime, serialize($annotation->getLoader()));
}