-
Notifications
You must be signed in to change notification settings - Fork 11.3k
[12.x]ScheduledTaskFailed
not dispatched on scheduled task failing
#55572
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
[12.x]ScheduledTaskFailed
not dispatched on scheduled task failing
#55572
Conversation
…tion test - Updated `ScheduleRunCommand` to dispatch `ScheduledTaskFailed` event when a scheduled command fails. - Added handling for exit codes to improve error reporting. - Introduced `ScheduleRunCommandTest` to verify that failure events are dispatched correctly for failing scheduled tasks.
…tion test - Updated `ScheduleRunCommand` to dispatch `ScheduledTaskFailed` event when a scheduled command fails. - Added handling for exit codes to improve error reporting. - Introduced `ScheduleRunCommandTest` to verify that failure events are dispatched correctly for failing scheduled tasks.
…tion test - Updated `ScheduleRunCommand` to dispatch `ScheduledTaskFailed` event when a scheduled command fails. - Added handling for exit codes to improve error reporting. - Introduced `ScheduleRunCommandTest` to verify that failure events are dispatched correctly for failing scheduled tasks.
…ing testing complication
…ing testing complication
Much needed bugfix! |
Am I the only one who started getting Schedule errors after this update? Error: it looks like its related to running your commands in background. If you have a Schedule set up like this: |
@tarkis Root Cause:The update introduced strict exit code checking for background commands. Your logs show an empty exit code ([]), which fails the strict comparison with 0 (success). When using runInBackground(), the Horizon snapshot command isn't properly returning an exit code Previously, this silent failure went unnoticed The update properly surfaces these issues by validating exit codes Immediate Fix (not optimal):
Proper Solution: Ensuring the command returns proper exit codes (0 for success) Adding explicit status code handling for background processes I'd recommend: Opening an issue with Horizon if none exists Checking if your Horizon version needs updating Monitoring your queue workers during this transition The error is actually helping surface a real issue - your scheduled command wasn't properly succeeding even before the update. This change makes your system more reliable in the long run. |
Removing |
can you confirm that this change works with your horizon setup, or provide clear reproduction steps if this is a known issue |
it not only horizon. You can reproduce with any command.
p.s. commands themselves are working and executing. The problem is that at the end runInBackground() throws an exception. |
@tarkis its fixed with your code changes, i recommend you add reproduce instructions in your PR for other contributors. |
Can you add tests for this to ensure that we will not break it in future updates? |
ScheduledTaskFailed
not dispatched on scheduled task failing
Fix:
ScheduledTaskFailed
Event Not Dispatching on All Failures in Laravel SchedulerOverview
Laravel's scheduler fails to dispatch the
ScheduledTaskFailed
event under various failure scenarios, despite documentation stating it should. This affects commands that throw exceptions, return non-zero exit codes, fail in the background, or fail at the system level.This change ensures the
ScheduledTaskFailed
event is dispatched consistently for all failure conditions.This update is non-breaking, as it doesn't interfere with other events or trigger any unintended behavior. It corrects the behavior of the
ScheduledTaskFailed
event, which was not being fired as expected. Essentially, this is a bug fix rather than a new feature. We don't anticipate any breaking changes, but if you have a different perspective or see a potential issue, we’d appreciate your input in the comments so we can address it accordingly.Problem Description
The
ScheduledTaskFailed
event does not fire in the following situations:Even though Laravel includes code for dispatching this event, it never actually fires under real-world usage, as noted in Issue #55352.
✅ Expected Behavior
The
ScheduledTaskFailed
event should be dispatched when any scheduled task fails.❌ Actual Behavior
Failures are visible in the console, but the
ScheduledTaskFailed
event is never triggered.How to Reproduce
1. Register a Failing Command
2. Set Up an Event Listener
3. Run the Scheduler
Result:
The command fails visibly, but no event is dispatched.
Root Cause
Laravel’s
ScheduleRunCommand::runEvent()
It does not consistently dispatchScheduledTaskFailed
if a task fails silently with a non-zero exit code.and the logic that checks for such conditions is missing.
Changes Made
ScheduleRunCommand::runEvent()
to:ScheduledTaskFailed
when a non-zero exit code is encountered.Benefits
Screenshots of testing
Actual code
case 1 : successful command :
Correct: No ScheduledTaskFailed event fired ✅
case 2 : failed command :
Bug: ScheduledTaskFailed not fired when command fails ❌
PR code
case 1 : successful command :
Correct: No ScheduledTaskFailed event fired ✅
case 2 : failed command :
Fixed: ScheduledTaskFailed now properly fires on failure ✅
Usage
After this fix, monitor all scheduled task failures like this:
Related
Status
✅ Fixed —
ScheduledTaskFailed
is now reliably dispatched for all failure types.