This commit is contained in:
2021-09-06 14:30:20 +08:00
parent 728f95492f
commit e5dc0b7836
4 changed files with 597 additions and 591 deletions
+7 -9
View File
@@ -28,10 +28,10 @@ use Server\ServerProviders;
use stdClass; use stdClass;
use Swoole\Process; use Swoole\Process;
use Swoole\Timer; use Swoole\Timer;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Application as ConsoleApplication;
/** /**
* Class Init * Class Init
@@ -68,7 +68,7 @@ class Application extends BaseApplication
/** /**
* @throws NotFindClassException * @throws
*/ */
public function withDatabase() public function withDatabase()
{ {
@@ -77,7 +77,7 @@ class Application extends BaseApplication
/** /**
* @throws NotFindClassException * @throws
*/ */
public function withCrontab() public function withCrontab()
{ {
@@ -118,9 +118,7 @@ class Application extends BaseApplication
/** /**
* @throws NotFindClassException * @throws
* @throws ReflectionException
* @throws Exception
*/ */
public function withFileChangeListen() public function withFileChangeListen()
{ {
@@ -163,7 +161,7 @@ class Application extends BaseApplication
public function import(string $service): static public function import(string $service): static
{ {
if (!class_exists($service)) { if (!class_exists($service)) {
throw new NotFindClassException($service); return $this;
} }
$class = Kiri::getDi()->get($service); $class = Kiri::getDi()->get($service);
if (method_exists($class, 'onImport')) { if (method_exists($class, 'onImport')) {
@@ -192,7 +190,7 @@ class Application extends BaseApplication
*/ */
public function register(string $command) public function register(string $command)
{ {
di(ConsoleApplication::class)->add(di($command)); di(ConsoleApplication::class)->add(di($command));
} }
@@ -239,7 +237,7 @@ class Application extends BaseApplication
*/ */
private function enableFileChange(Command $class, $input, $output): void private function enableFileChange(Command $class, $input, $output): void
{ {
fire(new OnBeforeCommandExecute()); fire(new OnBeforeCommandExecute());
if (!($class instanceof ServerCommand)) { if (!($class instanceof ServerCommand)) {
scan_directory(directory('app'), 'App'); scan_directory(directory('app'), 'App');
} }
File diff suppressed because it is too large Load Diff
+57 -59
View File
@@ -10,7 +10,6 @@ use Kiri\Abstracts\Config;
use Kiri\Di\NoteManager; use Kiri\Di\NoteManager;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Kiri\Kiri; use Kiri\Kiri;
use Kiri\Runtime;
use Psr\EventDispatcher\EventDispatcherInterface; use Psr\EventDispatcher\EventDispatcherInterface;
use ReflectionException; use ReflectionException;
use Server\ServerManager; use Server\ServerManager;
@@ -22,15 +21,15 @@ class OnWorkerStart implements EventDispatcherInterface
/** /**
* @var Annotation * @var Annotation
*/ */
#[Inject(Annotation::class)] #[Inject(Annotation::class)]
public Annotation $annotation; public Annotation $annotation;
/** /**
* @var Router * @var Router
*/ */
#[Inject(Router::class)] #[Inject(Router::class)]
public Router $router; public Router $router;
/** /**
@@ -40,68 +39,67 @@ class OnWorkerStart implements EventDispatcherInterface
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
public function dispatch(object $event) public function dispatch(object $event)
{ {
$isWorker = $event->workerId < $event->server->setting['worker_num']; $isWorker = $event->workerId < $event->server->setting['worker_num'];
$this->annotation->read(APP_PATH . 'app', 'App'); $this->interpretDirectory();
$this->interpretDirectory(); if ($isWorker) {
if ($isWorker) { ServerManager::setEnv('environmental', Kiri::WORKER);
ServerManager::setEnv('environmental', Kiri::WORKER); Kiri::getFactory()->getRouter()->_loader();
Kiri::getFactory()->getRouter()->_loader();
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; 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)); $this->setProcessName(sprintf('Worker[%d].%d', $event->server->worker_pid, $event->workerId));
} else { } else {
ServerManager::setEnv('environmental', Kiri::TASK); ServerManager::setEnv('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; 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)); $this->setProcessName(sprintf('Tasker[%d].%d', $event->server->worker_pid, $event->workerId));
} }
} }
/** /**
* @param $prefix * @param $prefix
* @throws ConfigException * @throws ConfigException
*/ */
protected function setProcessName($prefix) protected function setProcessName($prefix)
{ {
if (Kiri::getPlatform()->isMac()) { if (Kiri::getPlatform()->isMac()) {
return; return;
} }
$name = Config::get('id', 'system-service'); $name = Config::get('id', 'system-service');
if (!empty($prefix)) { if (!empty($prefix)) {
$name .= '.' . $prefix; $name .= '.' . $prefix;
} }
swoole_set_process_name($name); swoole_set_process_name($name);
} }
/** /**
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
private function interpretDirectory() private function interpretDirectory()
{ {
$fileLists = $this->annotation->runtime(APP_PATH . 'app'); $di = Kiri::getDi();
$di = Kiri::getDi(); $fileLists = $this->annotation->read(APP_PATH . 'app');
foreach ($fileLists as $class) { foreach ($fileLists->runtime(APP_PATH . 'app') as $class) {
foreach (NoteManager::getTargetNote($class) as $value) { foreach (NoteManager::getTargetNote($class) as $value) {
$value->execute($class); $value->execute($class);
} }
$methods = $di->getMethodAttribute($class); $methods = $di->getMethodAttribute($class);
foreach ($methods as $method => $attribute) { foreach ($methods as $method => $attribute) {
if (empty($attribute)) { if (empty($attribute)) {
continue; continue;
} }
foreach ($attribute as $item) { foreach ($attribute as $item) {
$item->execute($class, $method); $item->execute($class, $method);
} }
} }
} }
} }
} }
+3 -2
View File
@@ -61,12 +61,13 @@ class Annotation extends Component
* @param string $path * @param string $path
* @param string $namespace * @param string $namespace
* @param array $exclude * @param array $exclude
* @return void * @return static
* @throws Exception * @throws Exception
*/ */
public function read(string $path, string $namespace = 'App', array $exclude = []): void public function read(string $path, string $namespace = 'App', array $exclude = []): static
{ {
$this->_loader->_scanDir(new DirectoryIterator($path), $namespace, $exclude); $this->_loader->_scanDir(new DirectoryIterator($path), $namespace, $exclude);
return $this;
} }