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

114 lines
2.6 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;
2021-03-01 10:26:53 +08:00
use Snowflake\Exception\ComponentException;
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;
2020-08-31 01:27:08 +08:00
use Swoole\Server;
2021-03-02 18:16:34 +08:00
use Swoole\Timer;
2020-08-31 01:27:08 +08:00
/**
* 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-03-02 15:46:10 +08:00
2021-03-01 16:07:44 +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-03-01 19:16:41 +08:00
putenv('worker=' . $worker_id);
2021-03-02 18:51:45 +08:00
putenv('state=start');
2021-03-02 13:52:28 +08:00
2021-03-04 10:38:06 +08:00
$start = microtime(true);
2021-03-04 10:36:08 +08:00
annotation()->read(APP_PATH . 'app', 'App');
2021-03-04 10:38:06 +08:00
$this->debug(sprintf('scan app dir use time %s', microtime(true) - $start));
2021-03-04 10:36:08 +08:00
2021-03-01 16:07:44 +08:00
if ($worker_id >= $server->setting['worker_num']) {
$this->onTask($server, $worker_id);
} else {
$this->onWorker($server, $worker_id);
}
2021-03-01 19:01:57 +08:00
$this->debug(sprintf('%s #%d Pid:%d start.', ucfirst(env('environmental')), $worker_id, $server->worker_pid));
2021-03-01 16:07:44 +08:00
}
/**
* @param Server $server
* @param int $worker_id
* @throws ComponentException|ConfigException
*/
public function onTask(Server $server, int $worker_id)
{
putenv('environmental=' . Snowflake::TASK);
2021-03-04 10:38:06 +08:00
$start = microtime(true);
2021-03-01 16:07:44 +08:00
fire(Event::SERVER_TASK_START);
2021-03-04 10:38:06 +08:00
$this->debug(sprintf('Event::SERVER_WORKER_START use time %s', microtime(true) - $start));
2021-03-01 16:07:44 +08:00
$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);
putenv('environmental=' . Snowflake::WORKER);
try {
2021-03-04 10:38:06 +08:00
$start = microtime(true);
2021-03-01 16:07:44 +08:00
fire(Event::SERVER_WORKER_START, [$worker_id]);
2021-03-04 10:38:06 +08:00
$this->debug(sprintf('Event::SERVER_WORKER_START use time %s', microtime(true) - $start));
2021-03-01 16:07:44 +08:00
} catch (\Throwable $exception) {
$this->addError($exception);
write($exception->getMessage(), 'worker');
}
$this->set_process_name($server, $worker_id);
}
2021-02-27 04:48:44 +08:00
2021-02-28 17:15:06 +08:00
2021-03-01 16:07:44 +08:00
/**
* @param $socket
* @param $worker_id
* @return string
* @throws ConfigException
*/
private function set_process_name($socket, $worker_id): mixed
{
$prefix = Config::get('id', false, 'system');
if ($worker_id >= $socket->setting['worker_num']) {
$name = $prefix . ' Task: No.' . $worker_id;
} else {
$name = $prefix . ' worker: No.' . $worker_id;
}
2021-03-03 19:04:14 +08:00
if (Snowflake::isMac()) {
return 1;
}
2021-03-01 16:07:44 +08:00
return swoole_set_process_name($name);
}
2020-08-31 01:27:08 +08:00
}