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

138 lines
3.9 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\Core\Help;
use Kiri\Events\EventDispatch;
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-04-10 03:37:58 +08:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2022-06-08 14:53:13 +08:00
use ReflectionException;
2022-01-09 03:49:02 +08:00
use Swoole\Server;
2023-08-11 02:32:04 +08:00
use Swoole\Timer;
2022-01-09 03:49:02 +08:00
/**
* 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
{
2023-08-11 02:32:04 +08:00
/**
* @param Server $server
* @param int $workerId
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function onWorkerStart(Server $server, int $workerId): void
{
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnBeforeWorkerStart($workerId));
set_env('environmental_workerId', $workerId);
if ($workerId < $server->setting['worker_num']) {
$this->processName($server, 'Worker');
set_env('environmental', Kiri::WORKER);
$dispatch->dispatch(new OnWorkerStart($server, $workerId));
} else {
$this->processName($server, 'Tasker');
set_env('environmental', Kiri::TASK);;
$dispatch->dispatch(new OnTaskStart($server, $workerId));
}
$dispatch->dispatch(new OnAfterWorkerStart());
}
/**
* @param Server $server
* @param string $prefix
* @return void
*/
protected function processName(Server $server, string $prefix): void
{
$prefix = sprintf($prefix . ' Process[%d].%d', $server->worker_pid, $server->worker_id);
Kiri::setProcessName($prefix);
}
/**
* @param Server $server
* @param int $workerId
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|ReflectionException
*/
public function onWorkerStop(Server $server, int $workerId): void
{
event(new OnWorkerStop($server, $workerId));
Timer::clearAll();
}
/**
* @param Server $server
* @param int $workerId
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|ReflectionException
*/
public function onWorkerExit(Server $server, int $workerId): void
{
event(new OnWorkerExit($server, $workerId));
}
/**
* @param Server $server
* @param int $worker_id
* @param int $worker_pid
* @param int $exit_code
* @param int $signal
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal): void
{
event(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal));
2023-08-29 20:58:09 +08:00
debug_print_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
2023-08-11 02:32:04 +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)
);
error($message);
$this->system_mail($message);
}
/**
* @param $messageContent
* @throws Exception
*/
protected function system_mail($messageContent): void
{
try {
$email = \config('email', ['enable' => false]);
if (!empty($email) && ($email['enable'] ?? false)) {
Help::sendEmail($email, 'Service Error', $messageContent);
}
} catch (\Throwable $e) {
error($e, ['email']);
}
}
2022-01-09 03:49:02 +08:00
}