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

101 lines
2.4 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-07 02:21:29 +08:00
use Annotation\Loader;
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;
2021-04-07 02:21:29 +08:00
use Snowflake\Process\ServerInotify;
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-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
{
putenv('state=start');
putenv('worker=' . $worker_id);
name($server->worker_pid, $worker_id >= $server->setting['worker_num'] ? 'task' : 'worker');
2021-04-08 00:07:18 +08:00
$annotation = Snowflake::app()->getAnnotation();
2021-04-07 23:53:48 +08:00
/** @var Loader $runtime */
$runtime = unserialize(file_get_contents(storage('runtime.php')));
2021-04-08 00:07:18 +08:00
$annotation->setLoader($runtime);
2021-04-08 01:47:28 +08:00
2021-04-07 02:21:29 +08:00
if ($worker_id >= $server->setting['worker_num']) {
2021-04-09 02:11:48 +08:00
$annotation->runtime(MODEL_PATH);
2021-04-07 23:53:48 +08:00
2021-04-07 02:31:43 +08:00
$this->onTask($server, $worker_id);
2021-04-07 02:21:29 +08:00
} else {
2021-04-08 00:38:29 +08:00
$start = microtime(true);
2021-04-09 02:20:25 +08:00
$annotation->runtime(CONTROLLER_PATH);
2021-04-08 00:38:29 +08:00
$this->error('use time ' . (microtime(true) - $start));
2021-04-09 02:20:25 +08:00
Coroutine\go(function () use ($annotation) {
$annotation->runtime(APP_PATH, CONTROLLER_PATH);
2021-04-08 01:37:57 +08:00
});
2021-04-07 23:53:48 +08:00
2021-04-07 02:31:43 +08:00
$this->onWorker($server, $worker_id);
2021-04-07 02:21:29 +08:00
}
}
/**
* @param Server $server
* @param int $worker_id
* @throws Exception
*/
2021-04-07 02:31:43 +08:00
public function onTask(Server $server, int $worker_id)
2021-04-07 02:21:29 +08:00
{
putenv('environmental=' . Snowflake::TASK);
Snowflake::setTaskId($server->worker_pid);
fire(Event::SERVER_TASK_START);
}
/**
* @param Server $server
* @param int $worker_id
* @throws Exception
*/
2021-04-07 02:31:43 +08:00
public function onWorker(Server $server, int $worker_id)
2021-04-07 02:21:29 +08:00
{
Snowflake::setWorkerId($server->worker_pid);
putenv('environmental=' . Snowflake::WORKER);
try {
fire(Event::SERVER_WORKER_START, [$worker_id]);
} catch (\Throwable $exception) {
$this->addError($exception, 'throwable');
write($exception->getMessage(), 'worker');
}
}
2021-02-27 04:48:44 +08:00
2021-02-28 17:15:06 +08:00
2020-08-31 01:27:08 +08:00
}