modify
This commit is contained in:
@@ -3,6 +3,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace HttpServer\Events;
|
namespace HttpServer\Events;
|
||||||
|
|
||||||
|
use Annotation\Annotation;
|
||||||
use Annotation\Loader;
|
use Annotation\Loader;
|
||||||
use Annotation\Target;
|
use Annotation\Target;
|
||||||
use Exception;
|
use Exception;
|
||||||
@@ -25,6 +26,42 @@ class OnWorkerStart extends Callback
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $server
|
||||||
|
* @param $worker_id
|
||||||
|
* @throws ConfigException
|
||||||
|
*/
|
||||||
|
public function actionBefore($server, $worker_id)
|
||||||
|
{
|
||||||
|
putenv('state=start');
|
||||||
|
putenv('worker=' . $worker_id);
|
||||||
|
|
||||||
|
$alias = $worker_id >= $server->setting['worker_num'] ? 'task' : 'worker';
|
||||||
|
|
||||||
|
name($server->worker_pid, $alias . '.' . $worker_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Annotation
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function injectLoader($isWorker = false): Annotation
|
||||||
|
{
|
||||||
|
$runtime = file_get_contents(storage('runtime.php'));
|
||||||
|
$annotation = Snowflake::app()->getAnnotation();
|
||||||
|
$annotation->setLoader(unserialize($runtime));
|
||||||
|
|
||||||
|
if ($isWorker === true) {
|
||||||
|
$annotation->runtime(CONTROLLER_PATH);
|
||||||
|
$annotation->runtime(APP_PATH, CONTROLLER_PATH);
|
||||||
|
} else {
|
||||||
|
$annotation->runtime(MODEL_PATH);
|
||||||
|
}
|
||||||
|
return $annotation;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $worker_id
|
* @param int $worker_id
|
||||||
@@ -34,29 +71,16 @@ class OnWorkerStart extends Callback
|
|||||||
*/
|
*/
|
||||||
public function onHandler(Server $server, int $worker_id): void
|
public function onHandler(Server $server, int $worker_id): void
|
||||||
{
|
{
|
||||||
putenv('state=start');
|
$isWorker = $worker_id < $server->setting['worker_num'];
|
||||||
putenv('worker=' . $worker_id);
|
|
||||||
|
|
||||||
name($server->worker_pid, $worker_id >= $server->setting['worker_num'] ? 'task' : 'worker');
|
$this->injectLoader($isWorker);
|
||||||
|
|
||||||
$annotation = Snowflake::app()->getAnnotation();
|
$this->actionBefore($server, $worker_id);
|
||||||
|
|
||||||
/** @var Loader $runtime */
|
if ($worker_id < $server->setting['worker_num']) {
|
||||||
$runtime = unserialize(file_get_contents(storage('runtime.php')));
|
|
||||||
$annotation->setLoader($runtime);
|
|
||||||
|
|
||||||
if ($worker_id >= $server->setting['worker_num']) {
|
|
||||||
$annotation->runtime(MODEL_PATH);
|
|
||||||
|
|
||||||
$this->onTask($server, $worker_id);
|
|
||||||
} else {
|
|
||||||
$start = microtime(true);
|
|
||||||
$annotation->runtime(CONTROLLER_PATH);
|
|
||||||
$this->error('use time ' . (microtime(true) - $start));
|
|
||||||
Coroutine\go(function () use ($annotation) {
|
|
||||||
$annotation->runtime(APP_PATH, CONTROLLER_PATH);
|
|
||||||
});
|
|
||||||
$this->onWorker($server, $worker_id);
|
$this->onWorker($server, $worker_id);
|
||||||
|
} else {
|
||||||
|
$this->onTask($server, $worker_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user