modify
This commit is contained in:
@@ -22,96 +22,93 @@ class OnWorkerStart extends Callback
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
private int $_taskTable = 0;
|
private int $_taskTable = 0;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $worker_id
|
* @param int $worker_id
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onHandler(Server $server, int $worker_id): void
|
public function onHandler(Server $server, int $worker_id): void
|
||||||
{
|
{
|
||||||
Coroutine::set(['enable_deadlock_check' => false]);
|
Coroutine::set(['enable_deadlock_check' => false]);
|
||||||
|
|
||||||
$get_name = $this->get_process_name($server, $worker_id);
|
$get_name = $this->get_process_name($server, $worker_id);
|
||||||
if (!empty($get_name) && !Snowflake::isMac()) {
|
if (!empty($get_name) && !Snowflake::isMac()) {
|
||||||
swoole_set_process_name($get_name);
|
swoole_set_process_name($get_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
putenv('workerId=' . ($worker_id >= $server->setting['worker_num'] ? 'Task' : 'Worker') . '.' . $worker_id);
|
putenv('workerId=' . ($worker_id >= $server->setting['worker_num'] ? 'Task' : 'Worker') . '.' . $worker_id);
|
||||||
if ($worker_id >= $server->setting['worker_num']) {
|
if ($worker_id >= $server->setting['worker_num']) {
|
||||||
fire(Event::SERVER_TASK_START);
|
fire(Event::SERVER_TASK_START);
|
||||||
} else {
|
} else {
|
||||||
Snowflake::setWorkerId($server->worker_pid);
|
Snowflake::setWorkerId($server->worker_pid);
|
||||||
$this->setWorkerAction($worker_id);
|
$this->setWorkerAction($worker_id);
|
||||||
}
|
}
|
||||||
$this->onTaskSignal($server, $worker_id);
|
$this->onTaskSignal($server, $worker_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $workerId
|
* @param int $workerId
|
||||||
* 异步任务管制
|
* 异步任务管制
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function onTaskSignal(Server $server, int $workerId): mixed
|
public function onTaskSignal(Server $server, int $workerId): mixed
|
||||||
{
|
{
|
||||||
$sigkill = Coroutine::waitSignal(SIGTERM | SIGKILL | SIGUSR2 | SIGUSR1, -1);
|
$sigkill = Coroutine::waitSignal(SIGTERM | SIGKILL | SIGUSR2 | SIGUSR1, -1);
|
||||||
Coroutine\defer(function () {
|
if ($sigkill !== false) {
|
||||||
Snowflake::app()->stateInit();
|
while (Snowflake::app()->isRun()) {
|
||||||
});
|
sleep(1);
|
||||||
if ($sigkill === false) {
|
}
|
||||||
return $server->stop($workerId);
|
}
|
||||||
}
|
Snowflake::app()->stateInit();
|
||||||
while (Snowflake::app()->isRun()) {
|
return $server->stop($workerId);
|
||||||
sleep(1);
|
}
|
||||||
}
|
|
||||||
return $server->stop($workerId);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $worker_id
|
* @param $worker_id
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function setWorkerAction($worker_id)
|
private function setWorkerAction($worker_id)
|
||||||
{
|
{
|
||||||
$event = Snowflake::app()->getEvent();
|
$event = Snowflake::app()->getEvent();
|
||||||
try {
|
try {
|
||||||
$this->debug(sprintf('Worker #%d is start.....', $worker_id));
|
$this->debug(sprintf('Worker #%d is start.....', $worker_id));
|
||||||
$event->trigger(Event::SERVER_WORKER_START, [$worker_id]);
|
$event->trigger(Event::SERVER_WORKER_START, [$worker_id]);
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
$this->addError($exception);
|
$this->addError($exception);
|
||||||
write($exception->getMessage(), 'worker');
|
write($exception->getMessage(), 'worker');
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$event->trigger(Event::SERVER_AFTER_WORKER_START, [$worker_id]);
|
$event->trigger(Event::SERVER_AFTER_WORKER_START, [$worker_id]);
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
$this->addError($exception);
|
$this->addError($exception);
|
||||||
write($exception->getMessage(), 'worker');
|
write($exception->getMessage(), 'worker');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $socket
|
* @param $socket
|
||||||
* @param $worker_id
|
* @param $worker_id
|
||||||
* @return string
|
* @return string
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
*/
|
*/
|
||||||
private function get_process_name($socket, $worker_id): string
|
private function get_process_name($socket, $worker_id): string
|
||||||
{
|
{
|
||||||
$prefix = rtrim(Config::get('id', false, 'system:'), ':');
|
$prefix = rtrim(Config::get('id', false, 'system:'), ':');
|
||||||
if ($worker_id >= $socket->setting['worker_num']) {
|
if ($worker_id >= $socket->setting['worker_num']) {
|
||||||
return $prefix . ': Task: No.' . $worker_id;
|
return $prefix . ': Task: No.' . $worker_id;
|
||||||
} else {
|
} else {
|
||||||
return $prefix . ': worker: No.' . $worker_id;
|
return $prefix . ': worker: No.' . $worker_id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user