This commit is contained in:
as2252258@163.com
2021-07-20 01:57:37 +08:00
parent 46bcbb4f4b
commit 43d0596c12
+59 -57
View File
@@ -19,71 +19,73 @@ class ServerWorker extends \Server\Abstracts\Server
{ {
/** /**
* @param Server $server * @param Server $server
* @param int $workerId * @param int $workerId
* @throws Exception * @throws Exception
*/ */
public function onWorkerStart(Server $server, int $workerId) public function onWorkerStart(Server $server, int $workerId)
{ {
$annotation = Snowflake::app()->getAnnotation(); $this->_setConfigCache($workerId);
$annotation->read(APP_PATH); $annotation = Snowflake::app()->getAnnotation();
$this->runEvent(Constant::WORKER_START, null, [$server, $workerId]); $annotation->read(APP_PATH);
if ($workerId >= $server->setting['worker_num'] + 1) {
$annotation->runtime(CONTROLLER_PATH); $this->runEvent(Constant::WORKER_START, null, [$server, $workerId]);
} if ($workerId >= $server->setting['worker_num'] + 1) {
Event::trigger(Event::SERVER_ON_WORKER_START, [$server, $workerId]); $annotation->runtime(CONTROLLER_PATH);
name($server->worker_pid, 'Worker.' . $workerId); }
} Event::trigger(Event::SERVER_ON_WORKER_START, [$server, $workerId]);
name($server->worker_pid, 'Worker.' . $workerId);
}
/** /**
* @param $worker_id * @param $worker_id
* @throws Exception * @throws Exception
*/ */
private function setConfigs($worker_id) private function _setConfigCache($worker_id)
{ {
putenv('state=start'); putenv('state=start');
putenv('worker=' . $worker_id); putenv('worker=' . $worker_id);
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME)); $serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
if (empty($serialize)) { if (empty($serialize)) {
return; return;
} }
Config::sets(unserialize($serialize)); Config::sets(unserialize($serialize));
} }
/** /**
* @param Server $server * @param Server $server
* @param int $workerId * @param int $workerId
*/ */
public function onWorkerStop(Server $server, int $workerId) public function onWorkerStop(Server $server, int $workerId)
{ {
$this->runEvent(Constant::WORKER_STOP, null, [$server, $workerId]); $this->runEvent(Constant::WORKER_STOP, null, [$server, $workerId]);
} }
/** /**
* @param Server $server * @param Server $server
* @param int $workerId * @param int $workerId
*/ */
public function onWorkerExit(Server $server, int $workerId) public function onWorkerExit(Server $server, int $workerId)
{ {
$this->runEvent(Constant::WORKER_EXIT, null, [$server, $workerId]); $this->runEvent(Constant::WORKER_EXIT, null, [$server, $workerId]);
} }
/** /**
* @param Server $server * @param Server $server
* @param int $worker_id * @param int $worker_id
* @param int $worker_pid * @param int $worker_pid
* @param int $exit_code * @param int $exit_code
* @param int $signal * @param int $signal
*/ */
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal) public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal)
{ {
$this->runEvent(Constant::WORKER_ERROR, null, [$server, $worker_id, $worker_pid, $exit_code, $signal]); $this->runEvent(Constant::WORKER_ERROR, null, [$server, $worker_id, $worker_pid, $exit_code, $signal]);
} }
} }