Files
kiri-core/HttpServer/Events/OnWorkerStart.php
T

135 lines
3.2 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
2020-09-02 11:38:47 +08:00
namespace HttpServer\Events;
2020-08-31 01:27:08 +08:00
2021-02-28 17:34:55 +08:00
use Annotation\Target;
2020-08-31 01:27:08 +08:00
use Exception;
2020-09-04 01:05:33 +08:00
use HttpServer\Abstracts\Callback;
2020-09-07 16:50:26 +08:00
use Snowflake\Abstracts\Config;
2020-08-31 01:27:08 +08:00
use Snowflake\Event;
2020-09-07 16:50:26 +08:00
use Snowflake\Exception\ConfigException;
2020-08-31 01:27:08 +08:00
use Snowflake\Snowflake;
2021-02-28 00:41:22 +08:00
use Swoole\Coroutine;
2021-02-28 17:15:06 +08:00
use Swoole\Process;
2020-08-31 01:27:08 +08:00
use Swoole\Server;
/**
* Class OnWorkerStart
2020-09-02 11:38:47 +08:00
* @package HttpServer\Events
2020-08-31 01:27:08 +08:00
*/
2021-02-28 17:34:55 +08:00
#[Target]
2020-08-31 01:27:08 +08:00
class OnWorkerStart extends Callback
{
2021-02-28 18:11:50 +08:00
/** @var int|string 重启信号 */
2021-02-28 17:35:14 +08:00
private int $signal = SIGUSR1 | SIGUSR2;
2021-02-27 15:27:22 +08:00
2021-02-28 18:11:50 +08:00
/** @var bool 是否打印 */
2021-02-28 18:11:02 +08:00
private bool $isPrint = false;
2021-02-27 02:40:22 +08:00
/**
* @param Server $server
* @param int $worker_id
*
* @return mixed
* @throws ConfigException
* @throws Exception
*/
public function onHandler(Server $server, int $worker_id): void
{
2021-02-28 17:15:06 +08:00
Coroutine::set(['enable_deadlock_check' => false]);
2021-02-28 17:20:59 +08:00
Snowflake::app()->stateInit();
2021-02-28 18:11:02 +08:00
$this->isPrint = false;
2021-02-28 18:03:43 +08:00
$this->debug(sprintf('System#%d start.', $worker_id));
2021-02-28 17:15:06 +08:00
if ($worker_id >= $server->setting['worker_num']) {
$this->onTask($server, $worker_id);
} else {
$this->onWorker($server, $worker_id);
2021-02-28 00:42:12 +08:00
}
2021-02-28 18:02:05 +08:00
Coroutine::create([$this, 'onSignal'], $server, $worker_id);
2021-02-28 17:15:06 +08:00
}
2021-02-28 00:42:12 +08:00
2021-02-27 15:27:22 +08:00
2021-02-28 17:15:06 +08:00
/**
* @param Server $server
* @param int $worker_id
* @throws \Snowflake\Exception\ComponentException
* OnTask Worker
*/
public function onTask(Server $server, int $worker_id)
{
putenv('environmental=' . Snowflake::TASK);
fire(Event::SERVER_TASK_START);
$this->set_process_name($server, $worker_id);
}
/**
* @param Server $server
* @param int $worker_id
* @throws Exception
* onWorker
*/
public function onWorker(Server $server, int $worker_id)
{
Snowflake::setWorkerId($server->worker_pid);
2021-02-27 15:27:22 +08:00
2021-02-28 17:15:06 +08:00
putenv('environmental=' . Snowflake::WORKER);
try {
fire(Event::SERVER_WORKER_START, [$worker_id]);
} catch (\Throwable $exception) {
$this->addError($exception);
write($exception->getMessage(), 'worker');
2021-02-27 04:48:44 +08:00
}
2021-02-28 17:15:06 +08:00
$this->set_process_name($server, $worker_id);
2021-02-27 04:48:44 +08:00
}
2021-02-28 17:15:06 +08:00
2021-02-27 04:48:44 +08:00
/**
* @param $server
* @param $worker_id
*/
2021-02-28 17:43:33 +08:00
public function onSignal($server, $worker_id)
2021-02-28 14:29:38 +08:00
{
2021-02-28 18:02:05 +08:00
$env = ucfirst(Snowflake::getEnvironmental());
while (Coroutine::waitSignal($this->signal, -1)) {
if ($this->isPrint === false) {
$this->warning(sprintf('Receive %s#%d stop event.', $env, $worker_id));
2021-02-28 18:11:02 +08:00
$this->isPrint = true;
2021-02-28 14:29:38 +08:00
}
2021-02-28 18:02:05 +08:00
if (!Snowflake::app()->isRun()) {
break;
}
sleep(1);
}
return $server->stop($worker_id);
2021-02-28 14:29:38 +08:00
}
2021-02-27 02:40:22 +08:00
/**
* @param $socket
* @param $worker_id
* @return string
* @throws ConfigException
*/
2021-02-28 17:15:46 +08:00
private function set_process_name($socket, $worker_id): mixed
2021-02-27 02:40:22 +08:00
{
2021-02-28 17:15:06 +08:00
$prefix = Config::get('id', false, 'system');
2021-02-27 02:40:22 +08:00
if ($worker_id >= $socket->setting['worker_num']) {
2021-02-28 17:15:06 +08:00
$name = $prefix . ' Task: No.' . $worker_id;
2021-02-27 02:40:22 +08:00
} else {
2021-02-28 17:15:06 +08:00
$name = $prefix . ' worker: No.' . $worker_id;
2021-02-27 02:40:22 +08:00
}
2021-02-28 17:15:06 +08:00
return swoole_set_process_name($name);
2021-02-27 02:40:22 +08:00
}
2020-08-31 01:27:08 +08:00
}