Skip to content

ScheduledTaskFailed event is not dispatched when scheduled task is failing #55352

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

Open
sagautam5 opened this issue Apr 10, 2025 · 22 comments
Open

Comments

@sagautam5
Copy link

sagautam5 commented Apr 10, 2025

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

  1. 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');
});
  1. Add example task to routes/console.php that 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();
  1. 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.

@sagautam5 sagautam5 changed the title ScheduledTaskFailed is not fired when scheduled task is failing ScheduledTaskFailed event is not dispatched when scheduled task is failing Apr 10, 2025
@Rahulsingh0106
Copy link

Since Laravel runs scheduled commands as a background process, exceptions don’t bubble up, and ScheduledTaskFailed is never fired.

Suggested Fix:
In ScheduledTaskFinished, if $event->task->exitCode !== 0, dispatch a synthetic ScheduledTaskFailed event manually.

if ($event->task->exitCode !== 0) {
$this->dispatcher->dispatch(new ScheduledTaskFailed(
$event->task,
new Exception("Scheduled Task exited with non-zero exit code.")
));
}

Copy link

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!

@erickcomp
Copy link
Contributor

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:

public function handle(ScheduledTaskFinished|ScheduledTaskFailed $event,): void
{
    if ($event instanceof ScheduledTaskFailed || $event->task->exitCode != 0) {
        $this->handleFail($event);
    }
}

protected function handleFail($event)
{
    //...
}

@sagautam5
Copy link
Author

@erickcomp In my case, ScheduledTaskFailed was not fired but ScheduledTaskFinished was fired correctly. In successful completion of scheduled task there is no issue !

@erickcomp
Copy link
Contributor

@erickcomp In my case, ScheduledTaskFailed was not fired but ScheduledTaskFinished was fired correctly. In successful completion of scheduled task there is no issue !

In my case, the ScheduledTaskFinished is being fired on failure and has exit code !== 0

@sagautam5
Copy link
Author

@erickcomp what the laravel version you are using, is it 12.x ?

@erickcomp
Copy link
Contributor

@sagautam5 . Yup, it's 12.3.

@sagautam5
Copy link
Author

@erickcomp This means issue is on version 11.x and has been fixed on laravel 12.x, right ?

@erickcomp
Copy link
Contributor

@sagautam5 No, it does not. ScheduledTaskFailed is 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
.

@achrafAa
Copy link
Contributor

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
Copy link
Author

@achrafAa it seems this will fix my issue, hopefully it will get merged very soon !

@achrafAa
Copy link
Contributor

@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

@achrafAa
Copy link
Contributor

@sagautam5 @erickcomp up and comment again on this #55572

@achrafAa
Copy link
Contributor

achrafAa commented Apr 29, 2025

@sagautam5 you can now close the issue as its addressed in this PR #55572 and Merged 🚀

@sagautam5
Copy link
Author

@achrafAa What should I do ?
Pull requests are closed with unmerged commits. May be I have misunderstood the workflow for bug fixes.
Image

Image

@achrafAa
Copy link
Contributor

@sagautam5 those are the previous ones, the merged one is #55572

@achrafAa
Copy link
Contributor

Image

@alexey-m-ukolov
Copy link

I believe this change introduced a bug: #55609

@achrafAa
Copy link
Contributor

@alexey-m-ukolov more like uncovered some hidden bugs

@achrafAa
Copy link
Contributor

@sagautam5 the issue is still here as the PR was reverted for uncovering other issues 👀

@erickcomp
Copy link
Contributor

I'm a bit confused regarding the current status:
That's what I think how this is right now:

=> First PR was reject by Taylor;
=> The second PR was merged;
=> Because of the second PR, bugs started to appear, because the tasks were not returning proper return codes;
=> The second PR was rejected;
=> A third PR was created.

Is that it, @achrafAa ?

If so, is there any way we (the ones interested in listening to this event,) can help?

Anyway, thanks for all the efforts you're putting to resolve this bug!

@achrafAa
Copy link
Contributor

achrafAa commented May 2, 2025

@erickcomp The first PR was rejected due to concerns about potential issues.The second PR was accepted after sharing test results, but later reverted after identifying problems with the runInBackground commands—specifically, they were throwing exceptions due to returning null or an exit code that the fix interpreted as a failure, since the commands were running in a separate shell.

This PR #55624 addresses the core issue: ensuring failure detection for foreground scheduled commands, without interfering with or affecting background tasks, which require separate handling outside the scope of this fix.

a comment and react can help the PR get noticed

and most welcome, I have used Laravel since 2013 and I am interested in giving back to the community and helping each other 😁.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants