改名
This commit is contained in:
@@ -10,6 +10,7 @@ use Kiri\Events\EventDispatch;
|
|||||||
use Kiri\Runtime;
|
use Kiri\Runtime;
|
||||||
use Server\Events\OnAfterWorkerStart;
|
use Server\Events\OnAfterWorkerStart;
|
||||||
use Server\Events\OnBeforeWorkerStart;
|
use Server\Events\OnBeforeWorkerStart;
|
||||||
|
use Server\Events\OnTaskerStart as OnTaskStart;
|
||||||
use Server\Events\OnWorkerError;
|
use Server\Events\OnWorkerError;
|
||||||
use Server\Events\OnWorkerExit;
|
use Server\Events\OnWorkerExit;
|
||||||
use Server\Events\OnWorkerStart;
|
use Server\Events\OnWorkerStart;
|
||||||
@@ -17,7 +18,6 @@ use Server\Events\OnWorkerStop;
|
|||||||
use Server\ServerManager;
|
use Server\ServerManager;
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
use Swoole\Timer;
|
use Swoole\Timer;
|
||||||
use Server\Events\OnTaskerStart as OnTaskStart;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -28,108 +28,108 @@ class OnServerWorker extends \Server\Abstracts\Server
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var EventDispatch
|
* @var EventDispatch
|
||||||
*/
|
*/
|
||||||
#[Inject(EventDispatch::class)]
|
#[Inject(EventDispatch::class)]
|
||||||
public EventDispatch $eventDispatch;
|
public EventDispatch $eventDispatch;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $workerId
|
* @param int $workerId
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onWorkerStart(Server $server, int $workerId)
|
public function onWorkerStart(Server $server, int $workerId)
|
||||||
{
|
{
|
||||||
$this->eventDispatch->dispatch(new OnBeforeWorkerStart($workerId));
|
$this->eventDispatch->dispatch(new OnBeforeWorkerStart($workerId));
|
||||||
if ($workerId < $server->setting['worker_num']) {
|
if ($workerId < $server->setting['worker_num']) {
|
||||||
$this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
|
$this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
|
||||||
$this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId));
|
$this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId));
|
||||||
} else {
|
} else {
|
||||||
$this->eventDispatch->dispatch(new OnTaskStart($server, $workerId));
|
$this->eventDispatch->dispatch(new OnTaskStart($server, $workerId));
|
||||||
$this->setProcessName(sprintf('Tasker[%d].%d', $server->worker_pid, $workerId));
|
$this->setProcessName(sprintf('Tasker[%d].%d', $server->worker_pid, $workerId));
|
||||||
}
|
}
|
||||||
$this->eventDispatch->dispatch(new OnAfterWorkerStart());
|
$this->eventDispatch->dispatch(new OnAfterWorkerStart());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param OnBeforeWorkerStart $worker
|
* @param OnBeforeWorkerStart $worker
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function setConfigure(OnBeforeWorkerStart $worker)
|
public function setConfigure(OnBeforeWorkerStart $worker)
|
||||||
{
|
{
|
||||||
ServerManager::setEnv('worker', $worker->workerId);
|
ServerManager::setEnv('worker', $worker->workerId);
|
||||||
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
|
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
|
||||||
if (!empty($serialize)) {
|
if (!empty($serialize)) {
|
||||||
Config::sets(unserialize($serialize));
|
Config::sets(unserialize($serialize));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $workerId
|
* @param int $workerId
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onWorkerStop(Server $server, int $workerId)
|
public function onWorkerStop(Server $server, int $workerId)
|
||||||
{
|
{
|
||||||
$this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId));
|
$this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId));
|
||||||
|
|
||||||
Timer::clearAll();
|
Timer::clearAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $workerId
|
* @param int $workerId
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onWorkerExit(Server $server, int $workerId)
|
public function onWorkerExit(Server $server, int $workerId)
|
||||||
{
|
{
|
||||||
$this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId));
|
$this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId));
|
||||||
|
|
||||||
ServerManager::setEnv('state', 'exit');
|
ServerManager::setEnv('state', 'exit');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $worker_id
|
* @param int $worker_id
|
||||||
* @param int $worker_pid
|
* @param int $worker_pid
|
||||||
* @param int $exit_code
|
* @param int $exit_code
|
||||||
* @param int $signal
|
* @param int $signal
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal)
|
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal)
|
||||||
{
|
{
|
||||||
$this->eventDispatch->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal));
|
$this->eventDispatch->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',
|
$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)
|
$worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), 9)
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->logger->error($message);
|
$this->logger->error($message);
|
||||||
|
|
||||||
$this->system_mail($message);
|
$this->system_mail($message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $messageContent
|
* @param $messageContent
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
protected function system_mail($messageContent)
|
protected function system_mail($messageContent)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$email = Config::get('email');
|
$email = Config::get('email');
|
||||||
if (!empty($email) && ($email['enable'] ?? false) == true) {
|
if (!empty($email) && ($email['enable'] ?? false) == true) {
|
||||||
Help::sendEmail($email, 'Service Error', $messageContent);
|
Help::sendEmail($email, 'Service Error', $messageContent);
|
||||||
}
|
}
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
error($e, 'email');
|
error($e, 'email');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user