Files
kiri-core/System/Crontab/Producer.php
T

70 lines
1.5 KiB
PHP
Raw Normal View History

2021-03-19 19:16:12 +08:00
<?php
2021-03-26 01:01:21 +08:00
namespace Snowflake\Crontab;
2021-03-19 19:16:12 +08:00
2021-04-14 15:07:04 +08:00
use Exception;
2021-03-26 01:01:21 +08:00
use Snowflake\Abstracts\Component;
2021-03-19 19:16:12 +08:00
use Snowflake\Snowflake;
/**
2021-03-26 01:01:21 +08:00
* Class Producer
2021-03-19 19:16:12 +08:00
* @package Snowflake\Abstracts
*/
2021-03-26 01:01:21 +08:00
class Producer extends Component
2021-03-19 19:16:12 +08:00
{
2021-04-16 00:35:55 +08:00
const CRONTAB_KEY = '_application:system:crontab';
/**
* @param Crontab $crontab
* @throws Exception
*/
public function dispatch(Crontab $crontab)
{
$redis = Snowflake::app()->getRedis();
$name = $crontab->getName();
2021-04-16 00:58:44 +08:00
if ($redis->exists(self::CRONTAB_KEY) && $redis->type(self::CRONTAB_KEY) !== \Redis::REDIS_ZSET) {
2021-04-16 00:35:55 +08:00
throw new Exception('Cache key ' . self::CRONTAB_KEY . ' types error.');
}
2021-04-16 01:07:19 +08:00
$redis->del('crontab:' . $name);
2021-04-16 01:05:10 +08:00
$redis->zRem(static::CRONTAB_KEY, $name);
2021-04-16 00:54:54 +08:00
2021-04-16 00:58:44 +08:00
$redis->zAdd(self::CRONTAB_KEY, time() + $crontab->getTickTime(), $name);
2021-04-16 00:54:54 +08:00
$redis->set('crontab:' . $name, swoole_serialize($crontab));
2021-04-16 00:35:55 +08:00
}
/**
* @param string $name
* @throws Exception
*/
public function clear(string $name)
{
$redis = Snowflake::app()->getRedis();
$redis->setex('stop:crontab:' . md5($name), 120, 1);
}
/**
* @throws Exception
*/
public function clearAll()
{
$redis = Snowflake::app()->getRedis();
$data = $redis->zRange(self::CRONTAB_KEY, 0, -1);
foreach ($data as $datum) {
$redis->setex('stop:crontab:' . $datum, 120, 1);
$redis->del('crontab:' . $datum);
}
$redis->release();
}
2021-03-19 19:16:12 +08:00
}