This commit is contained in:
as2252258@163.com
2021-08-29 00:58:54 +08:00
parent 300ebfec7d
commit e3f02b51bc
4 changed files with 1056 additions and 1015 deletions
+197 -200
View File
@@ -42,221 +42,218 @@ use Swoole\Timer;
class Application extends BaseApplication class Application extends BaseApplication
{ {
/** /**
* @var string * @var string
*/ */
public string $id = 'uniqueId'; public string $id = 'uniqueId';
public string $state = ''; public string $state = '';
/** @var array<array<Process>> */ /** @var array<array<Process>> */
private array $_process = []; private array $_process = [];
/** /**
* @throws NotFindClassException * @throws NotFindClassException
*/ */
public function init() public function init()
{ {
$this->import(ConsoleProviders::class); $this->import(ConsoleProviders::class);
$this->import(ServerProviders::class); $this->import(ServerProviders::class);
} }
/** /**
* @throws NotFindClassException * @throws NotFindClassException
*/ */
public function withDatabase() public function withDatabase()
{ {
$this->import(DatabasesProviders::class); $this->import(DatabasesProviders::class);
} }
/** /**
* @throws NotFindClassException * @throws NotFindClassException
*/ */
public function withCrontab() public function withCrontab()
{ {
$this->import(CrontabProviders::class); $this->import(CrontabProviders::class);
} }
/** /**
* @param string $class * @param string $class
* @param Process $process * @param Process $process
*/ */
public function addProcess(string $class, Process $process) public function addProcess(string $class, Process $process)
{ {
if (!isset($this->_process[$class])) { if (!isset($this->_process[$class])) {
$this->_process[$class] = []; $this->_process[$class] = [];
}
$this->_process[$class][] = $process;
}
/**
* @return Process[]
*/
public function getProcess(): array
{
return $this->_process;
}
/**
* @param string $class
* @return Process|null
*/
public function getProcessName(string $class): ?Process
{
return $this->_process[$class] ?? null;
}
/**
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public function withFileChangeListen()
{
$manager = $this->getServer();
$manager->addProcess(FileChangeCustomProcess::class);
putenv('enableFileChange=on');
}
/**
* @param Closure|array $closure
* @return $this
* @throws Exception
*/
public function middleware(Closure|array $closure): static
{
$this->getRouter()->setMiddleware($closure);
return $this;
}
/**
* @param bool $useTree
* @return $this
* @throws Exception
*/
public function setUseTree(bool $useTree): static
{
$this->getRouter()->setUseTree($useTree);
return $this;
}
/**
* @param string $service
* @return $this
* @throws
*/
public function import(string $service): static
{
if (!class_exists($service)) {
throw new NotFindClassException($service);
}
$class = Kiri::getDi()->get($service);
if (method_exists($class, 'onImport')) {
$class->onImport($this);
}
return $this;
}
/**
* @param Kernel $kernel
* @return $this
*/
public function commands(Kernel $kernel): static
{
foreach ($kernel->getCommands() as $command) {
$this->register($command);
}
return $this;
}
/**
* @param string $command
* @throws
*/
public function register(string $command)
{
/** @var Console $abstracts */
$abstracts = $this->get('console');
$abstracts->register($command);
}
/**
* @param Input $argv
* @return void
* @throws Exception
*/
public function execute(Input $argv): void
{
try {
$this->register(Runtime::class);
$manager = Kiri::app()->get('console');
$class = $manager->setParameters($argv)->search();
$this->enableFileChange($class);
$data = $this->getBuilder($manager->exec($class));
} catch (\Throwable $exception) {
$data = $this->getBuilder(jTraceEx($exception));
} finally {
print_r($data);
Timer::clearAll();
}
}
/**
* @throws NotFindClassException
* @throws ReflectionException
*/
private function enableFileChange($class): void
{
if (env('enableFileChange', 'off') == 'off' || !($class instanceof Command)) {
scan_directory(directory('app'), 'App');
$loader = Kiri::app()->getRouter();
$loader->_loader();
} }
} $this->_process[$class][] = $process;
}
/** /**
* @param $data * @return Process[]
* @return Response|ResponseInterface */
* @throws NotFindClassException public function getProcess(): array
* @throws ReflectionException {
* @throws Exception return $this->_process;
*/ }
private function getBuilder($data): Response|ResponseInterface
{
return di(Response::class)->getBuilder($data);
}
/** /**
* @param $className * @param string $class
* @param null $abstracts * @return Process|null
* @return stdClass */
* @throws Exception public function getProcessName(string $class): ?Process
*/ {
public function make($className, $abstracts = null): stdClass return $this->_process[$class] ?? null;
{ }
return make($className, $abstracts);
}
/**
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public function withFileChangeListen()
{
$manager = $this->getServer();
$manager->addProcess(FileChangeCustomProcess::class);
enable_file_modification_listening();
}
/**
* @param Closure|array $closure
* @return $this
* @throws Exception
*/
public function middleware(Closure|array $closure): static
{
$this->getRouter()->setMiddleware($closure);
return $this;
}
/**
* @param bool $useTree
* @return $this
* @throws Exception
*/
public function setUseTree(bool $useTree): static
{
$this->getRouter()->setUseTree($useTree);
return $this;
}
/**
* @param string $service
* @return $this
* @throws
*/
public function import(string $service): static
{
if (!class_exists($service)) {
throw new NotFindClassException($service);
}
$class = Kiri::getDi()->get($service);
if (method_exists($class, 'onImport')) {
$class->onImport($this);
}
return $this;
}
/**
* @param Kernel $kernel
* @return $this
*/
public function commands(Kernel $kernel): static
{
foreach ($kernel->getCommands() as $command) {
$this->register($command);
}
return $this;
}
/**
* @param string $command
* @throws
*/
public function register(string $command)
{
/** @var Console $abstracts */
$abstracts = $this->get('console');
$abstracts->register($command);
}
/**
* @param Input $argv
* @return void
* @throws Exception
*/
public function execute(Input $argv): void
{
try {
$this->register(Runtime::class);
$manager = Kiri::app()->get('console');
$class = $manager->setParameters($argv)->search();
$this->enableFileChange($class);
$data = $this->getBuilder($manager->exec($class));
} catch (\Throwable $exception) {
$data = $this->getBuilder(jTraceEx($exception));
} finally {
print_r($data);
Timer::clearAll();
}
}
/**
* @throws NotFindClassException
* @throws ReflectionException
*/
private function enableFileChange($class): void
{
if (!($class instanceof Command)) {
scan_directory(directory('app'), 'App');
}
}
/**
* @param $data
* @return Response|ResponseInterface
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
private function getBuilder($data): Response|ResponseInterface
{
return di(Response::class)->getBuilder($data);
}
/**
* @param $className
* @param null $abstracts
* @return stdClass
* @throws Exception
*/
public function make($className, $abstracts = null): stdClass
{
return make($className, $abstracts);
}
} }
+812 -774
View File
File diff suppressed because it is too large Load Diff
+41 -35
View File
@@ -16,48 +16,54 @@ use Kiri\Kiri;
class Command extends \Console\Command class Command extends \Console\Command
{ {
public string $command = 'sw:server'; public string $command = 'sw:server';
public string $description = 'server start|stop|reload|restart'; public string $description = 'server start|stop|reload|restart';
const ACTIONS = ['start', 'stop', 'restart']; const ACTIONS = ['start', 'stop', 'restart'];
/** /**
* @param Input $dtl * @param Input $dtl
* @return string * @return string
* @throws Exception * @throws Exception
* @throws ConfigException * @throws ConfigException
*/ */
public function onHandler(Input $dtl): string public function onHandler(Input $dtl): string
{ {
$manager = Kiri::app()->getServer(); $manager = Kiri::app()->getServer();
$manager->setDaemon($dtl->get('daemon', 0)); $manager->setDaemon($dtl->get('daemon', 0));
if (!in_array($dtl->get('action'), self::ACTIONS)) { if (!in_array($dtl->get('action'), self::ACTIONS)) {
return 'I don\'t know what I want to do.'; return 'I don\'t know what I want to do.';
} }
if ($manager->isRunner() && $dtl->get('action') == 'start') { if ($manager->isRunner() && $dtl->get('action') == 'start') {
return 'Service is running. Please use restart.'; return 'Service is running. Please use restart.';
} }
$manager->shutdown();
$manager->shutdown(); if ($dtl->get('action') == 'stop') {
if ($dtl->get('action') == 'stop') { return 'shutdown success.';
return 'shutdown success.'; }
} return $this->generate_runtime_builder($manager);
}
$this->generate_runtime_builder();
return $manager->start();
}
/** /**
* * @param $manager
*/ * @return mixed
private function generate_runtime_builder() * @throws \Kiri\Exception\NotFindClassException
{ * @throws \ReflectionException
exec(PHP_BINARY . ' ' . APP_PATH . 'snowflake runtime:builder'); */
} private function generate_runtime_builder($manager)
{
exec(PHP_BINARY . ' ' . APP_PATH . 'kiri.php runtime:builder');
if (is_enable_file_modification_listening()) {
scan_directory(directory('app'), 'App');
$loader = Kiri::app()->getRouter();
$loader->_loader();
}
return $manager->start();
}
} }
+6 -6
View File
@@ -69,7 +69,7 @@ class OnServerWorker extends \Server\Abstracts\Server
Config::sets(unserialize($serialize)); Config::sets(unserialize($serialize));
} }
$this->workerInitExecutor($server, $workerId); $this->workerInitExecutor($server, $workerId);
if (env('enableFileChange', 'off') == 'off') { if (is_enable_file_modification_listening()) {
$annotation = Kiri::app()->getAnnotation(); $annotation = Kiri::app()->getAnnotation();
$annotation->read(APP_PATH . 'app', 'App', $annotation->read(APP_PATH . 'app', 'App',
$workerId < $server->setting['worker_num'] ? [] : [CONTROLLER_PATH] $workerId < $server->setting['worker_num'] ? [] : [CONTROLLER_PATH]
@@ -115,15 +115,15 @@ class OnServerWorker extends \Server\Abstracts\Server
private function workerInitExecutor(Server $server, int $workerId) private function workerInitExecutor(Server $server, int $workerId)
{ {
if ($workerId < $server->setting['worker_num']) { if ($workerId < $server->setting['worker_num']) {
if (env('enableFileChange', 'off') == 'off') {
$loader = Kiri::app()->getRouter();
$loader->_loader();
}
putenv('environmental=' . Kiri::WORKER); putenv('environmental=' . Kiri::WORKER);
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL; echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL;
$this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId)); $this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId));
if (is_enable_file_modification_listening()) {
$loader = Kiri::app()->getRouter();
$loader->_loader();
}
} else { } else {
putenv('environmental=' . Kiri::TASK); putenv('environmental=' . Kiri::TASK);