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\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;
/** /**
@@ -23,84 +21,92 @@ class OnWorkerStart extends Callback
{ {
/** /**
* @param Server $server * @param Server $server
* @param int $worker_id * @param int $worker_id
* *
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
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->setLoader(unserialize(file_get_contents(storage(Runtime::CACHE_NAME))));
$annotation = Snowflake::app()->getAnnotation(); if ($worker_id >= $server->setting['worker_num']) {
$annotation->setLoader(unserialize(file_get_contents(storage(Runtime::CACHE_NAME)))); $this->onTask($server, $annotation);
if ($worker_id >= $server->setting['worker_num']) { } else {
$annotation->runtime(APP_PATH, [CONTROLLER_PATH, TASK_PATH, LISTENER_PATH]); $this->onWorker($server, $annotation);
$this->onTask($server, $annotation); }
} else { }
$annotation->runtime(CONTROLLER_PATH);
$annotation->runtime(directory('app'), [CONTROLLER_PATH]);
$this->onWorker($server, $annotation);
}
}
/** /**
* @param Server $server * @param $worker_id
* @param int $worker_id * @throws Exception
* @return bool */
*/ private function setConfigs($worker_id)
private function isWorker(Server $server, int $worker_id): bool {
{ putenv('state=start');
return $worker_id < $server->setting['worker_num']; putenv('worker=' . $worker_id);
}
$serialize = file_get_contents(Runtime::CONFIG_NAME);
Config::sets(unserialize($serialize));
}
/** /**
* @param Server $server * @param Server $server
* @param Annotation $annotation * @param int $worker_id
* @throws ConfigException * @return bool
* @throws Exception */
*/ private function isWorker(Server $server, int $worker_id): bool
public function onTask(Server $server, Annotation $annotation) {
{ return $worker_id < $server->setting['worker_num'];
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 Server $server
* @param Annotation $annotation * @param Annotation $annotation
* @throws Exception * @throws ConfigException
*/ * @throws Exception
public function onWorker(Server $server, Annotation $annotation) */
{ public function onTask(Server $server, Annotation $annotation)
name($server->worker_pid, 'Worker#' . $server->worker_id); {
putenv('environmental=' . Snowflake::TASK);
$time = microtime(true); $annotation->runtime(APP_PATH, [CONTROLLER_PATH, TASK_PATH, LISTENER_PATH]);
$annotation->runtime(CONTROLLER_PATH);
$this->debug('use time.' . (microtime(true) - $time));
$annotation->runtime(directory('app'), CONTROLLER_PATH);
Snowflake::setWorkerId($server->worker_pid); name($server->worker_pid, 'Task#' . $server->worker_id);
putenv('environmental=' . Snowflake::WORKER);
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 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()));
} }