-
Notifications
You must be signed in to change notification settings - Fork 11.3k
[12.x] ScheduledTaskFailed
not dispatched on scheduled forground task fails
#55624
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
base: 12.x
Are you sure you want to change the base?
[12.x] ScheduledTaskFailed
not dispatched on scheduled forground task fails
#55624
Conversation
- Throw an exception for scheduled commands that fail in the foreground, providing the exit code in the message. - Introduce a new test suite to verify the behavior of scheduled commands, ensuring that events are dispatched correctly for failed, successful, and background tasks.
This reverts commit cdc08e3.
Thanks for submitting a PR! Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
if there is any other case you want me to test or handle, feel free to drop a comment. thanks |
ScheduledTaskFailed
not dispatched on scheduled forground task fails
Now that it's restricted to foreground tasks, I think that no side effects will be encountered. Thanks for your work, @achrafAa! |
most welcome, always happy to help |
Fix: Consistent Failure Event Dispatching for Foreground and Background Scheduled Tasks
Description
This PR resolves inconsistencies in Laravel's scheduler related to how task failures are handled and how the ScheduledTaskFailed event is dispatched.
Problem
Two main issues were affecting scheduled task failure behavior:
Foreground task failures (e.g., exceptions or non-zero exit codes) did not always result in
ScheduledTaskFailed
being dispatched properly.Background tasks using runInBackground() could cause unexpected exceptions when exit codes were not returned or were null, disrupting the scheduler.
This led to unreliable error monitoring and regressions after PR #55572, which had to be reverted due to these side effects.
Solution
This PR ensures:
Foreground task failures now reliably dispatch the ScheduledTaskFailed event when a task throws an exception or exits with a non-zero status code.
Background tasks (runInBackground()) are excluded from failure exceptions when exit codes are not explicitly returned, preventing unintended scheduler crashes.
The scheduler behaves consistently across both execution modes while preserving Laravel's event-driven error handling model.
ScheduledTaskFinished
ScheduledTaskFailed
$event->run($this->laravel);
itself throwsScheduledTaskFinished
ScheduledTaskFailed
$event->run($this->laravel);
itself throwsSo to be clear, ScheduledTaskFinished should always be dispatched unless the exception happens before or outside the actual command execution (e.g. command not found, or failure to start).
Impact
Ensures accurate and consistent dispatching of the ScheduledTaskFailed event.
Prevents exceptions from being thrown for background tasks with null or missing exit codes.
Maintains backward compatibility and avoids introducing regressions.
Related Issues
Fixes Scheduled Task Failure Events Not Consistently Dispatched (Including runInBackground Cases) #55614
Builds on discussion from reverted PR [12.x]
ScheduledTaskFailed
not dispatched on scheduled task failing #55572