Files
kiri-http-server/Handler/OnServerWorker.php
T

128 lines
3.2 KiB
PHP
Raw Normal View History

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-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
{
2022-01-14 15:20:57 +08:00
public Router $collector;
/**
* @return void
*/
public function init()
{
2022-02-15 17:25:14 +08:00
// $this->collector = Kiri::getDi()->get(Router::class);
2022-01-14 15:20:57 +08:00
}
2022-01-09 03:49:02 +08:00
/**
* @param Server $server
* @param int $workerId
* @throws Exception
*/
public function onWorkerStart(Server $server, int $workerId)
{
2022-01-20 19:04:15 +08:00
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->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']) {
2022-02-18 14:01:24 +08:00
$this->setProcessName(sprintf('Worker Process[%d].%d', $server->worker_pid, $workerId));
2022-01-20 19:04:15 +08:00
$dispatch->dispatch(new OnWorkerStart($server, $workerId));
2022-01-09 03:49:02 +08:00
set_env('environmental', Kiri::WORKER);
} else {
2022-01-20 19:04:15 +08:00
$dispatch->dispatch(new OnTaskStart($server, $workerId));
2022-02-18 14:01:24 +08:00
$this->setProcessName(sprintf('Tasker Process[%d].%d', $server->worker_pid, $workerId));
2022-01-09 03:49:02 +08:00
set_env('environmental', Kiri::TASK);
}
2022-01-20 19:04:15 +08:00
$dispatch->dispatch(new OnAfterWorkerStart());
2022-01-09 03:49:02 +08:00
}
/**
* @param Server $server
* @param int $workerId
* @throws Exception
*/
public function onWorkerStop(Server $server, int $workerId)
{
Timer::clearAll();
2022-01-20 19:04:15 +08:00
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnWorkerStop($server, $workerId));
2022-01-09 03:49:02 +08:00
}
/**
* @param Server $server
* @param int $workerId
* @throws Exception
*/
public function onWorkerExit(Server $server, int $workerId)
{
2022-01-20 19:04:15 +08:00
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnWorkerExit($server, $workerId));
2022-01-09 03:49:02 +08:00
}
/**
* @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)
{
2022-01-20 19:04:15 +08:00
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal));
2022-01-09 03:49:02 +08:00
$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');
}
}
}