From cf5c8d49ac42d84f4d29ccc96d0c9314b911154e Mon Sep 17 00:00:00 2001 From: pandaLIU <26201936+PandaLIU-1111@users.noreply.github.com> Date: Mon, 13 Dec 2021 14:00:36 +0800 Subject: [PATCH] Added `FailToExecute` event which will be dispatched when executing crontab failed. (#4344) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update crontab.md Co-authored-by: 李铭昕 <715557344@qq.com> --- CHANGELOG-2.2.md | 4 +++ docs/zh-cn/crontab.md | 37 ++++++++++++++++++++++++ src/crontab/src/Event/FailToExecute.php | 34 ++++++++++++++++++++++ src/crontab/src/Strategy/Executor.php | 19 +++++++++--- src/crontab/tests/ExecutorTest.php | Bin 3623 -> 3871 bytes 5 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 src/crontab/src/Event/FailToExecute.php diff --git a/CHANGELOG-2.2.md b/CHANGELOG-2.2.md index 792484101..07f05a91c 100644 --- a/CHANGELOG-2.2.md +++ b/CHANGELOG-2.2.md @@ -4,6 +4,10 @@ - [#4347](https://github.com/hyperf/hyperf/pull/4347) Fixed bug that amqp io has been bound to more than one coroutine when out of buffer. +## Added + +- [#4344](https://github.com/hyperf/hyperf/pull/4344) Added `Hyperf\Crontab\Event\FailToExecute` event which will be dispatched when executing crontab failed. + # v2.2.20 - 2021-12-13 ## Fixed diff --git a/docs/zh-cn/crontab.md b/docs/zh-cn/crontab.md index 1d9fd06cd..18133b5e3 100644 --- a/docs/zh-cn/crontab.md +++ b/docs/zh-cn/crontab.md @@ -253,3 +253,40 @@ return [ 当您完成上述的配置后,以及定义了定时任务后,只需要直接启动 `Server`,定时任务便会一同启动。 在您启动后,即便您定义了足够短周期的定时任务,定时任务也不会马上开始执行,所有定时任务都会等到下一个分钟周期时才会开始执行,比如您启动的时候是 `10 时 11 分 12 秒`,那么定时任务会在 `10 时 12 分 00 秒` 才会正式开始执行。 + +### FailToExecute 事件 + +当定时任务执行失败时,会触发 `FailToExecute` 事件,所以我们可以编写以下监听器,拿到对应的 `Crontab` 和 `Throwable`。 + +```php +crontab->getName()); + var_dump($event->throwable->getMessage()); + } +} +``` diff --git a/src/crontab/src/Event/FailToExecute.php b/src/crontab/src/Event/FailToExecute.php new file mode 100644 index 000000000..bcdd674bc --- /dev/null +++ b/src/crontab/src/Event/FailToExecute.php @@ -0,0 +1,34 @@ +crontab = $crontab; + $this->throwable = $throwable; + } +} diff --git a/src/crontab/src/Strategy/Executor.php b/src/crontab/src/Strategy/Executor.php index c836194e9..72badaf46 100644 --- a/src/crontab/src/Strategy/Executor.php +++ b/src/crontab/src/Strategy/Executor.php @@ -16,6 +16,7 @@ use Closure; use Hyperf\Contract\ApplicationInterface; use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Crontab\Crontab; +use Hyperf\Crontab\Event\FailToExecute; use Hyperf\Crontab\LoggerInterface; use Hyperf\Crontab\Mutex\RedisServerMutex; use Hyperf\Crontab\Mutex\RedisTaskMutex; @@ -23,6 +24,7 @@ use Hyperf\Crontab\Mutex\ServerMutex; use Hyperf\Crontab\Mutex\TaskMutex; use Hyperf\Utils\Coroutine; use Psr\Container\ContainerInterface; +use Psr\EventDispatcher\EventDispatcherInterface; use Swoole\Timer; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\NullOutput; @@ -49,6 +51,11 @@ class Executor */ protected $serverMutex; + /** + * @var null|EventDispatcherInterface + */ + protected $dispatcher; + public function __construct(ContainerInterface $container) { $this->container = $container; @@ -57,6 +64,9 @@ class Executor } elseif ($container->has(StdoutLoggerInterface::class)) { $this->logger = $container->get(StdoutLoggerInterface::class); } + if ($container->has(EventDispatcherInterface::class)) { + $this->dispatcher = $container->get(EventDispatcherInterface::class); + } } public function execute(Crontab $crontab) @@ -83,6 +93,7 @@ class Executor } } catch (\Throwable $throwable) { $result = false; + $this->dispatcher && $this->dispatcher->dispatch(new FailToExecute($crontab, $throwable)); } finally { $this->logResult($crontab, $result); } @@ -139,8 +150,8 @@ class Executor { if (! $this->taskMutex) { $this->taskMutex = $this->container->has(TaskMutex::class) - ? $this->container->get(TaskMutex::class) - : $this->container->get(RedisTaskMutex::class); + ? $this->container->get(TaskMutex::class) + : $this->container->get(RedisTaskMutex::class); } return $this->taskMutex; } @@ -163,8 +174,8 @@ class Executor { if (! $this->serverMutex) { $this->serverMutex = $this->container->has(ServerMutex::class) - ? $this->container->get(ServerMutex::class) - : $this->container->get(RedisServerMutex::class); + ? $this->container->get(ServerMutex::class) + : $this->container->get(RedisServerMutex::class); } return $this->serverMutex; } diff --git a/src/crontab/tests/ExecutorTest.php b/src/crontab/tests/ExecutorTest.php index 72be161a0c37ed1b4a845ab2fd766a97943440f1..c21a1c3d19e831dc5aa2bb1b8c5a49a8566d846c 100644 GIT binary patch delta 104 zcmZ23Ghc4Q1t!IS;-VPWvedj1m(1dV#FFHUR5bqP{Y;FklM~o^cu|y1*5x*syq1k~ X^LqAm%pk^Oe|A|^C7X4*e=z|7_`)UX delta 26 icmbO)w_Ik!1*XkR%pX`LujAp_oX9bed2=YwDJB4kzX_NC