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

107 lines
2.5 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-04-09 02:29:40 +08:00
use Annotation\Annotation;
2020-08-31 01:27:08 +08:00
use Exception;
2020-09-04 01:05:33 +08:00
use HttpServer\Abstracts\Callback;
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-04-09 10:28:01 +08:00
use Swoole\Coroutine\System;
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-04-09 10:34:33 +08:00
2021-04-07 02:21:29 +08:00
/**
* @param Server $server
* @param int $worker_id
*
* @return mixed
* @throws Exception
*/
public function onHandler(Server $server, int $worker_id): void
{
2021-04-09 03:12:37 +08:00
putenv('state=start');
putenv('worker=' . $worker_id);
2021-04-09 10:32:36 +08:00
$content = System::readFile(storage('runtime.php'));
2021-04-09 10:28:01 +08:00
2021-04-09 03:19:03 +08:00
$annotation = Snowflake::app()->getAnnotation();
2021-04-09 10:28:01 +08:00
$annotation->setLoader(unserialize($content));
2021-04-09 03:14:08 +08:00
2021-04-09 03:19:03 +08:00
if ($worker_id < $server->setting['worker_num']) {
$this->onWorker($server, $annotation);
} else {
$this->onTask($server, $annotation);
}
2021-04-07 02:21:29 +08:00
}
2021-04-09 10:28:01 +08:00
/**
* @param Server $server
* @param int $worker_id
* @return bool
*/
private function isWorker(Server $server, int $worker_id): bool
2021-04-09 02:35:29 +08:00
{
2021-04-09 03:16:04 +08:00
return $worker_id < $server->setting['worker_num'];
2021-04-09 02:35:29 +08:00
}
2021-04-09 10:28:01 +08:00
/**
* @param Server $server
* @param Annotation $annotation
* @throws ConfigException
* @throws Exception
*/
2021-04-09 03:19:03 +08:00
public function onTask(Server $server, Annotation $annotation)
2021-04-07 02:21:29 +08:00
{
2021-04-09 03:19:03 +08:00
$annotation->runtime(MODEL_PATH);
2021-04-07 02:21:29 +08:00
putenv('environmental=' . Snowflake::TASK);
2021-04-09 03:37:08 +08:00
name($server->worker_pid, 'Task#' . $server->worker_id);
2021-04-07 02:21:29 +08:00
Snowflake::setTaskId($server->worker_pid);
fire(Event::SERVER_TASK_START);
}
2021-04-09 10:28:01 +08:00
/**
* @param Server $server
* @param Annotation $annotation
* @throws Exception
*/
2021-04-09 03:19:03 +08:00
public function onWorker(Server $server, Annotation $annotation)
2021-04-07 02:21:29 +08:00
{
try {
2021-04-09 03:26:50 +08:00
$time = microtime(true);
2021-04-09 03:19:03 +08:00
$annotation->runtime(CONTROLLER_PATH);
2021-04-09 03:26:50 +08:00
$this->debug('load controller time .' . (microtime(true) - $time));
2021-04-09 03:19:03 +08:00
$annotation->runtime(APP_PATH, CONTROLLER_PATH);
2021-04-09 03:37:08 +08:00
name($server->worker_pid, 'Worker#' . $server->worker_id);
2021-04-09 03:19:03 +08:00
Snowflake::setWorkerId($server->worker_pid);
putenv('environmental=' . Snowflake::WORKER);
2021-04-09 03:12:37 +08:00
fire(Event::SERVER_WORKER_START, [getenv('worker')]);
2021-04-07 02:21:29 +08:00
} catch (\Throwable $exception) {
$this->addError($exception, 'throwable');
write($exception->getMessage(), 'worker');
}
}
2021-02-27 04:48:44 +08:00
2020-08-31 01:27:08 +08:00
}