改名
This commit is contained in:
@@ -23,67 +23,69 @@ use Swoole\Server;
|
|||||||
class OnWorkerStart extends Callback
|
class OnWorkerStart extends Callback
|
||||||
{
|
{
|
||||||
|
|
||||||
/** @var int 重启信号 */
|
/** @var int 重启信号 */
|
||||||
private int $signal = SIGUSR1 | SIGUSR2 | SIGTERM;
|
private int $signal = SIGUSR1 | SIGUSR2 | SIGTERM;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @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]);
|
||||||
|
|
||||||
$this->debug(sprintf('System#%d start.', $worker_id));
|
putenv('worker=' . $worker_id);
|
||||||
if ($worker_id >= $server->setting['worker_num']) {
|
|
||||||
$this->onTask($server, $worker_id);
|
$this->debug(sprintf('System#%d start.', $worker_id));
|
||||||
} else {
|
if ($worker_id >= $server->setting['worker_num']) {
|
||||||
$this->onWorker($server, $worker_id);
|
$this->onTask($server, $worker_id);
|
||||||
}
|
} else {
|
||||||
Coroutine\go([$this, 'onSignal'], $server, $worker_id);
|
$this->onWorker($server, $worker_id);
|
||||||
}
|
}
|
||||||
|
Coroutine\go([$this, 'onSignal'], $server, $worker_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $worker_id
|
* @param int $worker_id
|
||||||
* @throws ComponentException|ConfigException
|
* @throws ComponentException|ConfigException
|
||||||
* OnTask Worker
|
* OnTask Worker
|
||||||
*/
|
*/
|
||||||
public function onTask(Server $server, int $worker_id)
|
public function onTask(Server $server, int $worker_id)
|
||||||
{
|
{
|
||||||
putenv('environmental=' . Snowflake::TASK);
|
putenv('environmental=' . Snowflake::TASK);
|
||||||
|
|
||||||
fire(Event::SERVER_TASK_START);
|
fire(Event::SERVER_TASK_START);
|
||||||
|
|
||||||
$this->set_process_name($server, $worker_id);
|
$this->set_process_name($server, $worker_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $worker_id
|
* @param int $worker_id
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
* onWorker
|
* onWorker
|
||||||
*/
|
*/
|
||||||
public function onWorker(Server $server, int $worker_id)
|
public function onWorker(Server $server, int $worker_id)
|
||||||
{
|
{
|
||||||
Snowflake::setWorkerId($server->worker_pid);
|
Snowflake::setWorkerId($server->worker_pid);
|
||||||
|
|
||||||
putenv('environmental=' . Snowflake::WORKER);
|
putenv('environmental=' . Snowflake::WORKER);
|
||||||
try {
|
try {
|
||||||
fire(Event::SERVER_WORKER_START, [$worker_id]);
|
fire(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');
|
||||||
}
|
}
|
||||||
$this->set_process_name($server, $worker_id);
|
$this->set_process_name($server, $worker_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,35 +93,38 @@ class OnWorkerStart extends Callback
|
|||||||
* @param $worker_id
|
* @param $worker_id
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function onSignal($server, $worker_id): mixed
|
public function onSignal($server, $worker_id): mixed
|
||||||
{
|
{
|
||||||
$receive = Coroutine::waitSignal($this->signal, -1);
|
$receive = Coroutine::waitSignal($this->signal, -1);
|
||||||
while ($receive === true) {
|
while ($receive === true) {
|
||||||
if (!Snowflake::app()->isRun()) {
|
|
||||||
break;
|
var_dump(env('worker') . '::' . (string)Snowflake::app()->isRun());
|
||||||
}
|
|
||||||
sleep(1);
|
if (!Snowflake::app()->isRun()) {
|
||||||
}
|
break;
|
||||||
return $server->stop($worker_id);
|
}
|
||||||
}
|
sleep(1);
|
||||||
|
}
|
||||||
|
return $server->stop($worker_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $socket
|
* @param $socket
|
||||||
* @param $worker_id
|
* @param $worker_id
|
||||||
* @return string
|
* @return string
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
*/
|
*/
|
||||||
private function set_process_name($socket, $worker_id): mixed
|
private function set_process_name($socket, $worker_id): mixed
|
||||||
{
|
{
|
||||||
$prefix = Config::get('id', false, 'system');
|
$prefix = Config::get('id', false, 'system');
|
||||||
if ($worker_id >= $socket->setting['worker_num']) {
|
if ($worker_id >= $socket->setting['worker_num']) {
|
||||||
$name = $prefix . ' Task: No.' . $worker_id;
|
$name = $prefix . ' Task: No.' . $worker_id;
|
||||||
} else {
|
} else {
|
||||||
$name = $prefix . ' worker: No.' . $worker_id;
|
$name = $prefix . ' worker: No.' . $worker_id;
|
||||||
}
|
}
|
||||||
return swoole_set_process_name($name);
|
return swoole_set_process_name($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user