0
0
Laravelframework~10 mins

Failed job handling in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Failed job handling
Job dispatched
Job runs
Job success?
NoJob fails
Job recorded in failed_jobs table
Job done
Admin retries or inspects
Job re-dispatched or fixed
This flow shows how Laravel handles a job that fails during execution by recording it and allowing retry or inspection.
Execution Sample
Laravel
<?php
dispatch(new ProcessOrder($order));
// Job runs
// If fails, Laravel logs failure
// Admin can retry failed jobs
This code dispatches a job; if it fails, Laravel logs it for later retry or inspection.
Execution Table
StepActionJob StatusDatabase TableNext Step
1Job dispatchedPendingNoneJob runs
2Job starts runningRunningNoneCheck success
3Job fails during executionFailedfailed_jobsRecord failure
4Failure recorded in failed_jobs tableFailedfailed_jobsAdmin inspects or retries
5Admin retries job (deletes record)PendingNoneJob runs again
6Job succeeds after retrySucceededNoneProcess complete
7No failed job record remainsSucceededNoneProcess complete
💡 Record removed from failed_jobs on retry; job succeeds, ending the failure handling cycle.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
Job StatusPendingRunningFailedPendingSucceeded
failed_jobs tableEmptyEmptyHas failure recordEmptyEmpty
Key Moments - 3 Insights
Why does the job status change to 'Failed' even though the job code is still running?
When the job encounters an error or exception, Laravel marks it as 'Failed' immediately and records it in the failed_jobs table (see execution_table step 3). This happens before the job fully stops.
What happens to the failed job record after a successful retry?
Upon retrying, Laravel removes the job record from the failed_jobs table (see execution_table step 5), then dispatches a new job instance.
Can a failed job be retried multiple times?
Yes, the admin can retry failed jobs multiple times. Each retry sets the job status back to 'Pending' and reruns it (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the job status at step 3?
APending
BRunning
CFailed
DSucceeded
💡 Hint
Check the 'Job Status' column at step 3 in the execution_table.
At which step does Laravel record the failure in the failed_jobs table?
AStep 2
BStep 3
CStep 5
DStep 7
💡 Hint
Look at the 'Database Table' column in the execution_table to see when failed_jobs is updated.
If the admin never retries the failed job, what will be the final job status?
AFailed
BSucceeded
CPending
DRemoved
💡 Hint
Refer to the variable_tracker for 'Job Status' after step 3 and no retry steps.
Concept Snapshot
Laravel Failed Job Handling:
- Dispatch job with dispatch(new JobClass)
- If job fails, Laravel logs it in failed_jobs table
- Admin can inspect and retry failed jobs
- Retry deletes record from failed_jobs and reruns
- Keeps app reliable by tracking failures
Full Transcript
In Laravel, when you dispatch a job, it runs in the background. If the job runs successfully, it finishes quietly. But if something goes wrong, Laravel marks the job as failed and records it in a special database table called failed_jobs. This lets you see which jobs failed and why. You can then retry these failed jobs manually or automatically. When you retry, Laravel removes the record from the failed_jobs table and dispatches the job to run again. This process helps keep your app reliable by tracking and managing background job failures.