2022-01-09 03:49:02 +08:00
|
|
|
<?php
|
|
|
|
|
|
2022-01-10 11:39:55 +08:00
|
|
|
namespace Kiri\Server\Handler;
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
use Exception;
|
2022-01-12 14:43:28 +08:00
|
|
|
use Kiri;
|
2022-01-09 03:49:02 +08:00
|
|
|
use Kiri\Abstracts\Config;
|
2022-01-12 11:39:16 +08:00
|
|
|
use Kiri\Annotation\Inject;
|
2022-01-09 03:49:02 +08:00
|
|
|
use Kiri\Core\Help;
|
|
|
|
|
use Kiri\Events\EventDispatch;
|
2022-01-12 14:43:28 +08:00
|
|
|
use Kiri\Message\Handler\Router;
|
2022-01-18 16:07:05 +08:00
|
|
|
use Kiri\Message\Waite;
|
2022-01-10 11:39:55 +08:00
|
|
|
use Kiri\Server\Events\OnAfterWorkerStart;
|
|
|
|
|
use Kiri\Server\Events\OnBeforeWorkerStart;
|
|
|
|
|
use Kiri\Server\Events\OnTaskerStart as OnTaskStart;
|
|
|
|
|
use Kiri\Server\Events\OnWorkerError;
|
|
|
|
|
use Kiri\Server\Events\OnWorkerExit;
|
|
|
|
|
use Kiri\Server\Events\OnWorkerStart;
|
|
|
|
|
use Kiri\Server\Events\OnWorkerStop;
|
2022-01-09 03:49:02 +08:00
|
|
|
use Swoole\Server;
|
|
|
|
|
use Swoole\Timer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class OnServerWorker
|
|
|
|
|
* @package Server\Worker
|
|
|
|
|
*/
|
2022-01-10 13:43:01 +08:00
|
|
|
class OnServerWorker extends \Kiri\Server\Abstracts\Server
|
2022-01-09 03:49:02 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var EventDispatch
|
|
|
|
|
*/
|
|
|
|
|
#[Inject(EventDispatch::class)]
|
|
|
|
|
public EventDispatch $eventDispatch;
|
|
|
|
|
|
|
|
|
|
|
2022-01-14 15:20:57 +08:00
|
|
|
public Router $collector;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function init()
|
|
|
|
|
{
|
|
|
|
|
$this->collector = Kiri::getDi()->get(Router::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:49:02 +08:00
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $workerId
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function onWorkerStart(Server $server, int $workerId)
|
|
|
|
|
{
|
|
|
|
|
$this->eventDispatch->dispatch(new OnBeforeWorkerStart($workerId));
|
2022-01-12 11:39:16 +08:00
|
|
|
set_env('environmental_workerId', $workerId);
|
2022-01-09 03:49:02 +08:00
|
|
|
if ($workerId < $server->setting['worker_num']) {
|
|
|
|
|
$this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId));
|
2022-01-14 15:20:57 +08:00
|
|
|
$this->collector->scan_build_route();
|
2022-01-12 14:46:10 +08:00
|
|
|
$this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
|
2022-01-09 03:49:02 +08:00
|
|
|
set_env('environmental', Kiri::WORKER);
|
|
|
|
|
} else {
|
|
|
|
|
$this->eventDispatch->dispatch(new OnTaskStart($server, $workerId));
|
|
|
|
|
$this->setProcessName(sprintf('Tasker[%d].%d', $server->worker_pid, $workerId));
|
|
|
|
|
set_env('environmental', Kiri::TASK);
|
|
|
|
|
}
|
|
|
|
|
$this->eventDispatch->dispatch(new OnAfterWorkerStart());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $workerId
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function onWorkerStop(Server $server, int $workerId)
|
|
|
|
|
{
|
|
|
|
|
Timer::clearAll();
|
|
|
|
|
$this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $workerId
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function onWorkerExit(Server $server, int $workerId)
|
|
|
|
|
{
|
|
|
|
|
$this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $worker_id
|
|
|
|
|
* @param int $worker_pid
|
|
|
|
|
* @param int $exit_code
|
|
|
|
|
* @param int $signal
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
$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)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->logger->error($message);
|
|
|
|
|
|
|
|
|
|
$this->system_mail($message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $messageContent
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
protected function system_mail($messageContent)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$email = Config::get('email', ['enable' => false]);
|
|
|
|
|
if (!empty($email) && ($email['enable'] ?? false) == true) {
|
|
|
|
|
Help::sendEmail($email, 'Service Error', $messageContent);
|
|
|
|
|
}
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
error($e, 'email');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|