Added enable to control the crontab task which to register or not. (#3325)

Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
yansongda 2021-03-02 19:39:31 +08:00 committed by GitHub
parent fb4774f3c5
commit 810e7a4f2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 79 additions and 7 deletions

View File

@ -4,6 +4,10 @@
- [#3326](https://github.com/hyperf/hyperf/pull/3326) Fixed bug that `unpack` custom data failed when using `JsonEofPacker`.
## Added
- [#3325](https://github.com/hyperf/hyperf/pull/3325) Added `enable` to control the crontab task which to register or not.
# v2.1.8 - 2021-03-01
## Fixed

View File

@ -67,6 +67,11 @@ class Crontab extends AbstractAnnotation
*/
public $memo = '';
/**
* @var bool
*/
public $enable = true;
public function __construct($value = null)
{
parent::__construct($value);

View File

@ -65,6 +65,11 @@ class Crontab
*/
protected $executeTime;
/**
* @var bool
*/
protected $enable = true;
public function getName(): ?string
{
return $this->name;
@ -174,4 +179,15 @@ class Crontab
$this->executeTime = $executeTime;
return $this;
}
public function isEnable(): bool
{
return $this->enable;
}
public function setEnable(bool $enable): Crontab
{
$this->enable = $enable;
return $this;
}
}

View File

@ -30,7 +30,7 @@ class CrontabManager
public function register(Crontab $crontab): bool
{
if (! $this->isValidCrontab($crontab)) {
if (! $this->isValidCrontab($crontab) || ! $crontab->isEnable()) {
return false;
}
$this->crontabs[$crontab->getName()] = $crontab;

View File

@ -18,7 +18,6 @@ use Hyperf\Crontab\Crontab;
use Hyperf\Crontab\CrontabManager;
use Hyperf\Di\Annotation\AnnotationCollector;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Process\Annotation\Process;
use Hyperf\Process\Event\BeforeCoroutineHandle;
use Hyperf\Process\Event\BeforeProcessHandle;
@ -100,6 +99,7 @@ class CrontabRegisterListener implements ListenerInterface
isset($annotation->onOneServer) && $crontab->setOnOneServer($annotation->onOneServer);
isset($annotation->callback) && $crontab->setCallback($annotation->callback);
isset($annotation->memo) && $crontab->setMemo($annotation->memo);
isset($annotation->enable) && $crontab->setEnable($annotation->enable);
return $crontab;
}
}

View File

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Crontab;
use Hyperf\Crontab\Crontab;
use Hyperf\Crontab\CrontabManager;
use Hyperf\Crontab\Parser;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class CrontabManagerTest extends TestCase
{
public function testCrontabRegister()
{
$crontab = new Crontab();
$manager = new CrontabManager(new Parser());
$manager->register($crontab);
$this->assertSame([], $manager->getCrontabs());
$manager->register(tap(new Crontab(), function (Crontab $crontab) {
$crontab->setName('test')->setRule('* * * * *')->setCallback(static function () {
});
}));
$this->assertArrayHasKey('test', $manager->getCrontabs());
$manager->register(tap(new Crontab(), function (Crontab $crontab) {
$crontab->setName('test2')->setRule('* * * * *')->setEnable(false)->setCallback(static function () {
});
}));
$this->assertArrayNotHasKey('test2', $manager->getCrontabs());
}
}

View File

@ -14,8 +14,8 @@ namespace HyperfTest\Crontab;
use Hyperf\Crontab\CrontabManager;
use Hyperf\Crontab\Parser;
use Hyperf\Crontab\Scheduler;
use Hyperf\Utils\Reflection\ClassInvoker;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
/**
* @internal
@ -26,9 +26,9 @@ class SchedulerTest extends TestCase
public function testGetSchedules()
{
$scheduler = new Scheduler(new CrontabManager(new Parser()));
$reflectionMethod = new ReflectionMethod(Scheduler::class, 'getSchedules');
$reflectionMethod->setAccessible(true);
$result = $reflectionMethod->invoke($scheduler);
$this->assertSame([], $result);
$invoker = new ClassInvoker($scheduler);
$this->assertSame([], $invoker->getSchedules());
}
}