This commit is contained in:
as2252258@163.com
2021-03-26 02:12:34 +08:00
parent 451491e928
commit 9c557aac19
5 changed files with 128 additions and 92 deletions
+2 -5
View File
@@ -23,7 +23,7 @@ use Rpc\Service;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
use Snowflake\Core\Json; use Snowflake\Core\Json;
use Snowflake\Crontab\Consumer; use Snowflake\Crontab\Consumer;
use Snowflake\Crontab\CrontabZookeeperProcess; use Snowflake\Crontab\ZookeeperProcess;
use Snowflake\Error\LoggerProcess; use Snowflake\Error\LoggerProcess;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
@@ -133,10 +133,7 @@ class Server extends HttpService
public function start(): string public function start(): string
{ {
$configs = Config::get('servers', true); $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); $baseServer = $this->initCore($configs);
if (!$baseServer) { if (!$baseServer) {
return 'ok'; return 'ok';
-1
View File
@@ -463,7 +463,6 @@ abstract class BaseApplication extends Service
'jwt' => ['class' => Jwt::class], 'jwt' => ['class' => Jwt::class],
'async' => ['class' => Async::class], 'async' => ['class' => Async::class],
'filter' => ['class' => HttpFilter::class], 'filter' => ['class' => HttpFilter::class],
'crontab' => ['class' => \Snowflake\Crontab\Producer::class],
'object' => ['class' => ObjectPool::class], 'object' => ['class' => ObjectPool::class],
'goto' => ['class' => BaseGoto::class], 'goto' => ['class' => BaseGoto::class],
'rpc' => ['class' => \Rpc\Producer::class], 'rpc' => ['class' => \Rpc\Producer::class],
+89 -84
View File
@@ -20,6 +20,7 @@ use Snowflake\Abstracts\BaseApplication;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
use Snowflake\Abstracts\Input; use Snowflake\Abstracts\Input;
use Snowflake\Abstracts\Kernel; use Snowflake\Abstracts\Kernel;
use Snowflake\Crontab\CrontabProviders;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Exception\NotFindPropertyException; use Snowflake\Exception\NotFindPropertyException;
@@ -37,102 +38,106 @@ use function Co\run;
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 = '';
/** /**
* @throws NotFindClassException * @throws NotFindClassException
*/ */
public function init() public function init()
{ {
$this->import(ConsoleProviders::class); $this->import(ConsoleProviders::class);
$this->import(DatabasesProviders::class); $this->import(DatabasesProviders::class);
$this->import(ServerProviders::class); $this->import(ServerProviders::class);
}
if (Config::get('crontab.enable') !== true) {
$this->import(CrontabProviders::class);
}
}
/** /**
* @param string $service * @param string $service
* @return $this * @return $this
* @throws * @throws
*/ */
public function import(string $service): static public function import(string $service): static
{ {
if (!class_exists($service)) { if (!class_exists($service)) {
throw new NotFindClassException($service); throw new NotFindClassException($service);
} }
$class = Snowflake::createObject($service); $class = Snowflake::createObject($service);
if (method_exists($class, 'onImport')) { if (method_exists($class, 'onImport')) {
$class->onImport($this); $class->onImport($this);
} }
return $this; return $this;
} }
/** /**
* @param $kernel * @param $kernel
* @return $this * @return $this
*/ */
public function commands(Kernel $kernel): static public function commands(Kernel $kernel): static
{ {
foreach ($kernel->getCommands() as $command) { foreach ($kernel->getCommands() as $command) {
$this->register($command); $this->register($command);
} }
return $this; return $this;
} }
/** /**
* @param string $command * @param string $command
* @throws * @throws
*/ */
public function register(string $command) public function register(string $command)
{ {
/** @var Console $abstracts */ /** @var Console $abstracts */
$abstracts = $this->get('console'); $abstracts = $this->get('console');
$abstracts->register($command); $abstracts->register($command);
} }
/** /**
* @param Input $argv * @param Input $argv
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function start(Input $argv): void public function start(Input $argv): void
{ {
try { try {
fire(Event::SERVER_BEFORE_START); fire(Event::SERVER_BEFORE_START);
$manager = Snowflake::app()->get('console'); $manager = Snowflake::app()->get('console');
$manager->setParameters($argv); $manager->setParameters($argv);
$class = $manager->search(); $class = $manager->search();
response()->send($manager->execCommand($class)); response()->send($manager->execCommand($class));
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
response()->send(implode("\n", [ response()->send(implode("\n", [
'Msg: ' . $exception->getMessage(), 'Msg: ' . $exception->getMessage(),
'Line: ' . $exception->getLine(), 'Line: ' . $exception->getLine(),
'File: ' . $exception->getFile() 'File: ' . $exception->getFile()
])); ]));
} finally { } finally {
Timer::clearAll(); Timer::clearAll();
} }
} }
/** /**
* @param $className * @param $className
* @param null $abstracts * @param null $abstracts
* @return stdClass * @return stdClass
* @throws Exception * @throws Exception
*/ */
public function make($className, $abstracts = null): stdClass public function make($className, $abstracts = null): stdClass
{ {
return make($className, $abstracts); return make($className, $abstracts);
} }
} }
+35
View File
@@ -0,0 +1,35 @@
<?php
namespace Snowflake\Crontab;
use Snowflake\Abstracts\Config;
use Snowflake\Abstracts\Providers;
use Snowflake\Application;
/**
* Class CrontabProviders
* @package Snowflake\Crontab
*/
class CrontabProviders extends Providers
{
/**
* @param Application $application
*/
public function onImport(Application $application)
{
$server = $application->getServer();
if (Config::get('crontab.enable') !== true) {
return;
}
$application->set('crontab', ['class' => Producer::class]);
$server->addProcess('CrontabZookeeper', ZookeeperProcess::class);
$server->addProcess('Consumer', Consumer::class);
}
}
@@ -17,10 +17,10 @@ use Swoole\Coroutine\Channel;
use Swoole\Timer; use Swoole\Timer;
/** /**
* Class CrontabZookeeperProcess * Class ZookeeperProcess
* @package Snowflake\Process * @package Snowflake\Process
*/ */
class CrontabZookeeperProcess extends Process class ZookeeperProcess extends Process
{ {