Files
kiri-core/Server/Worker/ServerWorker.php
T

163 lines
3.7 KiB
PHP
Raw Normal View History

2021-07-17 02:16:49 +08:00
<?php
2021-07-18 12:06:56 +08:00
namespace Server\Worker;
2021-07-17 02:16:49 +08:00
2021-07-23 10:48:18 +08:00
use Annotation\Annotation;
2021-07-19 19:04:13 +08:00
use Exception;
use Server\Constant;
use Snowflake\Abstracts\Config;
2021-07-21 16:26:30 +08:00
use Snowflake\Core\Help;
2021-07-19 19:04:13 +08:00
use Snowflake\Event;
2021-07-21 16:07:04 +08:00
use Snowflake\Exception\ConfigException;
2021-07-19 19:04:13 +08:00
use Snowflake\Runtime;
use Snowflake\Snowflake;
2021-07-17 02:16:49 +08:00
use Swoole\Server;
2021-07-21 11:25:31 +08:00
use Swoole\Timer;
2021-07-17 02:16:49 +08:00
2021-07-19 19:04:13 +08:00
/**
* Class ServerWorker
* @package Server\Worker
*/
class ServerWorker extends \Server\Abstracts\Server
2021-07-17 02:16:49 +08:00
{
2021-07-20 16:38:51 +08:00
/**
* @param Server $server
* @param int $workerId
* @throws Exception
*/
public function onWorkerStart(Server $server, int $workerId)
{
$this->_setConfigCache($workerId);
$annotation = Snowflake::app()->getAnnotation();
$annotation->read(APP_PATH . 'app');
$this->runEvent(Constant::WORKER_START, null, [$server, $workerId]);
2021-07-23 10:48:18 +08:00
$this->workerInitExecutor($server, $annotation, $workerId);
}
/**
* @param Server $server
* @param Annotation $annotation
* @param int $workerId
* @throws ConfigException
* @throws Exception
*/
private function workerInitExecutor(Server $server, Annotation $annotation, int $workerId)
{
2021-07-21 17:02:50 +08:00
if ($workerId < $server->setting['worker_num']) {
2021-07-21 11:25:31 +08:00
$loader = Snowflake::app()->getRouter();
$loader->_loader();
2021-07-23 11:55:02 +08:00
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL;
2021-07-26 02:58:45 +08:00
$this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId));
2021-07-21 16:07:04 +08:00
2021-07-20 16:38:51 +08:00
$annotation->runtime(CONTROLLER_PATH);
2021-07-21 11:25:31 +08:00
$annotation->runtime(MODEL_PATH);
} else {
2021-07-23 11:55:02 +08:00
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Tasker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL;
2021-07-23 10:48:18 +08:00
2021-07-26 02:58:45 +08:00
$this->setProcessName(sprintf('Tasker[%d].%d', $server->worker_pid, $workerId));
2021-07-21 16:07:04 +08:00
2021-07-21 11:25:31 +08:00
$annotation->runtime(APP_PATH, [CONTROLLER_PATH]);
2021-07-20 16:38:51 +08:00
}
}
2021-07-21 16:07:04 +08:00
2021-07-20 16:38:51 +08:00
/**
* @param $worker_id
* @throws Exception
*/
private function _setConfigCache($worker_id)
{
putenv('state=start');
putenv('worker=' . $worker_id);
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
if (empty($serialize)) {
return;
}
Config::sets(unserialize($serialize));
}
/**
* @param Server $server
* @param int $workerId
2021-07-21 11:25:31 +08:00
* @throws Exception
2021-07-20 16:38:51 +08:00
*/
public function onWorkerStop(Server $server, int $workerId)
{
$this->runEvent(Constant::WORKER_STOP, null, [$server, $workerId]);
2021-07-21 11:25:31 +08:00
Event::trigger(Event::SERVER_WORKER_STOP);
fire(Event::SYSTEM_RESOURCE_CLEAN);
Timer::clearAll();
2021-07-20 16:38:51 +08:00
}
/**
* @param Server $server
* @param int $workerId
2021-07-21 11:25:31 +08:00
* @throws Exception
2021-07-20 16:38:51 +08:00
*/
public function onWorkerExit(Server $server, int $workerId)
{
$this->runEvent(Constant::WORKER_EXIT, null, [$server, $workerId]);
2021-07-21 11:25:31 +08:00
putenv('state=exit');
2021-07-21 15:35:40 +08:00
Event::trigger(Event::SERVER_WORKER_EXIT, [$server, $workerId]);
2021-07-21 11:25:31 +08:00
Snowflake::getApp('logger')->insert();
2021-07-20 16:38:51 +08:00
}
/**
* @param Server $server
* @param int $worker_id
* @param int $worker_pid
* @param int $exit_code
* @param int $signal
2021-07-21 11:25:31 +08:00
* @throws Exception
2021-07-20 16:38:51 +08:00
*/
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]);
2021-07-21 11:25:31 +08:00
Event::trigger(Event::SERVER_WORKER_ERROR);
$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)
);
write($message, 'worker-exit');
$this->system_mail($message);
}
/**
* @param $messageContent
* @throws Exception
*/
protected function system_mail($messageContent)
{
try {
$email = Config::get('email');
2021-07-21 16:26:30 +08:00
if (!empty($email) && ($email['enable'] ?? false) == true) {
Help::sendEmail($email, 'Service Error', $messageContent);
2021-07-21 11:25:31 +08:00
}
} catch (\Throwable $e) {
error($e, 'email');
}
2021-07-20 16:38:51 +08:00
}
2021-07-17 02:16:49 +08:00
}