This commit is contained in:
as2252258@163.com
2021-03-22 00:16:40 +08:00
parent 41a5386616
commit 10035f28e6
2 changed files with 42 additions and 92 deletions
+21 -9
View File
@@ -17,6 +17,8 @@ use Exception;
class Crontab extends Component class Crontab extends Component
{ {
const CRONTAB_KEY = 'system:crontab';
/** /**
* @param \Snowflake\Crontab $crontab * @param \Snowflake\Crontab $crontab
@@ -25,9 +27,15 @@ class Crontab extends Component
*/ */
public function dispatch(\Snowflake\Crontab $crontab) public function dispatch(\Snowflake\Crontab $crontab)
{ {
/** @var CrontabProcess $redis */ $redis = Snowflake::app()->getRedis();
$redis = Snowflake::app()->get(CrontabProcess::class);
$redis->write(serialize($crontab)); $name = md5($crontab->getName());
$redis->set('crontab:' . $name, serialize($crontab));
$tickTime = time() + $crontab->getTickTime() * 1000;
$redis->zAdd(self::CRONTAB_KEY, $tickTime, $crontab->getName());
} }
@@ -37,9 +45,10 @@ class Crontab extends Component
*/ */
public function clear(string $name) public function clear(string $name)
{ {
/** @var CrontabProcess $redis */ $redis = Snowflake::app()->getRedis();
$redis = Snowflake::app()->get(CrontabProcess::class);
$redis->write(Json::encode(['action' => 'clear', 'name' => $name])); $redis->zRem(self::CRONTAB_KEY, $name);
$redis->del('crontab:' . md5($name));
} }
@@ -48,9 +57,12 @@ class Crontab extends Component
*/ */
public function clearAll() public function clearAll()
{ {
/** @var CrontabProcess $redis */ $redis = Snowflake::app()->getRedis();
$redis = Snowflake::app()->get(CrontabProcess::class); $data = $redis->zRange(self::CRONTAB_KEY, 0, -1);
$redis->write(Json::encode(['action' => 'clearAll'])); $redis->del(self::CRONTAB_KEY);
foreach ($data as $datum) {
$redis->del('crontab:' . md5($datum));
}
} }
+21 -83
View File
@@ -5,6 +5,8 @@ namespace Snowflake\Process;
use Snowflake\Crontab; use Snowflake\Crontab;
use Snowflake\Abstracts\Crontab as ACrontab;
use Snowflake\Snowflake;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Coroutine\WaitGroup; use Swoole\Coroutine\WaitGroup;
use Swoole\Coroutine\Channel; use Swoole\Coroutine\Channel;
@@ -35,18 +37,21 @@ class CrontabProcess extends Process
*/ */
public function onHandler(\Swoole\Process $process): void public function onHandler(\Swoole\Process $process): void
{ {
$this->readBySocket($process);
Timer::tick(1000, function () { Timer::tick(1000, function () {
$startTime = time(); $startTime = time();
var_dump($startTime); $redis = Snowflake::app()->getRedis();
$time = $this->scores[$startTime]; $range = $redis->zRangeByScore(ACrontab::CRONTAB_KEY, 0, $startTime);
unset($this->scores[$startTime]); $redis->zRemRangeByScore(ACrontab::CRONTAB_KEY, 0, $startTime);
foreach ($time as $value) { foreach ($range as $value) {
$crontab = $redis->get('crontab:' . md5($value));
if (empty($crontab) || !($crontab = unserialize($crontab))) {
continue;
}
Coroutine::create(function (Crontab $value, int $startTime) { Coroutine::create(function (Crontab $value, int $startTime) {
$this->dispatch($value, $startTime); $this->dispatch($value);
}, $value, $startTime); }, $crontab, $startTime);
} }
}); });
} }
@@ -57,14 +62,14 @@ class CrontabProcess extends Process
* @param int $startTime * @param int $startTime
* @throws \Exception * @throws \Exception
*/ */
private function dispatch(Crontab $value, int $startTime) private function dispatch(Crontab $value)
{ {
try { try {
$value->increment()->execute(); $value->increment()->execute();
if ($value->getExecuteNumber() < $value->getMaxExecuteNumber()) { if ($value->getExecuteNumber() < $value->getMaxExecuteNumber()) {
$this->addTask($value, $startTime + $value->getTickTime()); $this->addTask($value);
} else if ($value->isLoop()) { } else if ($value->isLoop()) {
$this->addTask($value, $startTime + $value->getTickTime()); $this->addTask($value);
} }
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$this->application->error($exception->getMessage()); $this->application->error($exception->getMessage());
@@ -72,51 +77,6 @@ class CrontabProcess extends Process
} }
/**
* @param \Swoole\Process $process
* @throws \Exception
*/
public function readBySocket(\Swoole\Process $process)
{
Coroutine::create(function (\Swoole\Process $process) {
try {
$content = $process->read();
$_content = json_decode($content, true);
if (is_null($_content)) {
$this->jobDelivery($content);
} else {
$this->otherAction($_content);
}
} catch (\Throwable $exception) {
$this->application->error($exception->getMessage());
} finally {
$this->onHandler($process);
}
}, $process);
}
/**
* @param $content
*/
private function otherAction($content)
{
call_user_func(match ($content['action']) {
'clear' => function ($content) {
$this->clear($content['name']);
},
'clearAll' => function () {
$this->names = [];
Timer::clearAll();
},
default => function () {
$this->application->error('unknown action');
}
}, $content);
}
/** /**
* @param string $name * @param string $name
*/ */
@@ -135,43 +95,21 @@ class CrontabProcess extends Process
} }
/**
* @param $content
*/
private function jobDelivery($content)
{
/** @var Crontab $content */
$content = unserialize($content);
$ticker = intval($content->getTickTime() * 1000) + time();
$this->addTask($content, $ticker);
}
/** /**
* @param Crontab $content * @param Crontab $content
* @param $ticker * @param $ticker
*/ */
private function addTask(Crontab $content, $ticker) private function addTask(Crontab $crontab)
{ {
$name = $content->getName(); $redis = Snowflake::app()->getRedis();
if (isset($this->names[$name])) {
unset($this->names[$content->getName()]);
$search = array_search($content->getName(), $this->scores); $name = md5($crontab->getName());
unset($this->scores[$search]);
}
if (!isset($this->scores[$ticker])) { $redis->set('crontab:' . $name, serialize($crontab));
$this->scores[$ticker] = [];
}
$this->timers[$content->getName()] = $ticker; $tickTime = time() + $crontab->getTickTime();
$this->scores[$ticker][] = $content->getName();
$this->names[$content->getName()] = $content;
ksort($this->scores, SORT_NUMERIC); $redis->zAdd(ACrontab::CRONTAB_KEY, $tickTime, $crontab->getName());
} }