mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-01 19:27:39 +08:00
Added FailToExecute
event which will be dispatched when executing crontab failed. (#4344)
* Update crontab.md Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
parent
b9f022ad41
commit
cf5c8d49ac
@ -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
|
||||
|
@ -253,3 +253,40 @@ return [
|
||||
|
||||
当您完成上述的配置后,以及定义了定时任务后,只需要直接启动 `Server`,定时任务便会一同启动。
|
||||
在您启动后,即便您定义了足够短周期的定时任务,定时任务也不会马上开始执行,所有定时任务都会等到下一个分钟周期时才会开始执行,比如您启动的时候是 `10 时 11 分 12 秒`,那么定时任务会在 `10 时 12 分 00 秒` 才会正式开始执行。
|
||||
|
||||
### FailToExecute 事件
|
||||
|
||||
当定时任务执行失败时,会触发 `FailToExecute` 事件,所以我们可以编写以下监听器,拿到对应的 `Crontab` 和 `Throwable`。
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Listener;
|
||||
|
||||
use Hyperf\Crontab\Event\FailToExecute;
|
||||
use Hyperf\Event\Annotation\Listener;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
#[Listener]
|
||||
class FailToExecuteCrontabListener implements ListenerInterface
|
||||
{
|
||||
public function listen(): array
|
||||
{
|
||||
return [
|
||||
FailToExecute::class,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FailToExecute $event
|
||||
*/
|
||||
public function process(object $event)
|
||||
{
|
||||
var_dump($event->crontab->getName());
|
||||
var_dump($event->throwable->getMessage());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
34
src/crontab/src/Event/FailToExecute.php
Normal file
34
src/crontab/src/Event/FailToExecute.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?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 Hyperf\Crontab\Event;
|
||||
|
||||
use Hyperf\Crontab\Crontab;
|
||||
use Throwable;
|
||||
|
||||
class FailToExecute
|
||||
{
|
||||
/**
|
||||
* @var Crontab
|
||||
*/
|
||||
public $crontab;
|
||||
|
||||
/**
|
||||
* @var Throwable
|
||||
*/
|
||||
public $throwable;
|
||||
|
||||
public function __construct(Crontab $crontab, Throwable $throwable)
|
||||
{
|
||||
$this->crontab = $crontab;
|
||||
$this->throwable = $throwable;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user