modify
This commit is contained in:
@@ -10,10 +10,7 @@ use RdKafka\TopicConf;
|
||||
use ReflectionException;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
|
||||
@@ -448,27 +448,27 @@ abstract class BaseApplication extends Service
|
||||
protected function moreComponents(): void
|
||||
{
|
||||
$this->setComponents([
|
||||
'error' => ['class' => ErrorHandler::class],
|
||||
'event' => ['class' => Event::class],
|
||||
'connections' => ['class' => Connection::class],
|
||||
'redis_connections' => ['class' => SRedis::class],
|
||||
'pool' => ['class' => SPool::class],
|
||||
'response' => ['class' => Response::class],
|
||||
'request' => ['class' => Request::class],
|
||||
'config' => ['class' => Config::class],
|
||||
'logger' => ['class' => Logger::class],
|
||||
'attributes' => ['class' => SAnnotation::class],
|
||||
'router' => ['class' => Router::class],
|
||||
'redis' => ['class' => Redis::class],
|
||||
'jwt' => ['class' => Jwt::class],
|
||||
'async' => ['class' => Async::class],
|
||||
'filter' => ['class' => HttpFilter::class],
|
||||
'crontab' => ['class' => Crontab::class],
|
||||
'object' => ['class' => ObjectPool::class],
|
||||
'goto' => ['class' => BaseGoto::class],
|
||||
'rpc' => ['class' => \Rpc\Producer::class],
|
||||
'rpc-service' => ['class' => \Rpc\Service::class],
|
||||
'http2' => ['class' => Http2::class],
|
||||
'error' => ['class' => ErrorHandler::class],
|
||||
'event' => ['class' => Event::class],
|
||||
'connections' => ['class' => Connection::class],
|
||||
'redis_connections' => ['class' => SRedis::class],
|
||||
'pool' => ['class' => SPool::class],
|
||||
'response' => ['class' => Response::class],
|
||||
'request' => ['class' => Request::class],
|
||||
'config' => ['class' => Config::class],
|
||||
'logger' => ['class' => Logger::class],
|
||||
'attributes' => ['class' => SAnnotation::class],
|
||||
'router' => ['class' => Router::class],
|
||||
'redis' => ['class' => Redis::class],
|
||||
'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],
|
||||
'rpc-service' => ['class' => \Rpc\Service::class],
|
||||
'http2' => ['class' => Http2::class],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ use Rpc\Producer as RPCProducer;
|
||||
* @property Producer $kafka
|
||||
* @property Client $client
|
||||
* @property Curl $curl
|
||||
* @property Crontab $crontab
|
||||
* @property \Snowflake\Crontab\Producer $crontab
|
||||
* @property HttpFilter $filter
|
||||
* @property RPCProducer $rpc
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Crontab;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Process\Process;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Coroutine;
|
||||
|
||||
|
||||
/**
|
||||
* Class Consumer
|
||||
* @package Snowflake\Crontab
|
||||
*/
|
||||
class Consumer extends Process
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\Process $process
|
||||
*/
|
||||
public function onHandler(\Swoole\Process $process): void
|
||||
{
|
||||
// TODO: Implement onHandler() method.
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
|
||||
$process->name('Crontab consumer');
|
||||
|
||||
while (true) {
|
||||
[$value, $startTime] = swoole_unserialize($process->read());
|
||||
|
||||
$crontab = $redis->get('crontab:' . md5($value));
|
||||
$redis->del('crontab:' . md5($value));
|
||||
if (empty($crontab) || !($crontab = swoole_unserialize($crontab))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Coroutine::create(function (Crontab $value, int $startTime) {
|
||||
$this->dispatch($value);
|
||||
}, $crontab, $startTime);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Crontab $value
|
||||
* @throws Exception
|
||||
*/
|
||||
private function dispatch(Crontab $value)
|
||||
{
|
||||
$value->increment()->execute();
|
||||
if ($value->getExecuteNumber() < $value->getMaxExecuteNumber()) {
|
||||
$this->addTask($value);
|
||||
} else if ($value->isLoop()) {
|
||||
$this->addTask($value);
|
||||
}
|
||||
fire(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Crontab $crontab
|
||||
* @throws Exception
|
||||
*/
|
||||
private function addTask(Crontab $crontab)
|
||||
{
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
|
||||
$name = md5($crontab->getName());
|
||||
|
||||
$redis->set('crontab:' . $name, swoole_serialize($crontab));
|
||||
|
||||
$tickTime = time() + $crontab->getTickTime();
|
||||
|
||||
$redis->zAdd(Producer::CRONTAB_KEY, $tickTime, $crontab->getName());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake;
|
||||
namespace Snowflake\Crontab;
|
||||
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Snowflake\Abstracts\BaseObject;
|
||||
use Snowflake\Process\CrontabProcess;
|
||||
use Snowflake\Event;
|
||||
use Swoole\Timer;
|
||||
|
||||
/**
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Crontab;
|
||||
|
||||
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Process\Process;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Coroutine\WaitGroup;
|
||||
use Swoole\Coroutine\Channel;
|
||||
use Swoole\Timer;
|
||||
|
||||
/**
|
||||
* Class CrontabZookeeperProcess
|
||||
* @package Snowflake\Process
|
||||
*/
|
||||
class CrontabZookeeperProcess extends Process
|
||||
{
|
||||
|
||||
|
||||
private Channel $channel;
|
||||
private WaitGroup $waitGroup;
|
||||
|
||||
|
||||
/** @var Crontab[] $names */
|
||||
public array $names = [];
|
||||
|
||||
|
||||
public array $scores = [];
|
||||
public array $timers = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\Process $process
|
||||
* @throws ReflectionException
|
||||
* @throws ComponentException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function onHandler(\Swoole\Process $process): void
|
||||
{
|
||||
$crontab = Snowflake::app()->get('crontab');
|
||||
$crontab->clearAll();
|
||||
|
||||
$process->name('Crontab zookeeper.');
|
||||
Timer::tick(1000, function () {
|
||||
$startTime = time();
|
||||
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
|
||||
$range = $redis->zRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime);
|
||||
$redis->zRemRangeByScore(Producer::CRONTAB_KEY, '0', (string)$startTime);
|
||||
|
||||
/** @var Consumer $consumer */
|
||||
$consumer = Snowflake::app()->get(Consumer::class);
|
||||
|
||||
foreach ($range as $value) {
|
||||
$consumer->write(swoole_serialize(['crontab:' . md5($value), $startTime]));
|
||||
}
|
||||
$redis->release();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*/
|
||||
public function clear(string $name)
|
||||
{
|
||||
if (!isset($this->names[$name])) {
|
||||
return;
|
||||
}
|
||||
$timers = $this->timers[$name];
|
||||
|
||||
$search = array_search($name, $this->scores[$timers]);
|
||||
if ($search !== false) {
|
||||
unset($this->scores[$timers][$search]);
|
||||
}
|
||||
unset($this->timers[$name], $this->names[$name]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,31 +1,30 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Abstracts;
|
||||
namespace Snowflake\Crontab;
|
||||
|
||||
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Process\CrontabProcess;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Snowflake;
|
||||
use Exception;
|
||||
|
||||
|
||||
/**
|
||||
* Class Crontab
|
||||
* Class Producer
|
||||
* @package Snowflake\Abstracts
|
||||
*/
|
||||
class Crontab extends Component
|
||||
class Producer extends Component
|
||||
{
|
||||
|
||||
const CRONTAB_KEY = 'system:crontab';
|
||||
|
||||
|
||||
/**
|
||||
* @param \Snowflake\Crontab $crontab
|
||||
* @param Crontab $crontab
|
||||
* @param $executeTime
|
||||
* @throws Exception
|
||||
*/
|
||||
public function dispatch(\Snowflake\Crontab $crontab)
|
||||
public function dispatch(Crontab $crontab)
|
||||
{
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
|
||||
@@ -1,125 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Process;
|
||||
|
||||
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Snowflake\Crontab;
|
||||
use Snowflake\Abstracts\Crontab as ACrontab;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Coroutine\WaitGroup;
|
||||
use Swoole\Coroutine\Channel;
|
||||
use Swoole\Timer;
|
||||
|
||||
/**
|
||||
* Class CrontabProcess
|
||||
* @package Snowflake\Process
|
||||
*/
|
||||
class CrontabProcess extends Process
|
||||
{
|
||||
|
||||
|
||||
private Channel $channel;
|
||||
private WaitGroup $waitGroup;
|
||||
|
||||
|
||||
/** @var Crontab[] $names */
|
||||
public array $names = [];
|
||||
|
||||
|
||||
public array $scores = [];
|
||||
public array $timers = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\Process $process
|
||||
* @throws ReflectionException
|
||||
* @throws ComponentException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function onHandler(\Swoole\Process $process): void
|
||||
{
|
||||
$crontab = Snowflake::app()->get('crontab');
|
||||
$crontab->clearAll();
|
||||
|
||||
Timer::tick(1000, function () {
|
||||
$startTime = time();
|
||||
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
|
||||
$range = $redis->zRangeByScore(ACrontab::CRONTAB_KEY, '0', (string)$startTime);
|
||||
$redis->zRemRangeByScore(ACrontab::CRONTAB_KEY, '0', (string)$startTime);
|
||||
foreach ($range as $value) {
|
||||
$crontab = $redis->get('crontab:' . md5($value));
|
||||
$redis->del('crontab:' . md5($value));
|
||||
if (empty($crontab) || !($crontab = swoole_unserialize($crontab))) {
|
||||
continue;
|
||||
}
|
||||
Coroutine::create(function (Crontab $value, int $startTime) {
|
||||
$this->dispatch($value);
|
||||
}, $crontab, $startTime);
|
||||
}
|
||||
$redis->release();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Crontab $value
|
||||
* @throws Exception
|
||||
*/
|
||||
private function dispatch(Crontab $value)
|
||||
{
|
||||
$value->increment()->execute();
|
||||
if ($value->getExecuteNumber() < $value->getMaxExecuteNumber()) {
|
||||
$this->addTask($value);
|
||||
} else if ($value->isLoop()) {
|
||||
$this->addTask($value);
|
||||
}
|
||||
fire(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*/
|
||||
public function clear(string $name)
|
||||
{
|
||||
if (!isset($this->names[$name])) {
|
||||
return;
|
||||
}
|
||||
$timers = $this->timers[$name];
|
||||
|
||||
$search = array_search($name, $this->scores[$timers]);
|
||||
if ($search !== false) {
|
||||
unset($this->scores[$timers][$search]);
|
||||
}
|
||||
unset($this->timers[$name], $this->names[$name]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Crontab $crontab
|
||||
* @throws Exception
|
||||
*/
|
||||
private function addTask(Crontab $crontab)
|
||||
{
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
|
||||
$name = md5($crontab->getName());
|
||||
|
||||
$redis->set('crontab:' . $name, swoole_serialize($crontab));
|
||||
|
||||
$tickTime = time() + $crontab->getTickTime();
|
||||
|
||||
$redis->zAdd(ACrontab::CRONTAB_KEY, $tickTime, $crontab->getName());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user