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

120 lines
3.0 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;
/**
* 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-06-16 17:38:22 +08:00
/**
* @param Server $server
* @param int $workerId
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function onWorkerStart(Server $server, int $workerId): void
{
2023-04-17 01:29:43 +08:00
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnBeforeWorkerStart($workerId));
2022-06-16 17:38:22 +08:00
set_env('environmental_workerId', $workerId);
2023-04-17 01:29:43 +08:00
2022-06-16 17:38:22 +08:00
if ($workerId < $server->setting['worker_num']) {
2023-04-17 01:29:43 +08:00
$dispatch->dispatch(new OnWorkerStart($server, $workerId));
2022-06-16 17:38:22 +08:00
} else {
2023-04-17 01:29:43 +08:00
$dispatch->dispatch(new OnTaskStart($server, $workerId));
2022-06-16 17:38:22 +08:00
}
2023-04-17 01:29:43 +08:00
$dispatch->dispatch(new OnAfterWorkerStart());
2022-06-16 17:38:22 +08:00
}
/**
* @param Server $server
* @param int $workerId
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|ReflectionException
*/
2023-04-21 22:26:43 +08:00
public function onWorkerStop(Server $server, int $workerId): void
2022-06-16 17:38:22 +08:00
{
2023-04-21 22:26:43 +08:00
event(new OnWorkerStop($server, $workerId));
2022-06-16 17:38:22 +08:00
}
/**
* @param Server $server
* @param int $workerId
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|ReflectionException
*/
2023-04-21 22:26:43 +08:00
public function onWorkerExit(Server $server, int $workerId): void
2022-06-16 17:38:22 +08:00
{
2023-04-21 22:26:43 +08:00
event(new OnWorkerExit($server, $workerId));
2022-06-16 17:38:22 +08:00
}
/**
* @param Server $server
* @param int $worker_id
* @param int $worker_pid
* @param int $exit_code
* @param int $signal
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
2023-04-21 22:26:43 +08:00
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal): void
2022-06-16 17:38:22 +08:00
{
2023-04-17 01:29:43 +08:00
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal));
2022-06-16 17:38:22 +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)
);
2023-04-21 22:26:43 +08:00
error($message);
2022-06-16 17:38:22 +08:00
$this->system_mail($message);
}
/**
* @param $messageContent
* @throws Exception
*/
2023-04-21 22:26:43 +08:00
protected function system_mail($messageContent): void
2022-06-16 17:38:22 +08:00
{
try {
2023-05-25 16:59:17 +08:00
$email = \config('email', ['enable' => false]);
2022-06-16 17:38:22 +08:00
if (!empty($email) && ($email['enable'] ?? false)) {
Help::sendEmail($email, 'Service Error', $messageContent);
}
} catch (\Throwable $e) {
2023-04-16 02:15:51 +08:00
error($e, ['email']);
2022-06-16 17:38:22 +08:00
}
}
2022-01-09 03:49:02 +08:00
}