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

157 lines
4.6 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
2022-01-12 14:43:28 +08:00
use Kiri;
2024-11-18 11:24:08 +08:00
use Kiri\Abstracts\CoordinatorManager;
use Kiri\Coordinator;
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;
2023-11-22 09:26:18 +08:00
use Kiri\Server\Events\OnTaskerStart as OnTaskerStart;
2022-01-10 11:39:55 +08:00
use Kiri\Server\Events\OnWorkerError;
use Kiri\Server\Events\OnWorkerExit;
use Kiri\Server\Events\OnWorkerStart;
use Kiri\Server\Events\OnWorkerStop;
2024-09-29 09:01:03 +08:00
use Psr\Http\Message\RequestInterface;
2022-01-09 03:49:02 +08:00
use Swoole\Server;
2023-08-11 02:32:04 +08:00
use Swoole\Timer;
2023-12-12 18:03:07 +08:00
use Throwable;
use function config;
2022-01-09 03:49:02 +08:00
/**
* Class OnServerWorker
* @package Server\Worker
*/
2023-12-12 18:03:07 +08:00
class OnServerWorker extends Kiri\Server\Abstracts\Server
2022-01-09 03:49:02 +08:00
{
2023-12-02 17:27:22 +08:00
/**
* @return void
*/
public function init(): void
{
on(OnBeforeWorkerStart::class, [$this, 'onWorkerNameAlias']);
}
/**
* @param OnBeforeWorkerStart $workerStart
* @return void
*/
public function onWorkerNameAlias(OnBeforeWorkerStart $workerStart): void
{
set_env('environmental_workerId', $workerStart->workerId);
if ($workerStart->workerId < $workerStart->server->setting['worker_num']) {
$this->processName($workerStart->server, 'Worker');
set_env('environmental', Kiri::WORKER);
} else {
$this->processName($workerStart->server, 'Tasker');
set_env('environmental', Kiri::TASK);
}
}
2023-08-11 02:32:04 +08:00
/**
* @param Server $server
* @param int $workerId
* @return void
2023-12-12 15:35:34 +08:00
* @throws
2023-08-11 02:32:04 +08:00
*/
public function onWorkerStart(Server $server, int $workerId): void
{
2024-08-29 17:01:07 +08:00
$dispatch = Kiri::getDi()->get(EventDispatch::class);
$dispatch->dispatch(new OnBeforeWorkerStart($server, $workerId));
2023-08-11 02:32:04 +08:00
if ($workerId < $server->setting['worker_num']) {
2024-11-18 11:24:08 +08:00
CoordinatorManager::utility(Coordinator::WORKER_START)->waite();
2024-08-29 17:01:07 +08:00
$dispatch->dispatch(new OnWorkerStart($server, $workerId));
2023-08-11 02:32:04 +08:00
} else {
2024-08-29 17:01:07 +08:00
$dispatch->dispatch(new OnTaskerStart($server, $workerId));
2023-08-11 02:32:04 +08:00
}
2024-08-29 17:01:07 +08:00
$dispatch->dispatch(new OnAfterWorkerStart($server, $workerId));
2023-08-11 02:32:04 +08:00
}
/**
* @param Server $server
* @param string $prefix
* @return void
*/
protected function processName(Server $server, string $prefix): void
{
2024-09-04 11:52:08 +08:00
Kiri::setProcessName(sprintf($prefix . '[%d]', $server->worker_pid));
2023-08-11 02:32:04 +08:00
}
/**
* @param Server $server
* @param int $workerId
2023-12-12 15:35:34 +08:00
* @throws
2023-08-11 02:32:04 +08:00
*/
public function onWorkerStop(Server $server, int $workerId): void
{
event(new OnWorkerStop($server, $workerId));
Timer::clearAll();
}
/**
* @param Server $server
* @param int $workerId
2023-12-12 15:35:34 +08:00
* @throws
2023-08-11 02:32:04 +08:00
*/
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
2023-12-12 15:35:34 +08:00
* @throws
2023-08-11 02:32:04 +08:00
*/
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
2024-09-29 09:01:03 +08:00
/** @var RequestInterface $context */
$context = Kiri\Di\Context::get(RequestInterface::class);
if (is_null($context)) {
$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(), $signal));
} else {
$message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s, method: %s, path: %s, query: %s', $worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), $signal),
$context->getMethod(), $context->getUri()->getPath(), $context->getUri()->getQuery());
}
2024-09-29 09:01:39 +08:00
error($message . PHP_EOL);
2023-08-11 02:32:04 +08:00
$this->system_mail($message);
}
/**
* @param $messageContent
2023-12-12 15:35:34 +08:00
* @throws
2023-08-11 02:32:04 +08:00
*/
protected function system_mail($messageContent): void
{
try {
2023-12-12 18:03:07 +08:00
$email = config('email', ['enable' => false]);
2023-08-11 02:32:04 +08:00
if (!empty($email) && ($email['enable'] ?? false)) {
Help::sendEmail($email, 'Service Error', $messageContent);
}
2023-12-12 18:03:07 +08:00
} catch (Throwable $e) {
2023-08-11 02:32:04 +08:00
error($e, ['email']);
}
}
2022-01-09 03:49:02 +08:00
}