framework: `ScheduledTaskFailed` event is not dispatched when scheduled task is failing
Laravel Version
11.0^
PHP Version
8.4
Database Driver & Version
I am using Postgres DB
Description
I was trying to listen the laravel builtin event ScheduledTaskFailed to send notifications like email/slack etc. when scheduled command is failed in the application.
However all other events such as ScheduledTaskFinished, ScheduledTaskSkipped, ScheduledTaskStarting are dispatched correctly but ScheduledTaskFailed is not dispatched even when scheduled task is failing.
I have also looked into the implementation in framework:
try {
$event->run($this->laravel);
$this->dispatcher->dispatch(new ScheduledTaskFinished(
$event,
round(microtime(true) - $start, 2)
));
$this->eventsRan = true;
} catch (Throwable $e) {
$this->dispatcher->dispatch(new ScheduledTaskFailed($event, $e));
$this->handler->report($e);
}
In this section, implementation inside catch is never executed even when failure of scheduled task.
Is there anything I am missing or misunderstood in it ?
Steps To Reproduce
- Add following listener logic inside boot method of app service provider
use Illuminate\Console\Events\ScheduledTaskFailed;
use Illuminate\Support\Facades\Event;
Event::listen(ScheduledTaskFailed::class, function () {
logger('Scheduled task failed');
});
- Add example task to
routes/console.phpthat runs every minute & fails
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
throw new \Exception('Inspirational quote sent!');
})->purpose('Display an inspiring quote')->everyMinute();
- Run Scheduler
php artisan schedule:run
Console Error:
2025-04-10 09:55:04 Running ['artisan' inspire] .................................................................................... 774.96ms FAIL
⇂ '/usr/local/bin/php' 'artisan' inspire > '/dev/null' 2>&1
But nothing logged into laravel log.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 24 (15 by maintainers)
@sagautam5 I created a PR for both version 11 and 12 also joined some screenshots of testing
please add a comment to the PR or upvote : [12.x] Updated ScheduleRunCommand to dispatch ScheduledTaskFailed event when a scheduled command fails. #55552
[11.x] Updated ScheduleRunCommand to dispatch ScheduledTaskFailed event when a scheduled command fails. #55551
@erickcomp @Rahulsingh0106
Hey 👋 I’ve created a PR to resolve this issue by ensuring the ScheduledTaskFailed event is dispatched for all failure cases — including exceptions, non-zero exit codes, and background/system-level failures.
PR: #https://github.com/laravel/framework/pull/55551
This should make failure handling much more consistent for monitoring, alerts, and debugging workflows. Let me know if any adjustments are needed! ✅
@sagautam5 the issue is still here as the PR was reverted for uncovering other issues 👀
@achrafAa Thank you so much, I am closing this issue. Hopefully new minor release will be available soon !
Fixed by https://github.com/laravel/framework/pull/55624
@alexey-m-ukolov more like uncovered some hidden bugs
@sagautam5 @erickcomp up and comment again on this https://github.com/laravel/framework/pull/55572
@achrafAa it seems this will fix my issue, hopefully it will get merged very soon !
@sagautam5 No, it does not.
ScheduledTaskFailedis not being fired on 12.x. I put it in my code in case it gets fixed, but right now I’m relying only on the exit code .I have the same issue in Laravel 12.x. I’m still making some tests, but so far, I created a Listener that handles both “ScheduledTaskFinished” and “ScheduledTaskFinished” and in the handler, I test the exit code for non-success to do the proper error handling. Something like this:
Thank you for reporting this issue!
As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.
If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.
Thank you!