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
+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');
}
}
}