modify plugin name

This commit is contained in:
2022-06-16 17:38:22 +08:00
parent 0233acb279
commit 0051f1f15c
25 changed files with 928 additions and 1055 deletions
+57 -12
View File
@@ -2,14 +2,18 @@
namespace Kiri\Server\Handler;
use Kiri\Annotation\Inject;
use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException;
use Kiri\Server\Events\OnAfterReload;
use Kiri\Server\Events\OnBeforeReload;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Kiri\Server\Abstracts\Server;
use Kiri\Server\Events\OnBeforeShutdown;
use Kiri\Server\Events\OnShutdown;
use Kiri\Server\Events\OnStart;
use Swoole\Server as SServer;
/**
@@ -18,39 +22,80 @@ use Kiri\Server\Events\OnStart;
*/
class OnServer extends Server
{
/**
* @param \Swoole\Server $server
* @param EventDispatch $dispatch
* @throws \Exception
*/
public function __construct(public EventDispatch $dispatch)
{
parent::__construct();
}
/**
* @param SServer $server
* @throws ConfigException
* @throws ReflectionException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function onStart(\Swoole\Server $server)
public function onStart(SServer $server)
{
$this->setProcessName(sprintf('start[%d].server', $server->master_pid));
\Kiri::setProcessName(sprintf('start[%d].server', $server->master_pid));
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnStart($server));
$this->dispatch->dispatch(new OnStart($server));
}
/**
* @param \Swoole\Server $server
* @param SServer $server
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function onBeforeShutdown(\Swoole\Server $server)
public function onBeforeShutdown(SServer $server)
{
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnBeforeShutdown($server));
$this->dispatch->dispatch(new OnBeforeShutdown($server));
}
/**
* @param \Swoole\Server $server
* @param SServer $server
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function onShutdown(\Swoole\Server $server)
public function onShutdown(SServer $server)
{
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnShutdown($server));
$this->dispatch->dispatch(new OnShutdown($server));
}
/**
* @param SServer $server
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function onBeforeReload(SServer $server)
{
$this->dispatch->dispatch(new OnBeforeReload($server));
}
/**
* @param SServer $server
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function onAfterReload(SServer $server)
{
$this->dispatch->dispatch(new OnAfterReload($server));
}
}
+21 -5
View File
@@ -2,8 +2,11 @@
namespace Kiri\Server\Handler;
use Kiri;
use Kiri\Annotation\Inject;
use Kiri\Events\EventDispatch;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Kiri\Server\Abstracts\Server;
use Kiri\Exception\ConfigException;
@@ -18,26 +21,39 @@ use Kiri\Server\Events\OnManagerStop;
class OnServerManager extends Server
{
/**
* @param EventDispatch $dispatch
* @throws \Exception
*/
public function __construct(public EventDispatch $dispatch)
{
parent::__construct();
}
/**
* @param \Swoole\Server $server
* @throws ConfigException|ReflectionException
* @param \Swoole\Server $server
* @throws ConfigException
* @throws ReflectionException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function onManagerStart(\Swoole\Server $server)
{
$this->setProcessName(sprintf('manger[%d].0', $server->manager_pid));
Kiri::setProcessName(sprintf('manger[%d].0', $server->manager_pid));
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnManagerStart($server));
$this->dispatch->dispatch(new OnManagerStart($server));
}
/**
* @param \Swoole\Server $server
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function onManagerStop(\Swoole\Server $server)
{
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnManagerStop($server));
$this->dispatch->dispatch(new OnManagerStop($server));
}
-38
View File
@@ -1,38 +0,0 @@
<?php
namespace Kiri\Server\Handler;
use Kiri\Annotation\Inject;
use Kiri\Events\EventDispatch;
use Kiri\Server\Events\OnAfterReload;
use Kiri\Server\Events\OnBeforeReload;
use Swoole\Server;
/**
*
*/
class OnServerReload
{
/**
* @param Server $server
* @throws \ReflectionException
*/
public function onBeforeReload(Server $server)
{
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnBeforeReload($server));
}
/**
* @param Server $server
* @throws \ReflectionException
*/
public function onAfterReload(Server $server)
{
\Kiri::getDi()->get(EventDispatch::class)->dispatch(new OnAfterReload($server));
}
}
+85 -96
View File
@@ -30,111 +30,100 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server
{
public Router $router;
/**
* @param EventDispatch $dispatch
* @param Router $router
* @throws Exception
*/
public function __construct(public EventDispatch $dispatch, public Router $router)
{
parent::__construct();
}
public EventDispatch $dispatch;
/**
* @param Server $server
* @param int $workerId
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function onWorkerStart(Server $server, int $workerId): void
{
$this->dispatch->dispatch(new OnBeforeWorkerStart($workerId));
set_env('environmental_workerId', $workerId);
if ($workerId < $server->setting['worker_num']) {
$this->dispatch->dispatch(new OnWorkerStart($server, $workerId));
} else {
$this->dispatch->dispatch(new OnTaskStart($server, $workerId));
}
$this->dispatch->dispatch(new OnAfterWorkerStart());
}
/**
* @return void
*/
public function init()
{
$this->dispatch = Kiri::getDi()->get(EventDispatch::class);
$this->router = Kiri::getDi()->get(Router::class);
}
/**
* @param Server $server
* @param int $workerId
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|ReflectionException
*/
public function onWorkerStop(Server $server, int $workerId)
{
$this->dispatch->dispatch(new OnWorkerStop($server, $workerId));
}
/**
* @param Server $server
* @param int $workerId
* @return void
* @throws Kiri\Exception\ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function onWorkerStart(Server $server, int $workerId)
{
$this->dispatch->dispatch(new OnBeforeWorkerStart($workerId));
set_env('environmental_workerId', $workerId);
if ($workerId < $server->setting['worker_num']) {
set_env('environmental', Kiri::WORKER);
$this->setProcessName(sprintf('Worker Process[%d].%d', $server->worker_pid, $workerId));
$this->dispatch->dispatch(new OnWorkerStart($server, $workerId));
} else {
set_env('environmental', Kiri::TASK);
$this->setProcessName(sprintf('Tasker Process[%d].%d', $server->worker_pid, $workerId));
$this->dispatch->dispatch(new OnTaskStart($server, $workerId));
}
$this->dispatch->dispatch(new OnAfterWorkerStart());
}
/**
* @param Server $server
* @param int $workerId
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|ReflectionException
*/
public function onWorkerExit(Server $server, int $workerId)
{
$this->dispatch->dispatch(new OnWorkerExit($server, $workerId));
}
/**
* @param Server $server
* @param int $workerId
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|ReflectionException
*/
public function onWorkerStop(Server $server, int $workerId)
{
Timer::clearAll();
$this->dispatch->dispatch(new OnWorkerStop($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)
{
$this->dispatch->dispatch(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)
);
$this->logger->error($message);
$this->system_mail($message);
}
/**
* @param Server $server
* @param int $workerId
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|ReflectionException
*/
public function onWorkerExit(Server $server, int $workerId)
{
$this->dispatch->dispatch(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)
{
$this->dispatch->dispatch(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)
);
$this->logger->error($message);
$this->system_mail($message);
}
/**
* @param $messageContent
* @throws Exception
*/
protected function system_mail($messageContent)
{
try {
$email = Config::get('email', ['enable' => false]);
if (!empty($email) && ($email['enable'] ?? false) == true) {
Help::sendEmail($email, 'Service Error', $messageContent);
}
} catch (\Throwable $e) {
error($e, 'email');
}
}
/**
* @param $messageContent
* @throws Exception
*/
protected function system_mail($messageContent)
{
try {
$email = Config::get('email', ['enable' => false]);
if (!empty($email) && ($email['enable'] ?? false)) {
Help::sendEmail($email, 'Service Error', $messageContent);
}
} catch (\Throwable $e) {
error($e, 'email');
}
}
}