Files
kiri-core/System/Abstracts/Crontab.php
T
2021-03-19 19:21:30 +08:00

66 lines
1.0 KiB
PHP

<?php
namespace Snowflake\Abstracts;
use Snowflake\Snowflake;
use Exception;
/**
* Class Crontab
* @package Snowflake\Abstracts
*/
class Crontab extends Component
{
/**
* @param \Snowflake\Crontab $crontab
* @param $executeTime
* @throws Exception
*/
public function dispatch(\Snowflake\Crontab $crontab, int $executeTime)
{
$redis = Snowflake::app()->getRedis();
$redis->zAdd('system:crontab', (string)$executeTime, serialize($crontab));
}
/**
* @param string $name
* @throws Exception
*/
public function clear(string $name)
{
$redis = Snowflake::app()->getRedis();
$data = $redis->zRevRange('system:crontab', 0, -1);
foreach ($data as $datum) {
/** @var \Snowflake\Crontab $crontab */
$crontab = unserialize($datum);
if ($crontab->getName() == $name) {
$redis->zRem('system:crontab', $datum);
break;
}
}
}
/**
* @throws Exception
*/
public function clearAll()
{
$redis = Snowflake::app()->getRedis();
$redis->zRemRangeByRank('system:crontab', '0', '-1');
}
}