Skip to content

Commit

Permalink
Dispatch BeforeExecute and AfterExecute events in catchToExecute meth…
Browse files Browse the repository at this point in the history
…od. (#6252)
  • Loading branch information
huangdijia committed Nov 2, 2023
1 parent 2f7d7fa commit 000373a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Added

- [#6236](https://github.com/hyperf/hyperf/pull/6236) Support unionType param for GenerateModelIDEVisitor.
- [#6246](https://github.com/hyperf/hyperf/pull/6246) Added crontab lifecycle events.
- [#6246](https://github.com/hyperf/hyperf/pull/6246) [#6252](https://github.com/hyperf/hyperf/pull/6252) Added crontab lifecycle events.
- [#6249](https://github.com/hyperf/hyperf/pull/6249) Support crontab closure type.

## Optimized
Expand Down
8 changes: 1 addition & 7 deletions src/crontab/src/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,10 @@
namespace Hyperf\Crontab\Event;

use Hyperf\Crontab\Crontab;
use Throwable;

abstract class Event
{
public function __construct(public Crontab $crontab, public ?Throwable $throwable = null)
public function __construct(public Crontab $crontab)
{
}

public function getThrowable(): ?Throwable
{
return $this->throwable;
}
}
12 changes: 12 additions & 0 deletions src/crontab/src/Event/FailToExecute.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
*/
namespace Hyperf\Crontab\Event;

use Hyperf\Crontab\Crontab;
use Throwable;

class FailToExecute extends Event
{
public function __construct(Crontab $crontab, public Throwable $throwable)
{
parent::__construct($crontab);
}

public function getThrowable(): Throwable
{
return $this->throwable;
}
}
4 changes: 2 additions & 2 deletions src/crontab/src/Strategy/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ protected function decorateRunnable(Crontab $crontab, Closure $runnable): Closur
protected function catchToExecute(Crontab $crontab, Closure $runnable): Closure
{
return function () use ($crontab, $runnable) {
$this->dispatcher?->dispatch(new BeforeExecute($crontab));
try {
$this->dispatcher?->dispatch(new BeforeExecute($crontab));
$result = true;
$runnable();
$this->dispatcher?->dispatch(new AfterExecute($crontab));
} catch (Throwable $throwable) {
$result = false;
$this->dispatcher?->dispatch(new FailToExecute($crontab, $throwable));
} finally {
$this->dispatcher?->dispatch(new AfterExecute($crontab, $throwable ?? null));
$this->logResult($crontab, $result, $throwable ?? null);
}
};
Expand Down

0 comments on commit 000373a

Please sign in to comment.