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
+20 -14
View File
@@ -11,8 +11,6 @@ use Snowflake\Event;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
use Snowflake\Runtime; use Snowflake\Runtime;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Coroutine;
use Swoole\Coroutine\System;
use Swoole\Server; use Swoole\Server;
/** /**
@@ -32,24 +30,33 @@ class OnWorkerStart extends Callback
*/ */
public function onHandler(Server $server, int $worker_id): void public function onHandler(Server $server, int $worker_id): void
{ {
putenv('state=start'); $this->setConfigs($worker_id);
putenv('worker=' . $worker_id);
Config::sets(sweep());
$annotation = Snowflake::app()->getAnnotation(); $annotation = Snowflake::app()->getAnnotation();
$annotation->setLoader(unserialize(file_get_contents(storage(Runtime::CACHE_NAME)))); $annotation->setLoader(unserialize(file_get_contents(storage(Runtime::CACHE_NAME))));
if ($worker_id >= $server->setting['worker_num']) { if ($worker_id >= $server->setting['worker_num']) {
$annotation->runtime(APP_PATH, [CONTROLLER_PATH, TASK_PATH, LISTENER_PATH]);
$this->onTask($server, $annotation); $this->onTask($server, $annotation);
} else { } else {
$annotation->runtime(CONTROLLER_PATH);
$annotation->runtime(directory('app'), [CONTROLLER_PATH]);
$this->onWorker($server, $annotation); $this->onWorker($server, $annotation);
} }
} }
/**
* @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 Server $server
* @param int $worker_id * @param int $worker_id
@@ -71,10 +78,8 @@ class OnWorkerStart extends Callback
{ {
putenv('environmental=' . Snowflake::TASK); putenv('environmental=' . Snowflake::TASK);
$annotation->runtime(directory('app'), [ $annotation->runtime(APP_PATH, [CONTROLLER_PATH, TASK_PATH, LISTENER_PATH]);
CONTROLLER_PATH,
TASK_PATH,
]);
name($server->worker_pid, 'Task#' . $server->worker_id); name($server->worker_pid, 'Task#' . $server->worker_id);
Snowflake::setTaskId($server->worker_pid); Snowflake::setTaskId($server->worker_pid);
@@ -93,9 +98,10 @@ class OnWorkerStart extends Callback
name($server->worker_pid, 'Worker#' . $server->worker_id); name($server->worker_pid, 'Worker#' . $server->worker_id);
$time = microtime(true); $time = microtime(true);
$annotation->runtime(CONTROLLER_PATH); $annotation->runtime(CONTROLLER_PATH);
$this->debug('use time.' . (microtime(true) - $time)); $this->debug('use time.' . (microtime(true) - $time));
$annotation->runtime(directory('app'), CONTROLLER_PATH); $annotation->runtime(directory('app'), [CONTROLLER_PATH]);
Snowflake::setWorkerId($server->worker_pid); Snowflake::setWorkerId($server->worker_pid);
putenv('environmental=' . Snowflake::WORKER); putenv('environmental=' . Snowflake::WORKER);
+2
View File
@@ -23,6 +23,7 @@ class Runtime extends Command
const CACHE_NAME = '.runtime.cache'; const CACHE_NAME = '.runtime.cache';
const CONFIG_NAME = '.config.cache';
/** /**
@@ -36,6 +37,7 @@ class Runtime extends Command
$runtime = storage(static::CACHE_NAME); $runtime = storage(static::CACHE_NAME);
Snowflake::writeFile(storage(static::CONFIG_NAME), serialize(sweep()));
Snowflake::writeFile($runtime, serialize($annotation->getLoader())); Snowflake::writeFile($runtime, serialize($annotation->getLoader()));
} }