lavary / crunz

A PHP-based job scheduler
MIT License
1.42k stars 136 forks source link

Task specific onError callback? #281

Open Notmissleo opened 4 years ago

Notmissleo commented 4 years ago

Description

Is it possible for there to be a way to set a task specific onError callback function? Right now, an onError function can be set but there's no way (afaik) to limit it to a single task like before() and after().

It would be nice if onError() can be used just like before() and after() callbacks.

Basically a post-execution callback that's only called if the execution of the event (not schedule) is unsucessful.

Example

In addition to this:

use Crunz\Schedule;

$schedule = new Schedule();

$task = $schedule->run('command/to/run');
$task->everyFiveMinutes();

$schedule
->onError(function() {
   // Send mail
})
->onError(function() {
   // Do something else
});

return $schedule;

It would be nice if this was supported :

use Crunz\Schedule;

$schedule = new Schedule();

$task = $schedule->run('command/to/run');
$task->everyFiveMinutes();

$task->onError(function() {
   // Send mail
})
->onError(function() {
   // Do something else
});

return $schedule;
PabloKowalczyk commented 4 years ago

Hello, actually it is possible internally to do this. I've implemented it as a part of #261. IMO it should be easy to make Event::addErrorCallback public, it just need some unit tests. Would you like to provide a PR?