From 38e1d732a43775541b1f08116682331d1cb41f9b Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 24 Jan 2024 14:16:09 +0800 Subject: [PATCH] Added cron support for closure-command (#6495) --- CHANGELOG-3.1.md | 1 + src/command/src/ClosureCommand.php | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/CHANGELOG-3.1.md b/CHANGELOG-3.1.md index 990b86539..b8862211a 100644 --- a/CHANGELOG-3.1.md +++ b/CHANGELOG-3.1.md @@ -8,6 +8,7 @@ - [#6483](https://github.com/hyperf/hyperf/pull/6483) [#6487] (https://github.com/hyperf/hyperf/pull/6487) Added new ways to register crontab. - [#6488](https://github.com/hyperf/hyperf/pull/6488) Added the default implement for `Psr\Log\LoggerInterface`. +- [#6495](https://github.com/hyperf/hyperf/pull/6495) Added cron support for closure-command. ## Optimized diff --git a/src/command/src/ClosureCommand.php b/src/command/src/ClosureCommand.php index ce00d08bf..16f9e4308 100644 --- a/src/command/src/ClosureCommand.php +++ b/src/command/src/ClosureCommand.php @@ -12,8 +12,12 @@ declare(strict_types=1); namespace Hyperf\Command; use Closure; +use Hyperf\Crontab\Crontab; +use Hyperf\Crontab\Schedule; use Psr\Container\ContainerInterface; +use function Hyperf\Tappable\tap; + final class ClosureCommand extends Command { private ParameterParser $parameterParser; @@ -43,4 +47,20 @@ final class ClosureCommand extends Command return $this; } + + /** + * @param callable(Crontab $crontab):Crontab|null $callback + */ + public function cron(string $rule, array $arguments = [], ?callable $callback = null): self + { + tap( + Schedule::command($this->getName(), $arguments) + ->setName($this->getName()) + ->setRule($rule) + ->setMemo($this->getDescription()), + $callback + ); + + return $this; + } }