Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon what are the best practices for managing failed jobs in Laravel


what are the best practices for managing failed jobs in Laravel


Managing failed jobs in Laravel involves several best practices to ensure reliable and efficient handling of job failures. Here are some key strategies and techniques to consider:

Automatic Retry

1. Specify the Number of Retries:
- Set the `$tries` property in your job class to specify the maximum number of times a job should be retried if it fails:

php
     public $tries = 3;
     

Delayed Retries

2. Specify a Delay:
- Use the `$backoff` property to specify a delay before retrying a failed job:

php
     public $backoff = 3;
     

Custom Error Handling

3. Implement Custom Error Handling:
- Define the `failed()` method in your job class to handle custom error handling logic:

php
     public function failed(Exception $exception) {
         // Log the error or send a notification
     }
     

Logging

4. Effective Logging:
- Use Laravel's comprehensive logging capabilities to log error messages, stack traces, and debugging information to various channels:

php
     Log::error('Error occurred while processing job');
     

Notification

5. Notification System:
- Use Laravel's notification system to send notifications (emails, SMS, Slack messages, etc.) when specific events occur, such as job failures:

php
     Notification::send(new JobFailedNotification($job));
     

Job Chaining

6. Job Chaining:
- Implement job chaining by creating multiple job classes and specifying the chain of jobs to be executed in a particular order:

php
     CreateUserJob::withChain([
         new SendWelcomeEmailJob(),
         new LogRegistrationActivityJob(),
         new NotifyAdminJob(),
     ])->dispatch();
     

Batch Retry

7. Batch Retry:
- Use the `queue:failed:batch-retry` command to retry failed jobs in batches, filtering by specific conditions such as job type, exception type, or time range:

bash
     php artisan queue:failed:batch-retry --failed-after="2 days ago" --queue="default" --limit=10 --filter="CrawlWebsiteJob" --filter-by-exception="ModelNotFoundException"
     

Pruning Failed Jobs

8. Pruning Failed Jobs:
- Use the `queue:prune-failed` command to prune the records in your application's `failed_jobs` table, retaining only the failed job records that were inserted within a specified time period:

bash
     php artisan queue:prune-failed --hours=48
     

Storing Failed Jobs in DynamoDB

9. Storing Failed Jobs in DynamoDB:
- Configure Laravel to store failed job records in DynamoDB instead of a relational database table:

php
     'queue' => [
         'failed' => [
             'driver' => 'dynamodb',
             // DynamoDB table configuration
         ],
     ],
     

Supervisor Configuration

10. Supervisor Configuration:
- Configure Supervisor to manage your Laravel workers and ensure that they are running reliably:

bash
      sudo supervisorctl reread
      sudo supervisorctl update
      sudo supervisorctl start "laravel-worker:*"
      

By following these best practices, you can effectively manage failed jobs in Laravel, ensuring reliable and efficient handling of job failures in your application.

Citations:
[1] https://www.twilio.com/en-us/blog/queueing-in-laravel
[2] https://github.com/kirschbaum-development/laravel-queue-batch-retry
[3] https://www.reddit.com/r/laravel/comments/w8o3uc/help_me_understand_how_to_handle_failing_jobs/
[4] https://laravel.com/docs/11.x/queues
[5] https://laracasts.com/discuss/channels/laravel/best-way-to-retry-failed-jobs