In Laravel, you can manually retry a failed job by following these steps:
Locate the failed job ID: You can find the failed job ID by running the php artisan queue:failed
command. This command will list all the failed jobs with their IDs.
Create a new command to retry the failed job: Open a terminal and navigate to your Laravel application's root directory. Run the following command to generate a new command file:
php artisan make:command RetryFailedJob
This command will generate a new file app/Console/Commands/RetryFailedJob.php
.
handle()
method in the RetryFailedJob
command: Open the generated app/Console/Commands/RetryFailedJob.php
file and replace the handle()
method with the following code:public function handle()
{
$failedJobId = <failed_job_id>; // Replace with the actual failed job ID
$failedJob = FailedJob::find($failedJobId);
if ($failedJob) {
$payload = json_decode($failedJob->payload);
$job = unserialize($payload->data->command);
dispatch($job);
$this->info('The failed job has been retried successfully!');
} else {
$this->error('Invalid failed job ID!');
}
}
Replace <failed_job_id>
with the actual failed job ID you obtained in step 1.
RetryFailedJob
command: Open the app/Console/Kernel.php
file and add the RetryFailedJob
command to the $commands
array:protected $commands = [
Commands\RetryFailedJob::class,
];
php artisan retry:failed
Once you run the command, the failed job will be retried and processed. If the job still fails, Laravel will automatically log it as a failed job again, and you can retry it later using the same steps.