111
This commit is contained in:
@@ -6,8 +6,11 @@ namespace Http;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Input;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Server\Worker\OnWorkerStart as WorkerDispatch;
|
||||
use Server\Events\OnWorkerStart;
|
||||
|
||||
/**
|
||||
* Class Command
|
||||
@@ -58,11 +61,15 @@ class Command extends \Console\Command
|
||||
private function generate_runtime_builder($manager)
|
||||
{
|
||||
exec(PHP_BINARY . ' ' . APP_PATH . 'kiri.php runtime:builder');
|
||||
if (is_enable_file_modification_listening()) {
|
||||
if (!is_enable_file_modification_listening()) {
|
||||
scan_directory(directory('app'), 'App');
|
||||
$loader = Kiri::app()->getRouter();
|
||||
$loader->_loader();
|
||||
}
|
||||
|
||||
$event = di(EventProvider::class);
|
||||
$event->on(OnWorkerStart::class, [di(WorkerDispatch::class), 'dispatch']);
|
||||
|
||||
return $manager->start();
|
||||
}
|
||||
|
||||
|
||||
@@ -11,13 +11,13 @@ class OnWorkerStart
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $workerId
|
||||
*/
|
||||
public function __construct(Server $server, int $workerId)
|
||||
{
|
||||
}
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $workerId
|
||||
*/
|
||||
public function __construct(public Server $server, public int $workerId)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ class OnServerWorker extends \Server\Abstracts\Server
|
||||
{
|
||||
$this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
|
||||
|
||||
$this->onWorkerStartInit($server, $workerId);
|
||||
// $this->onWorkerStartInit($server, $workerId);
|
||||
|
||||
$this->eventDispatch->dispatch(new OnAfterWorkerStart());
|
||||
}
|
||||
@@ -68,14 +68,14 @@ class OnServerWorker extends \Server\Abstracts\Server
|
||||
if (!empty($serialize)) {
|
||||
Config::sets(unserialize($serialize));
|
||||
}
|
||||
$this->workerInitExecutor($server, $workerId);
|
||||
if (is_enable_file_modification_listening()) {
|
||||
$annotation = Kiri::app()->getAnnotation();
|
||||
$annotation->read(APP_PATH . 'app', 'App',
|
||||
$workerId < $server->setting['worker_num'] ? [] : [CONTROLLER_PATH]
|
||||
);
|
||||
$this->interpretDirectory($annotation);
|
||||
}
|
||||
$this->interpretDirectory($annotation);
|
||||
$this->workerInitExecutor($server, $workerId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Worker;
|
||||
|
||||
use Annotation\Annotation;
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Kiri\Runtime;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use ReflectionException;
|
||||
use Server\Events\OnWorkerStart as WorkerStart;
|
||||
|
||||
class OnWorkerStart implements EventDispatcherInterface
|
||||
{
|
||||
|
||||
|
||||
public Annotation $annotation;
|
||||
|
||||
|
||||
/**
|
||||
* @param \Server\Events\OnWorkerStart $onWorkerStart
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->annotation = Kiri::app()->getAnnotation();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param object $event
|
||||
* @return object|void
|
||||
* @throws \Kiri\Exception\ConfigException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function dispatch(object $event)
|
||||
{
|
||||
putenv('state=start');
|
||||
putenv('worker=' . $event->workerId);
|
||||
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
|
||||
if (!empty($serialize)) {
|
||||
Config::sets(unserialize($serialize));
|
||||
}
|
||||
if ($event->workerId < $event->server->setting['worker_num']) {
|
||||
$this->onWorkerInit($event);
|
||||
} else {
|
||||
$this->onTaskInit($event);
|
||||
}
|
||||
$this->interpretDirectory();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $event
|
||||
* @throws \Kiri\Exception\ConfigException
|
||||
*/
|
||||
public function onTaskInit($event)
|
||||
{
|
||||
$this->annotation->read(APP_PATH . 'app', 'App', [CONTROLLER_PATH]);
|
||||
|
||||
putenv('environmental=' . Kiri::TASK);
|
||||
|
||||
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Tasker[%d].%d start.", $event->server->worker_pid, $event->workerId) . PHP_EOL;
|
||||
|
||||
$this->setProcessName(sprintf('Tasker[%d].%d', $event->server->worker_pid, $event->workerId));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $event
|
||||
* @throws \Kiri\Exception\ConfigException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function onWorkerInit($event)
|
||||
{
|
||||
$this->annotation->read(APP_PATH . 'app');
|
||||
putenv('environmental=' . Kiri::WORKER);
|
||||
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $event->server->worker_pid, $event->workerId) . PHP_EOL;
|
||||
$this->setProcessName(sprintf('Worker[%d].%d', $event->server->worker_pid, $event->workerId));
|
||||
if (is_enable_file_modification_listening()) {
|
||||
$loader = Kiri::app()->getRouter();
|
||||
$loader->_loader();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $prefix
|
||||
* @throws ConfigException
|
||||
*/
|
||||
protected function setProcessName($prefix)
|
||||
{
|
||||
if (Kiri::getPlatform()->isMac()) {
|
||||
return;
|
||||
}
|
||||
$name = Config::get('id', 'system-service');
|
||||
if (!empty($prefix)) {
|
||||
$name .= '.' . $prefix;
|
||||
}
|
||||
swoole_set_process_name($name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function interpretDirectory()
|
||||
{
|
||||
$fileLists = $this->annotation->runtime(APP_PATH . 'app');
|
||||
$di = Kiri::getDi();
|
||||
foreach ($fileLists as $class) {
|
||||
foreach ($di->getTargetNote($class) as $value) {
|
||||
$value->execute($class);
|
||||
}
|
||||
$methods = $di->getMethodAttribute($class);
|
||||
foreach ($methods as $method => $attribute) {
|
||||
if (empty($attribute)) {
|
||||
continue;
|
||||
}
|
||||
foreach ($attribute as $item) {
|
||||
$item->execute($class, $method);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user