This commit is contained in:
2023-08-11 02:32:04 +08:00
parent db7e16c35c
commit 3196a7cffb
2 changed files with 95 additions and 122 deletions
+95 -77
View File
@@ -17,6 +17,7 @@ use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Swoole\Server;
use Swoole\Timer;
/**
@@ -27,92 +28,109 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server
{
/**
* @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);
/**
* @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']) {
$dispatch->dispatch(new OnWorkerStart($server, $workerId));
} else {
$dispatch->dispatch(new OnTaskStart($server, $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());
}
$dispatch->dispatch(new OnAfterWorkerStart());
}
/**
* @param Server $server
* @param int $workerId
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|ReflectionException
*/
public function onWorkerStop(Server $server, int $workerId): void
{
event(new OnWorkerStop($server, $workerId));
}
/**
* @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 onWorkerExit(Server $server, int $workerId): void
{
event(new OnWorkerExit($server, $workerId));
}
/**
* @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 $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));
$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 Server $server
* @param int $workerId
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|ReflectionException
*/
public function onWorkerExit(Server $server, int $workerId): void
{
event(new OnWorkerExit($server, $workerId));
}
/**
* @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']);
}
}
/**
* @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));
$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']);
}
}
}
-45
View File
@@ -8,13 +8,9 @@ use Kiri;
use Kiri\Events\EventDispatch;
use Kiri\Router\Router;
use Kiri\Server\Events\OnShutdown;
use Kiri\Server\Events\OnTaskerStart;
use Kiri\Server\Events\OnWorkerStart;
use Kiri\Server\Events\OnWorkerStop;
use Kiri\Server\Abstracts\AsyncServer;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Swoole\Timer;
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
@@ -48,23 +44,12 @@ class Server
}
/**
* @return void
*/
public function init(): void
{
on(OnWorkerStart::class, [$this, 'onWorkerName']);
on(OnTaskerStart::class, [$this, 'onTaskerName']);
}
/**
* @return void
* @throws Exception
*/
public function start(): void
{
on(OnWorkerStop::class, [Timer::class, 'clearAll'], 9999);
if (\config('reload.hot', false) === true) {
$this->manager->addProcess(HotReload::class);
} else {
@@ -75,36 +60,6 @@ class Server
}
/**
* @param OnWorkerStart $onWorkerStart
*/
public function onWorkerName(OnWorkerStart $onWorkerStart): void
{
if (!property_exists($onWorkerStart->server, 'worker_pid')) {
return;
}
$prefix = sprintf('Worker Process[%d].%d', $onWorkerStart->server->worker_pid, $onWorkerStart->workerId);
set_env('environmental', Kiri::WORKER);
Kiri::setProcessName($prefix);
}
/**
* @param OnTaskerStart $onWorkerStart
*/
public function onTaskerName(OnTaskerStart $onWorkerStart): void
{
if (!property_exists($onWorkerStart->server, 'worker_pid')) {
return;
}
$prefix = sprintf('Tasker Process[%d].%d', $onWorkerStart->server->worker_pid, $onWorkerStart->workerId);
set_env('environmental', Kiri::TASK);
Kiri::setProcessName($prefix);
}
/**
* @return void
* @throws ContainerExceptionInterface