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

124 lines
2.8 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 00:41:22 +08:00
use co;
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
*/
class OnWorkerStart extends Callback
{
2021-02-27 02:40:22 +08:00
private int $_taskTable = 0;
2021-02-27 15:27:22 +08:00
private int $signal = SIGTERM | SIGKILL | SIGUSR2 | SIGUSR1;
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]);
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 17:15:06 +08:00
$this->onSignal();
}
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:04:27 +08:00
public function onSignal()
2021-02-28 14:29:38 +08:00
{
2021-02-28 17:04:27 +08:00
Coroutine\go(function () {
2021-02-28 14:30:11 +08:00
if (!Coroutine::waitSignal($this->signal)) {
return 0;
}
2021-02-28 14:29:38 +08:00
while (Snowflake::app()->isRun()) {
Coroutine::sleep(1);
}
2021-02-28 17:04:27 +08:00
return Snowflake::app()->getSwoole()->stop();
});
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
}