This commit is contained in:
as2252258@163.com
2021-04-16 00:35:55 +08:00
parent 331ba228a3
commit 2e78fc73c7
2 changed files with 48 additions and 49 deletions
+45 -47
View File
@@ -16,64 +16,62 @@ use Snowflake\Snowflake;
class Producer extends Component class Producer extends Component
{ {
const CRONTAB_KEY = '_application:system:crontab'; const CRONTAB_KEY = '_application:system:crontab';
/** /**
* @param Crontab $crontab * @param Crontab $crontab
* @throws Exception * @throws Exception
*/ */
public function dispatch(Crontab $crontab) public function dispatch(Crontab $crontab)
{ {
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
$name = $crontab->getName(); $name = $crontab->getName();
if ( if (
$redis->exists(self::CRONTAB_KEY) && $redis->exists(self::CRONTAB_KEY) &&
$redis->type(self::CRONTAB_KEY) !== \Redis::REDIS_ZSET) { $redis->type(self::CRONTAB_KEY) !== \Redis::REDIS_ZSET) {
throw new Exception('Cache key ' . self::CRONTAB_KEY . ' types error.'); throw new Exception('Cache key ' . self::CRONTAB_KEY . ' types error.');
} }
$tickTime = time() + $crontab->getTickTime(); $tickTime = time() + $crontab->getTickTime();
$redis->del('stop:crontab:' . $name, 120); $redis->del('stop:crontab:' . $name, 120);
var_dump($tickTime); $result = $redis->zAdd(self::CRONTAB_KEY . ':' . time(), $tickTime, $name);
if ($result) {
$result = $redis->zAdd(self::CRONTAB_KEY, $tickTime, $name); $redis->set('crontab:' . $name, swoole_serialize($crontab));
if ($result) { }
$redis->set('crontab:' . $name, swoole_serialize($crontab)); }
}
}
/** /**
* @param string $name * @param string $name
* @throws Exception * @throws Exception
*/ */
public function clear(string $name) public function clear(string $name)
{ {
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
$redis->setex('stop:crontab:' . md5($name), 120, 1); $redis->setex('stop:crontab:' . md5($name), 120, 1);
$redis->zRem(self::CRONTAB_KEY, $name); $redis->zRem(self::CRONTAB_KEY, $name);
$redis->del('crontab:' . md5($name)); $redis->del('crontab:' . md5($name));
} }
/** /**
* @throws Exception * @throws Exception
*/ */
public function clearAll() public function clearAll()
{ {
$redis = Snowflake::app()->getRedis(); $redis = Snowflake::app()->getRedis();
$data = $redis->zRange(self::CRONTAB_KEY, 0, -1); $data = $redis->zRange(self::CRONTAB_KEY, 0, -1);
foreach ($data as $datum) { foreach ($data as $datum) {
$redis->setex('stop:crontab:' . $datum, 120, 1); $redis->setex('stop:crontab:' . $datum, 120, 1);
$redis->del('crontab:' . $datum); $redis->del('crontab:' . $datum);
} }
$redis->release(); $redis->release();
} }
} }
+3 -2
View File
@@ -43,7 +43,7 @@ class Zookeeper extends Process
if (Snowflake::getPlatform()->isLinux()) { if (Snowflake::getPlatform()->isLinux()) {
name($this->pid, 'Crontab zookeeper.'); name($this->pid, 'Crontab zookeeper.');
} }
Timer::tick(1000, function () { while (true) {
[$range, $redis] = $this->loadCarobTask(); [$range, $redis] = $this->loadCarobTask();
$server = Snowflake::app()->getSwoole(); $server = Snowflake::app()->getSwoole();
@@ -52,7 +52,8 @@ class Zookeeper extends Process
$this->dispatch($server, $redis, $setting, $value); $this->dispatch($server, $redis, $setting, $value);
} }
$redis->release(); $redis->release();
}); sleep(1);
}
} }