diff --git a/HttpServer/Server.php b/HttpServer/Server.php index 930e5bb7..2fb859f5 100644 --- a/HttpServer/Server.php +++ b/HttpServer/Server.php @@ -23,7 +23,7 @@ use Rpc\Service; use Snowflake\Abstracts\Config; use Snowflake\Core\Json; use Snowflake\Crontab\Consumer; -use Snowflake\Crontab\CrontabZookeeperProcess; +use Snowflake\Crontab\ZookeeperProcess; use Snowflake\Error\LoggerProcess; use Snowflake\Event; use Snowflake\Exception\ComponentException; @@ -133,10 +133,7 @@ class Server extends HttpService public function start(): string { $configs = Config::get('servers', true); - if (Config::get('crontab.enable') === true) { - $this->addProcess('CrontabZookeeper', CrontabZookeeperProcess::class); - $this->addProcess('CrontabConsumer', Consumer::class); - } + $baseServer = $this->initCore($configs); if (!$baseServer) { return 'ok'; diff --git a/System/Abstracts/BaseApplication.php b/System/Abstracts/BaseApplication.php index 28a04e6e..91b7f9b8 100644 --- a/System/Abstracts/BaseApplication.php +++ b/System/Abstracts/BaseApplication.php @@ -463,7 +463,6 @@ abstract class BaseApplication extends Service 'jwt' => ['class' => Jwt::class], 'async' => ['class' => Async::class], 'filter' => ['class' => HttpFilter::class], - 'crontab' => ['class' => \Snowflake\Crontab\Producer::class], 'object' => ['class' => ObjectPool::class], 'goto' => ['class' => BaseGoto::class], 'rpc' => ['class' => \Rpc\Producer::class], diff --git a/System/Application.php b/System/Application.php index 6b8484d7..990e57be 100644 --- a/System/Application.php +++ b/System/Application.php @@ -20,6 +20,7 @@ use Snowflake\Abstracts\BaseApplication; use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Input; use Snowflake\Abstracts\Kernel; +use Snowflake\Crontab\CrontabProviders; use Snowflake\Exception\ComponentException; use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindPropertyException; @@ -37,102 +38,106 @@ use function Co\run; class Application extends BaseApplication { - /** - * @var string - */ - public string $id = 'uniqueId'; + /** + * @var string + */ + public string $id = 'uniqueId'; - public string $state = ''; + public string $state = ''; - /** - * @throws NotFindClassException - */ - public function init() - { - $this->import(ConsoleProviders::class); - $this->import(DatabasesProviders::class); - $this->import(ServerProviders::class); - } + /** + * @throws NotFindClassException + */ + public function init() + { + $this->import(ConsoleProviders::class); + $this->import(DatabasesProviders::class); + $this->import(ServerProviders::class); + + if (Config::get('crontab.enable') !== true) { + $this->import(CrontabProviders::class); + } + } - /** - * @param string $service - * @return $this - * @throws - */ - public function import(string $service): static - { - if (!class_exists($service)) { - throw new NotFindClassException($service); - } - $class = Snowflake::createObject($service); - if (method_exists($class, 'onImport')) { - $class->onImport($this); - } - return $this; - } + /** + * @param string $service + * @return $this + * @throws + */ + public function import(string $service): static + { + if (!class_exists($service)) { + throw new NotFindClassException($service); + } + $class = Snowflake::createObject($service); + if (method_exists($class, 'onImport')) { + $class->onImport($this); + } + return $this; + } - /** - * @param $kernel - * @return $this - */ - public function commands(Kernel $kernel): static - { - foreach ($kernel->getCommands() as $command) { - $this->register($command); - } - return $this; - } + /** + * @param $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 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 start(Input $argv): void - { - try { - fire(Event::SERVER_BEFORE_START); + /** + * @param Input $argv + * @return void + * @throws Exception + */ + public function start(Input $argv): void + { + try { + fire(Event::SERVER_BEFORE_START); - $manager = Snowflake::app()->get('console'); - $manager->setParameters($argv); - $class = $manager->search(); - response()->send($manager->execCommand($class)); - } catch (\Throwable $exception) { - response()->send(implode("\n", [ - 'Msg: ' . $exception->getMessage(), - 'Line: ' . $exception->getLine(), - 'File: ' . $exception->getFile() - ])); - } finally { - Timer::clearAll(); - } - } + $manager = Snowflake::app()->get('console'); + $manager->setParameters($argv); + $class = $manager->search(); + response()->send($manager->execCommand($class)); + } catch (\Throwable $exception) { + response()->send(implode("\n", [ + 'Msg: ' . $exception->getMessage(), + 'Line: ' . $exception->getLine(), + 'File: ' . $exception->getFile() + ])); + } finally { + Timer::clearAll(); + } + } - /** - * @param $className - * @param null $abstracts - * @return stdClass - * @throws Exception - */ - public function make($className, $abstracts = null): stdClass - { - return make($className, $abstracts); - } + /** + * @param $className + * @param null $abstracts + * @return stdClass + * @throws Exception + */ + public function make($className, $abstracts = null): stdClass + { + return make($className, $abstracts); + } } diff --git a/System/Crontab/CrontabProviders.php b/System/Crontab/CrontabProviders.php new file mode 100644 index 00000000..32450c8c --- /dev/null +++ b/System/Crontab/CrontabProviders.php @@ -0,0 +1,35 @@ +getServer(); + if (Config::get('crontab.enable') !== true) { + return; + } + $application->set('crontab', ['class' => Producer::class]); + + $server->addProcess('CrontabZookeeper', ZookeeperProcess::class); + $server->addProcess('Consumer', Consumer::class); + } + +} diff --git a/System/Crontab/CrontabZookeeperProcess.php b/System/Crontab/ZookeeperProcess.php similarity index 96% rename from System/Crontab/CrontabZookeeperProcess.php rename to System/Crontab/ZookeeperProcess.php index 7e0a5c5b..b2e747f3 100644 --- a/System/Crontab/CrontabZookeeperProcess.php +++ b/System/Crontab/ZookeeperProcess.php @@ -17,10 +17,10 @@ use Swoole\Coroutine\Channel; use Swoole\Timer; /** - * Class CrontabZookeeperProcess + * Class ZookeeperProcess * @package Snowflake\Process */ -class CrontabZookeeperProcess extends Process +class ZookeeperProcess extends Process {