qqq
This commit is contained in:
+95
-77
@@ -17,6 +17,7 @@ use Psr\Container\ContainerExceptionInterface;
|
|||||||
use Psr\Container\NotFoundExceptionInterface;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
|
use Swoole\Timer;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,92 +28,109 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $workerId
|
* @param int $workerId
|
||||||
* @return void
|
* @return void
|
||||||
* @throws ContainerExceptionInterface
|
* @throws ContainerExceptionInterface
|
||||||
* @throws NotFoundExceptionInterface
|
* @throws NotFoundExceptionInterface
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onWorkerStart(Server $server, int $workerId): void
|
public function onWorkerStart(Server $server, int $workerId): void
|
||||||
{
|
{
|
||||||
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
|
$dispatch = \Kiri::getDi()->get(EventDispatch::class);
|
||||||
$dispatch->dispatch(new OnBeforeWorkerStart($workerId));
|
$dispatch->dispatch(new OnBeforeWorkerStart($workerId));
|
||||||
set_env('environmental_workerId', $workerId);
|
set_env('environmental_workerId', $workerId);
|
||||||
|
|
||||||
if ($workerId < $server->setting['worker_num']) {
|
if ($workerId < $server->setting['worker_num']) {
|
||||||
$dispatch->dispatch(new OnWorkerStart($server, $workerId));
|
$this->processName($server, 'Worker');
|
||||||
} else {
|
set_env('environmental', Kiri::WORKER);
|
||||||
$dispatch->dispatch(new OnTaskStart($server, $workerId));
|
$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 Server $server
|
||||||
* @param int $workerId
|
* @param string $prefix
|
||||||
* @throws ContainerExceptionInterface
|
* @return void
|
||||||
* @throws NotFoundExceptionInterface|ReflectionException
|
*/
|
||||||
*/
|
protected function processName(Server $server, string $prefix): void
|
||||||
public function onWorkerStop(Server $server, int $workerId): void
|
{
|
||||||
{
|
$prefix = sprintf($prefix . ' Process[%d].%d', $server->worker_pid, $server->worker_id);
|
||||||
event(new OnWorkerStop($server, $workerId));
|
Kiri::setProcessName($prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $workerId
|
* @param int $workerId
|
||||||
* @throws ContainerExceptionInterface
|
* @throws ContainerExceptionInterface
|
||||||
* @throws NotFoundExceptionInterface|ReflectionException
|
* @throws NotFoundExceptionInterface|ReflectionException
|
||||||
*/
|
*/
|
||||||
public function onWorkerExit(Server $server, int $workerId): void
|
public function onWorkerStop(Server $server, int $workerId): void
|
||||||
{
|
{
|
||||||
event(new OnWorkerExit($server, $workerId));
|
event(new OnWorkerStop($server, $workerId));
|
||||||
}
|
Timer::clearAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $worker_id
|
* @param int $workerId
|
||||||
* @param int $worker_pid
|
* @throws ContainerExceptionInterface
|
||||||
* @param int $exit_code
|
* @throws NotFoundExceptionInterface|ReflectionException
|
||||||
* @param int $signal
|
*/
|
||||||
* @throws ContainerExceptionInterface
|
public function onWorkerExit(Server $server, int $workerId): void
|
||||||
* @throws NotFoundExceptionInterface
|
{
|
||||||
* @throws Exception
|
event(new OnWorkerExit($server, $workerId));
|
||||||
*/
|
}
|
||||||
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
|
* @param Server $server
|
||||||
* @throws Exception
|
* @param int $worker_id
|
||||||
*/
|
* @param int $worker_pid
|
||||||
protected function system_mail($messageContent): void
|
* @param int $exit_code
|
||||||
{
|
* @param int $signal
|
||||||
try {
|
* @throws ContainerExceptionInterface
|
||||||
$email = \config('email', ['enable' => false]);
|
* @throws NotFoundExceptionInterface
|
||||||
if (!empty($email) && ($email['enable'] ?? false)) {
|
* @throws Exception
|
||||||
Help::sendEmail($email, 'Service Error', $messageContent);
|
*/
|
||||||
}
|
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal): void
|
||||||
} catch (\Throwable $e) {
|
{
|
||||||
error($e, ['email']);
|
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
@@ -8,13 +8,9 @@ use Kiri;
|
|||||||
use Kiri\Events\EventDispatch;
|
use Kiri\Events\EventDispatch;
|
||||||
use Kiri\Router\Router;
|
use Kiri\Router\Router;
|
||||||
use Kiri\Server\Events\OnShutdown;
|
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 Kiri\Server\Abstracts\AsyncServer;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
use Swoole\Timer;
|
|
||||||
|
|
||||||
|
|
||||||
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
|
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
|
* @return void
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function start(): void
|
public function start(): void
|
||||||
{
|
{
|
||||||
on(OnWorkerStop::class, [Timer::class, 'clearAll'], 9999);
|
|
||||||
if (\config('reload.hot', false) === true) {
|
if (\config('reload.hot', false) === true) {
|
||||||
$this->manager->addProcess(HotReload::class);
|
$this->manager->addProcess(HotReload::class);
|
||||||
} else {
|
} 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
|
* @return void
|
||||||
* @throws ContainerExceptionInterface
|
* @throws ContainerExceptionInterface
|
||||||
|
|||||||
Reference in New Issue
Block a user