This commit is contained in:
as2252258@163.com
2021-08-28 19:35:38 +08:00
parent de45170f2c
commit 8d53c9e0f7
+137 -136
View File
@@ -30,168 +30,169 @@ class OnServerWorker extends \Server\Abstracts\Server
{ {
/** /**
* @var EventDispatch * @var EventDispatch
*/ */
#[Inject(EventDispatch::class)] #[Inject(EventDispatch::class)]
public EventDispatch $eventDispatch; public EventDispatch $eventDispatch;
/** /**
* @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)
{ {
$this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId)); $this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
$this->onWorkerStartInit($server, $workerId); $this->onWorkerStartInit($server, $workerId);
$this->eventDispatch->dispatch(new OnAfterWorkerStart()); $this->eventDispatch->dispatch(new OnAfterWorkerStart());
} }
/** /**
* @param Server $server * @param Server $server
* @param int $workerId * @param int $workerId
* @throws ConfigException * @throws ConfigException
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
private function onWorkerStartInit(Server $server, int $workerId) private function onWorkerStartInit(Server $server, int $workerId)
{ {
putenv('state=start'); putenv('state=start');
putenv('worker=' . $workerId); putenv('worker=' . $workerId);
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME)); $serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
if (!empty($serialize)) { if (!empty($serialize)) {
Config::sets(unserialize($serialize)); Config::sets(unserialize($serialize));
} }
if (env('enableFileChange', 'off') == 'off') {
$annotation = Kiri::app()->getAnnotation(); $annotation = Kiri::app()->getAnnotation();
$annotation->read(APP_PATH . 'app', 'App', $annotation->read(APP_PATH . 'app', 'App',
$workerId < $server->setting['worker_num'] ? [] : [CONTROLLER_PATH] $workerId < $server->setting['worker_num'] ? [] : [CONTROLLER_PATH]
); );
$this->workerInitExecutor($server, $annotation, $workerId); $this->workerInitExecutor($server, $annotation, $workerId);
$this->interpretDirectory($annotation); $this->interpretDirectory($annotation);
} }
}
/** /**
* @param Annotation $annotation * @param Annotation $annotation
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
private function interpretDirectory(Annotation $annotation) private function interpretDirectory(Annotation $annotation)
{ {
$fileLists = $annotation->runtime(APP_PATH . 'app'); $fileLists = $annotation->runtime(APP_PATH . 'app');
$di = Kiri::getDi(); $di = Kiri::getDi();
foreach ($fileLists as $class) { foreach ($fileLists as $class) {
foreach ($di->getTargetNote($class) as $value) { foreach ($di->getTargetNote($class) as $value) {
$value->execute($class); $value->execute($class);
} }
$methods = $di->getMethodAttribute($class); $methods = $di->getMethodAttribute($class);
foreach ($methods as $method => $attribute) { foreach ($methods as $method => $attribute) {
if (empty($attribute)) { if (empty($attribute)) {
continue; continue;
} }
foreach ($attribute as $item) { foreach ($attribute as $item) {
$item->execute($class, $method); $item->execute($class, $method);
} }
} }
} }
} }
/** /**
* @param Server $server * @param Server $server
* @param Annotation $annotation * @param Annotation $annotation
* @param int $workerId * @param int $workerId
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
*/ */
private function workerInitExecutor(Server $server, Annotation $annotation, int $workerId) private function workerInitExecutor(Server $server, Annotation $annotation, int $workerId)
{ {
if ($workerId < $server->setting['worker_num']) { if ($workerId < $server->setting['worker_num']) {
$loader = Kiri::app()->getRouter(); $loader = Kiri::app()->getRouter();
$loader->_loader(); $loader->_loader();
putenv('environmental=' . Kiri::WORKER); putenv('environmental=' . Kiri::WORKER);
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL; echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL;
$this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId)); $this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId));
} else { } else {
putenv('environmental=' . Kiri::TASK); putenv('environmental=' . Kiri::TASK);
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Tasker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL; echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Tasker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL;
$this->setProcessName(sprintf('Tasker[%d].%d', $server->worker_pid, $workerId)); $this->setProcessName(sprintf('Tasker[%d].%d', $server->worker_pid, $workerId));
} }
} }
/** /**
* @param Server $server * @param Server $server
* @param int $workerId * @param int $workerId
* @throws Exception * @throws Exception
*/ */
public function onWorkerStop(Server $server, int $workerId) public function onWorkerStop(Server $server, int $workerId)
{ {
$this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId)); $this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId));
Timer::clearAll(); Timer::clearAll();
} }
/** /**
* @param Server $server * @param Server $server
* @param int $workerId * @param int $workerId
* @throws Exception * @throws Exception
*/ */
public function onWorkerExit(Server $server, int $workerId) public function onWorkerExit(Server $server, int $workerId)
{ {
putenv('state=exit'); putenv('state=exit');
$this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId)); $this->eventDispatch->dispatch(new OnWorkerExit($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
* @throws Exception * @throws Exception
*/ */
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->eventDispatch->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal)); $this->eventDispatch->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal));
$message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s', $message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s',
$worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), 9) $worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), 9)
); );
write($message, 'worker-exit'); write($message, 'worker-exit');
$this->system_mail($message); $this->system_mail($message);
} }
/** /**
* @param $messageContent * @param $messageContent
* @throws Exception * @throws Exception
*/ */
protected function system_mail($messageContent) protected function system_mail($messageContent)
{ {
try { try {
$email = Config::get('email'); $email = Config::get('email');
if (!empty($email) && ($email['enable'] ?? false) == true) { if (!empty($email) && ($email['enable'] ?? false) == true) {
Help::sendEmail($email, 'Service Error', $messageContent); Help::sendEmail($email, 'Service Error', $messageContent);
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
error($e, 'email'); error($e, 'email');
} }
} }
} }