Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatch BeforeExecute and AfterExecute events in catchToExecute method. #6252

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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