This commit is contained in:
2021-03-19 19:16:12 +08:00
parent 8d5a66e16b
commit 88061f5f3d
4 changed files with 123 additions and 17 deletions
-1
View File
@@ -28,7 +28,6 @@ use Annotation\Annotation as SAnnotation;
use ReflectionException;
use Snowflake\Async;
use Snowflake\Cache\Redis;
use Snowflake\Crontab;
use Snowflake\Di\Service;
use Snowflake\Error\ErrorHandler;
use Snowflake\Error\Logger;
+65
View File
@@ -0,0 +1,65 @@
<?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');
}
}
-1
View File
@@ -17,7 +17,6 @@ use HttpServer\Server;
use Kafka\Producer;
use Snowflake\Async;
use Snowflake\Cache\Redis;
use Snowflake\Crontab;
use Snowflake\Error\Logger;
use Snowflake\Event;
use Snowflake\Jwt\Jwt;
+58 -15
View File
@@ -6,13 +6,13 @@ namespace Snowflake;
use Closure;
use Exception;
use Snowflake\Abstracts\Component;
use Snowflake\Abstracts\BaseObject;
/**
* Class Async
* @package Snowflake
*/
class Crontab extends Component
class Crontab extends BaseObject
{
@@ -36,6 +36,62 @@ class Crontab extends Component
private int $execute_number = 0;
/**
* @return array|Closure
*/
public function getHandler(): array|Closure
{
return $this->handler;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return mixed
*/
public function getParams(): mixed
{
return $this->params;
}
/**
* @return int
*/
public function getTickTime(): int
{
return $this->tickTime;
}
/**
* @return bool
*/
public function isLoop(): bool
{
return $this->isLoop;
}
/**
* @return int
*/
public function getMaxExecuteNumber(): int
{
return $this->max_execute_number;
}
/**
* @return int
*/
public function getExecuteNumber(): int
{
return $this->execute_number;
}
/**
* @param array|Closure $handler
@@ -94,19 +150,6 @@ class Crontab extends Component
}
/**
* @throws Exception
*/
public function dispatch()
{
$redis = Snowflake::app()->getRedis();
$executeTime = $this->tickTime + time();
$redis->zAdd('system:crontab', (string)$executeTime, serialize($this));
}
/**
* @throws Exception
*/