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

88 lines
1.9 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
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;
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
{
/**
* @param Server $server
* @param int $worker_id
*
2020-12-15 14:04:02 +08:00
* @return mixed
* @throws ConfigException
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2020-12-15 14:04:02 +08:00
public function onHandler(Server $server, int $worker_id): void
2020-08-31 01:27:08 +08:00
{
$get_name = $this->get_process_name($server, $worker_id);
if (!empty($get_name) && !Snowflake::isMac()) {
swoole_set_process_name($get_name);
}
2021-01-04 18:45:08 +08:00
2020-09-01 13:03:12 +08:00
if ($worker_id >= $server->setting['worker_num']) {
2021-01-04 17:16:54 +08:00
fire(Event::SERVER_TASK_START);
2021-01-05 10:15:33 +08:00
putenv('workerId=Task.' . $worker_id);
2020-09-01 13:03:12 +08:00
return;
}
2021-01-05 10:15:33 +08:00
putenv('workerId=Worker.' . $worker_id);
2020-10-14 11:23:01 +08:00
Snowflake::setWorkerId($server->worker_pid);
2020-09-11 14:27:09 +08:00
$this->setWorkerAction($worker_id);
2020-08-31 01:27:08 +08:00
}
/**
* @param $worker_id
* @throws Exception
*/
2020-09-11 14:27:09 +08:00
private function setWorkerAction($worker_id)
2020-08-31 01:27:08 +08:00
{
2021-02-22 18:04:32 +08:00
$event = Snowflake::app()->getEvent();
2020-08-31 01:27:08 +08:00
try {
2020-09-02 12:21:11 +08:00
$this->debug(sprintf('Worker #%d is start.....', $worker_id));
2020-09-11 14:27:09 +08:00
$event->trigger(Event::SERVER_WORKER_START, [$worker_id]);
2021-02-22 18:04:32 +08:00
} catch (\Throwable $exception) {
$this->addError($exception);
write($exception->getMessage(), 'worker');
}
try {
2021-02-22 17:44:24 +08:00
$event->trigger(Event::SERVER_AFTER_WORKER_START, [$worker_id]);
2020-08-31 01:27:08 +08:00
} catch (\Throwable $exception) {
2021-02-22 18:04:32 +08:00
$this->addError($exception);
2020-09-16 20:23:23 +08:00
write($exception->getMessage(), 'worker');
2020-08-31 01:27:08 +08:00
}
}
/**
* @param $socket
* @param $worker_id
* @return string
2020-09-07 16:50:26 +08:00
* @throws ConfigException
2020-08-31 01:27:08 +08:00
*/
2020-12-15 14:04:02 +08:00
private function get_process_name($socket, $worker_id): string
2020-08-31 01:27:08 +08:00
{
2021-01-04 18:45:08 +08:00
$prefix = rtrim(Config::get('id', false, 'system:'), ':');
2020-08-31 01:27:08 +08:00
if ($worker_id >= $socket->setting['worker_num']) {
return $prefix . ': Task: No.' . $worker_id;
} else {
return $prefix . ': worker: No.' . $worker_id;
}
}
}