modify
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
namespace Snowflake\Abstracts;
|
||||
|
||||
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Process\CrontabProcess;
|
||||
use Snowflake\Snowflake;
|
||||
use Exception;
|
||||
|
||||
@@ -21,11 +23,11 @@ class Crontab extends Component
|
||||
* @param $executeTime
|
||||
* @throws Exception
|
||||
*/
|
||||
public function dispatch(\Snowflake\Crontab $crontab, int $executeTime)
|
||||
public function dispatch(\Snowflake\Crontab $crontab)
|
||||
{
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
|
||||
$redis->zAdd('system:crontab', (string)$executeTime, serialize($crontab));
|
||||
/** @var CrontabProcess $redis */
|
||||
$redis = Snowflake::app()->get(CrontabProcess::class);
|
||||
$redis->write(serialize($crontab));
|
||||
}
|
||||
|
||||
|
||||
@@ -35,20 +37,9 @@ class Crontab extends Component
|
||||
*/
|
||||
public function clear(string $name)
|
||||
{
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
|
||||
$data = $redis->zRevRange('system:crontab', 0, -1);
|
||||
if (empty($data)) {
|
||||
return;
|
||||
}
|
||||
foreach ($data as $datum) {
|
||||
/** @var \Snowflake\Crontab $crontab */
|
||||
$crontab = unserialize($datum);
|
||||
if ($crontab->getName() == $name) {
|
||||
$redis->zRem('system:crontab', $datum);
|
||||
}
|
||||
}
|
||||
|
||||
/** @var CrontabProcess $redis */
|
||||
$redis = Snowflake::app()->get(CrontabProcess::class);
|
||||
$redis->write(Json::encode(['action' => 'clear', 'name' => $name]));
|
||||
}
|
||||
|
||||
|
||||
@@ -57,9 +48,9 @@ class Crontab extends Component
|
||||
*/
|
||||
public function clearAll()
|
||||
{
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
|
||||
$redis->del('system:crontab');
|
||||
/** @var CrontabProcess $redis */
|
||||
$redis = Snowflake::app()->get(CrontabProcess::class);
|
||||
$redis->write(Json::encode(['action' => 'clearAll']));
|
||||
}
|
||||
|
||||
|
||||
|
||||
+37
-10
@@ -7,6 +7,7 @@ namespace Snowflake;
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Snowflake\Abstracts\BaseObject;
|
||||
use Swoole\Timer;
|
||||
|
||||
/**
|
||||
* Class Async
|
||||
@@ -31,6 +32,9 @@ class Crontab extends BaseObject
|
||||
private bool $isLoop = false;
|
||||
|
||||
|
||||
private int $timerId = -1;
|
||||
|
||||
|
||||
private int $max_execute_number = -1;
|
||||
|
||||
|
||||
@@ -149,28 +153,51 @@ class Crontab extends BaseObject
|
||||
$this->execute_number = $execute_number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getTimerId(): int
|
||||
{
|
||||
return $this->timerId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $timerId
|
||||
*/
|
||||
public function setTimerId(int $timerId): void
|
||||
{
|
||||
$this->timerId = $timerId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function clearTimer()
|
||||
{
|
||||
$this->warning('crontab timer clear.');
|
||||
if (Timer::exists($this->timerId)) {
|
||||
Timer::clear($this->timerId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function execute(): void
|
||||
{
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
try {
|
||||
$this->execute_number += 1;
|
||||
call_user_func($this->handler, $this->params);
|
||||
if ($this->isLoop === false) {
|
||||
return;
|
||||
}
|
||||
if ($this->max_execute_number === -1) {
|
||||
$redis->zAdd('system:crontab', time() + $this->tickTime, serialize($this));
|
||||
} else if ($this->execute_number < $this->max_execute_number) {
|
||||
$redis->zAdd('system:crontab', time() + $this->tickTime, serialize($this));
|
||||
$this->execute_number += 1;
|
||||
|
||||
if ($this->execute_number < $this->max_execute_number) {
|
||||
$this->clearTimer();
|
||||
}
|
||||
} catch (\Throwable $throwable) {
|
||||
$this->addError($throwable->getMessage());
|
||||
} finally {
|
||||
$redis->release();
|
||||
fire(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Snowflake\Process;
|
||||
|
||||
|
||||
use ReflectionException;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Crontab;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
@@ -23,67 +24,70 @@ use Swoole\Timer;
|
||||
class CrontabProcess extends Process
|
||||
{
|
||||
|
||||
|
||||
public Channel $channel;
|
||||
/** @var Crontab[] $names */
|
||||
public array $names = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\Process $process
|
||||
*/
|
||||
public function onHandler(\Swoole\Process $process): void
|
||||
{
|
||||
$this->channel = new Channel(5000);
|
||||
|
||||
Coroutine::create([$this, 'execute']);
|
||||
|
||||
Timer::tick(1000, [$this, 'systemLoop']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
while (true) {
|
||||
/** @var Crontab $list */
|
||||
$list = $this->channel->pop(-1);
|
||||
$list->execute();
|
||||
$content = $process->read();
|
||||
$_content = json_decode($content, true);
|
||||
if (is_null($_content)) {
|
||||
$this->jobDelivery($content);
|
||||
} else {
|
||||
$this->otherAction($content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
* @throws ComponentException
|
||||
* @throws ConfigException
|
||||
* @throws NotFindClassException
|
||||
* @throws Exception
|
||||
* @throws \Exception
|
||||
* @param $content
|
||||
*/
|
||||
public function systemLoop()
|
||||
private function otherAction($content)
|
||||
{
|
||||
$score = time();
|
||||
$redis = Snowflake::app()->getRedis();
|
||||
|
||||
$lists = $redis->zRangeByScore('system:crontab', '0', (string)$score);
|
||||
$redis->zRemRangeByScore('system:crontab', '0', (string)$score);
|
||||
|
||||
if (empty($lists)) {
|
||||
$redis->release();
|
||||
call_user_func(match ($content['action']) {
|
||||
'clear' => function ($content) {
|
||||
if (!isset($this->names[$content['name']])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$barrier = Barrier::make();
|
||||
foreach ($lists as $list) {
|
||||
$list = unserialize($list);
|
||||
if (!($list instanceof Crontab)) {
|
||||
continue;
|
||||
$this->names[$content['name']]->clearTimer();
|
||||
},
|
||||
'clearAll' => function () {
|
||||
foreach ($this->names as $name => $crontab) {
|
||||
$crontab->clearTimer();
|
||||
}
|
||||
$this->channel->push($list);
|
||||
},
|
||||
default => function () {
|
||||
$this->application->error('unknown action');
|
||||
}
|
||||
Barrier::wait($barrier);
|
||||
$redis->release();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $content
|
||||
*/
|
||||
private function jobDelivery($content)
|
||||
{
|
||||
$content = unserialize($content);
|
||||
$this->names[$content->getName()] = $content;
|
||||
if (!($content instanceof Crontab)) {
|
||||
return;
|
||||
}
|
||||
$runTicker = [$content, 'execute'];
|
||||
$timer = $content->getTickTime() * 1000;
|
||||
if ($content->isLoop()) {
|
||||
$timerId = Timer::tick($timer, $runTicker);
|
||||
} else {
|
||||
$timerId = Timer::after($timer, $runTicker);
|
||||
}
|
||||
$content->setTimerId($timerId);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user