Queues

Introduction

{tip} Laravel now offers Horizon, a beautiful dashboard and configuration system for your Redis powered queues. Check out the full Horizon documentation for more information.

Laravel queues provide a unified API across a variety of different queue backends, such as Beanstalk, Amazon SQS, Redis, or even a relational database. Queues allow you to defer the processing of a time consuming task, such as sending an email, until a later time. Deferring these time consuming tasks drastically speeds up web requests to your application.

The queue configuration file is stored in config/queue.php. In this file you will find connection configurations for each of the queue drivers that are included with the framework, which includes a database, Beanstalkd, Amazon SQS, Redis, and a synchronous driver that will execute jobs immediately (for local use). A null queue driver is also included which discards queued jobs.

Connections Vs. Queues

Before getting started with Laravel queues, it is important to understand the distinction between “connections” and “queues”. In your config/queue.php configuration file, there is a connections configuration option. This option defines a particular connection to a backend service such as Amazon SQS, Beanstalk, or Redis. However, any given queue connection may have multiple “queues” which may be thought of as different stacks or piles of queued jobs.

Note that each connection configuration example in the queue configuration file contains a queue attribute. This is the default queue that jobs will be dispatched to when they are sent to a given connection. In other words, if you dispatch a job without explicitly defining which queue it should be dispatched to, the job will be placed on the queue that is defined in the queue attribute of the connection configuration:

  1. // This job is sent to the default queue...
  2. Job::dispatch();
  3. // This job is sent to the "emails" queue...
  4. Job::dispatch()->onQueue('emails');

Some applications may not need to ever push jobs onto multiple queues, instead preferring to have one simple queue. However, pushing jobs to multiple queues can be especially useful for applications that wish to prioritize or segment how jobs are processed, since the Laravel queue worker allows you to specify which queues it should process by priority. For example, if you push jobs to a high queue, you may run a worker that gives them higher processing priority:

  1. php artisan queue:work --queue=high,default

Driver Notes & Prerequisites

Database

In order to use the database queue driver, you will need a database table to hold the jobs. To generate a migration that creates this table, run the queue:table Artisan command. Once the migration has been created, you may migrate your database using the migrate command:

  1. php artisan queue:table
  2. php artisan migrate

Redis

In order to use the redis queue driver, you should configure a Redis database connection in your config/database.php configuration file.

Redis Cluster

If your Redis queue connection uses a Redis Cluster, your queue names must contain a key hash tag. This is required in order to ensure all of the Redis keys for a given queue are placed into the same hash slot:

  1. 'redis' => [
  2. 'driver' => 'redis',
  3. 'connection' => 'default',
  4. 'queue' => '{default}',
  5. 'retry_after' => 90,
  6. ],

Blocking

When using the Redis queue, you may use the block_for configuration option to specify how long the driver should wait for a job to become available before iterating through the worker loop and re-polling the Redis database.

Adjusting this value based on your queue load can be more efficient than continually polling the Redis database for new jobs. For instance, you may set the value to 5 to indicate that the driver should block for five seconds while waiting for a job to become available:

  1. 'redis' => [
  2. 'driver' => 'redis',
  3. 'connection' => 'default',
  4. 'queue' => 'default',
  5. 'retry_after' => 90,
  6. 'block_for' => 5,
  7. ],

{note} Setting block_for to 0 will cause queue workers to block indefinitely until a job is available. This will also prevent signals such as SIGTERM from being handled until the next job has been processed.

Other Driver Prerequisites

The following dependencies are needed for the listed queue drivers:

  • Amazon SQS: aws/aws-sdk-php ~3.0
  • Beanstalkd: pda/pheanstalk ~4.0
  • Redis: predis/predis ~1.0 or phpredis PHP extension

Creating Jobs

Generating Job Classes

By default, all of the queueable jobs for your application are stored in the app/Jobs directory. If the app/Jobs directory doesn’t exist, it will be created when you run the make:job Artisan command. You may generate a new queued job using the Artisan CLI:

  1. php artisan make:job ProcessPodcast

The generated class will implement the Illuminate\Contracts\Queue\ShouldQueue interface, indicating to Laravel that the job should be pushed onto the queue to run asynchronously.

{tip} Job stubs may be customized using stub publishing

Class Structure

Job classes are very simple, normally containing only a handle method which is called when the job is processed by the queue. To get started, let’s take a look at an example job class. In this example, we’ll pretend we manage a podcast publishing service and need to process the uploaded podcast files before they are published:

  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Podcast;
  4. use App\Services\AudioProcessor;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. class ProcessPodcast implements ShouldQueue
  11. {
  12. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  13. protected $podcast;
  14. /**
  15. * Create a new job instance.
  16. *
  17. * @param Podcast $podcast
  18. * @return void
  19. */
  20. public function __construct(Podcast $podcast)
  21. {
  22. $this->podcast = $podcast;
  23. }
  24. /**
  25. * Execute the job.
  26. *
  27. * @param AudioProcessor $processor
  28. * @return void
  29. */
  30. public function handle(AudioProcessor $processor)
  31. {
  32. // Process uploaded podcast...
  33. }
  34. }

In this example, note that we were able to pass an Eloquent model directly into the queued job’s constructor. Because of the SerializesModels trait that the job is using, Eloquent models and their loaded relationships will be gracefully serialized and unserialized when the job is processing. If your queued job accepts an Eloquent model in its constructor, only the identifier for the model will be serialized onto the queue. When the job is actually handled, the queue system will automatically re-retrieve the full model instance and its loaded relationships from the database. It’s all totally transparent to your application and prevents issues that can arise from serializing full Eloquent model instances.

The handle method is called when the job is processed by the queue. Note that we are able to type-hint dependencies on the handle method of the job. The Laravel service container automatically injects these dependencies.

If you would like to take total control over how the container injects dependencies into the handle method, you may use the container’s bindMethod method. The bindMethod method accepts a callback which receives the job and the container. Within the callback, you are free to invoke the handle method however you wish. Typically, you should call this method from a service provider:

  1. use App\Jobs\ProcessPodcast;
  2. $this->app->bindMethod(ProcessPodcast::class.'@handle', function ($job, $app) {
  3. return $job->handle($app->make(AudioProcessor::class));
  4. });

{note} Binary data, such as raw image contents, should be passed through the base64_encode function before being passed to a queued job. Otherwise, the job may not properly serialize to JSON when being placed on the queue.

Handling Relationships

Because loaded relationships also get serialized, the serialized job string can become quite large. To prevent relations from being serialized, you can call the withoutRelations method on the model when setting a property value. This method will return an instance of the model with no loaded relationships:

  1. /**
  2. * Create a new job instance.
  3. *
  4. * @param \App\Models\Podcast $podcast
  5. * @return void
  6. */
  7. public function __construct(Podcast $podcast)
  8. {
  9. $this->podcast = $podcast->withoutRelations();
  10. }

Job Middleware

Job middleware allow you to wrap custom logic around the execution of queued jobs, reducing boilerplate in the jobs themselves. For example, consider the following handle method which leverages Laravel’s Redis rate limiting features to allow only one job to process every five seconds:

  1. /**
  2. * Execute the job.
  3. *
  4. * @return void
  5. */
  6. public function handle()
  7. {
  8. Redis::throttle('key')->block(0)->allow(1)->every(5)->then(function () {
  9. info('Lock obtained...');
  10. // Handle job...
  11. }, function () {
  12. // Could not obtain lock...
  13. return $this->release(5);
  14. });
  15. }

While this code is valid, the structure of the handle method becomes noisy since it is cluttered with Redis rate limiting logic. In addition, this rate limiting logic must be duplicated for any other jobs that we want to rate limit.

Instead of rate limiting in the handle method, we could define a job middleware that handles rate limiting. Laravel does not have a default location for job middleware, so you are welcome to place job middleware anywhere in your application. In this example, we will place the middleware in a app/Jobs/Middleware directory:

  1. <?php
  2. namespace App\Jobs\Middleware;
  3. use Illuminate\Support\Facades\Redis;
  4. class RateLimited
  5. {
  6. /**
  7. * Process the queued job.
  8. *
  9. * @param mixed $job
  10. * @param callable $next
  11. * @return mixed
  12. */
  13. public function handle($job, $next)
  14. {
  15. Redis::throttle('key')
  16. ->block(0)->allow(1)->every(5)
  17. ->then(function () use ($job, $next) {
  18. // Lock obtained...
  19. $next($job);
  20. }, function () use ($job) {
  21. // Could not obtain lock...
  22. $job->release(5);
  23. });
  24. }
  25. }

As you can see, like route middleware, job middleware receive the job being processed and a callback that should be invoked to continue processing the job.

After creating job middleware, they may be attached to a job by returning them from the job’s middleware method. This method does not exist on jobs scaffolded by the make:job Artisan command, so you will need to add it to your own job class definition:

  1. use App\Jobs\Middleware\RateLimited;
  2. /**
  3. * Get the middleware the job should pass through.
  4. *
  5. * @return array
  6. */
  7. public function middleware()
  8. {
  9. return [new RateLimited];
  10. }

Dispatching Jobs

Once you have written your job class, you may dispatch it using the dispatch method on the job itself. The arguments passed to the dispatch method will be given to the job’s constructor:

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Jobs\ProcessPodcast;
  5. use Illuminate\Http\Request;
  6. class PodcastController extends Controller
  7. {
  8. /**
  9. * Store a new podcast.
  10. *
  11. * @param Request $request
  12. * @return Response
  13. */
  14. public function store(Request $request)
  15. {
  16. // Create podcast...
  17. ProcessPodcast::dispatch($podcast);
  18. }
  19. }

If you would like to conditionally dispatch a job, you may use the dispatchIf and dispatchUnless methods:

  1. ProcessPodcast::dispatchIf($accountActive === true, $podcast);
  2. ProcessPodcast::dispatchUnless($accountSuspended === false, $podcast);

Delayed Dispatching

If you would like to delay the execution of a queued job, you may use the delay method when dispatching a job. For example, let’s specify that a job should not be available for processing until 10 minutes after it has been dispatched:

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Jobs\ProcessPodcast;
  5. use Illuminate\Http\Request;
  6. class PodcastController extends Controller
  7. {
  8. /**
  9. * Store a new podcast.
  10. *
  11. * @param Request $request
  12. * @return Response
  13. */
  14. public function store(Request $request)
  15. {
  16. // Create podcast...
  17. ProcessPodcast::dispatch($podcast)
  18. ->delay(now()->addMinutes(10));
  19. }
  20. }

{note} The Amazon SQS queue service has a maximum delay time of 15 minutes.

Dispatching After The Response Is Sent To Browser

Alternatively, the dispatchAfterResponse method delays dispatching a job until after the response is sent to the user’s browser. This will still allow the user to begin using the application even though a queued job is still executing. This should typically only be used for jobs that take about a second, such as sending an email:

  1. use App\Jobs\SendNotification;
  2. SendNotification::dispatchAfterResponse();

You may dispatch a Closure and chain the afterResponse method onto the helper to execute a Closure after the response has been sent to the browser:

  1. use App\Mail\WelcomeMessage;
  2. use Illuminate\Support\Facades\Mail;
  3. dispatch(function () {
  4. Mail::to('[email protected]')->send(new WelcomeMessage);
  5. })->afterResponse();

Synchronous Dispatching

If you would like to dispatch a job immediately (synchronously), you may use the dispatchSync method. When using this method, the job will not be queued and will be run immediately within the current process:

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Jobs\ProcessPodcast;
  5. use Illuminate\Http\Request;
  6. class PodcastController extends Controller
  7. {
  8. /**
  9. * Store a new podcast.
  10. *
  11. * @param Request $request
  12. * @return Response
  13. */
  14. public function store(Request $request)
  15. {
  16. // Create podcast...
  17. ProcessPodcast::dispatchSync($podcast);
  18. }
  19. }

Job Chaining

Job chaining allows you to specify a list of queued jobs that should be run in sequence after the primary job has executed successfully. If one job in the sequence fails, the rest of the jobs will not be run. To execute a queued job chain, you may use the chain method provided by the Bus facade:

  1. use Illuminate\Support\Facades\Bus;
  2. Bus::chain([
  3. new ProcessPodcast,
  4. new OptimizePodcast,
  5. new ReleasePodcast,
  6. ])->dispatch();

In addition to chaining job class instances, you may also chain Closures:

  1. Bus::chain([
  2. new ProcessPodcast,
  3. new OptimizePodcast,
  4. function () {
  5. Podcast::update(...);
  6. },
  7. ])->dispatch();

{note} Deleting jobs using the $this->delete() method will not prevent chained jobs from being processed. The chain will only stop executing if a job in the chain fails.

Chain Connection & Queue

If you would like to specify the connection and queue that should be used for the chained jobs, you may use the onConnection and onQueue methods. These methods specify the queue connection and queue name that should be used unless the queued job is explicitly assigned a different connection / queue:

  1. Bus::chain([
  2. new ProcessPodcast,
  3. new OptimizePodcast,
  4. new ReleasePodcast,
  5. ])->onConnection('redis')->onQueue('podcasts')->dispatch();

Chain Failures

When chaining jobs, you may use the chain method to specify a Closure that should be invoked if a job within the chain fails. The given callback will receive the exception instance that caused the job failure:

  1. use Illuminate\Support\Facades\Bus;
  2. use Throwable;
  3. Bus::chain([
  4. new ProcessPodcast,
  5. new OptimizePodcast,
  6. new ReleasePodcast,
  7. ])->catch(function (Throwable $e) {
  8. // A job within the chain has failed...
  9. })->dispatch();

Customizing The Queue & Connection

Dispatching To A Particular Queue

By pushing jobs to different queues, you may “categorize” your queued jobs and even prioritize how many workers you assign to various queues. Keep in mind, this does not push jobs to different queue “connections” as defined by your queue configuration file, but only to specific queues within a single connection. To specify the queue, use the onQueue method when dispatching the job:

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Jobs\ProcessPodcast;
  5. use Illuminate\Http\Request;
  6. class PodcastController extends Controller
  7. {
  8. /**
  9. * Store a new podcast.
  10. *
  11. * @param Request $request
  12. * @return Response
  13. */
  14. public function store(Request $request)
  15. {
  16. // Create podcast...
  17. ProcessPodcast::dispatch($podcast)->onQueue('processing');
  18. }
  19. }

Dispatching To A Particular Connection

If you are working with multiple queue connections, you may specify which connection to push a job to. To specify the connection, use the onConnection method when dispatching the job:

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Jobs\ProcessPodcast;
  5. use Illuminate\Http\Request;
  6. class PodcastController extends Controller
  7. {
  8. /**
  9. * Store a new podcast.
  10. *
  11. * @param Request $request
  12. * @return Response
  13. */
  14. public function store(Request $request)
  15. {
  16. // Create podcast...
  17. ProcessPodcast::dispatch($podcast)->onConnection('sqs');
  18. }
  19. }

You may chain the onConnection and onQueue methods to specify the connection and the queue for a job:

  1. ProcessPodcast::dispatch($podcast)
  2. ->onConnection('sqs')
  3. ->onQueue('processing');

Specifying Max Job Attempts / Timeout Values

Max Attempts

One approach to specifying the maximum number of times a job may be attempted is via the --tries switch on the Artisan command line:

  1. php artisan queue:work --tries=3

However, you may take a more granular approach by defining the maximum number of attempts on the job class itself. If the maximum number of attempts is specified on the job, it will take precedence over the value provided on the command line:

  1. <?php
  2. namespace App\Jobs;
  3. class ProcessPodcast implements ShouldQueue
  4. {
  5. /**
  6. * The number of times the job may be attempted.
  7. *
  8. * @var int
  9. */
  10. public $tries = 5;
  11. }

Time Based Attempts

As an alternative to defining how many times a job may be attempted before it fails, you may define a time at which the job should timeout. This allows a job to be attempted any number of times within a given time frame. To define the time at which a job should timeout, add a retryUntil method to your job class:

  1. /**
  2. * Determine the time at which the job should timeout.
  3. *
  4. * @return \DateTime
  5. */
  6. public function retryUntil()
  7. {
  8. return now()->addSeconds(5);
  9. }

{tip} You may also define a retryUntil method on your queued event listeners.

Max Exceptions

Sometimes you may wish to specify that a job may be attempted many times, but should fail if the retries are triggered by a given number of exceptions. To accomplish this, you may define a maxExceptions property on your job class:

  1. <?php
  2. namespace App\Jobs;
  3. class ProcessPodcast implements ShouldQueue
  4. {
  5. /**
  6. * The number of times the job may be attempted.
  7. *
  8. * @var int
  9. */
  10. public $tries = 25;
  11. /**
  12. * The maximum number of exceptions to allow before failing.
  13. *
  14. * @var int
  15. */
  16. public $maxExceptions = 3;
  17. /**
  18. * Execute the job.
  19. *
  20. * @return void
  21. */
  22. public function handle()
  23. {
  24. Redis::throttle('key')->allow(10)->every(60)->then(function () {
  25. // Lock obtained, process the podcast...
  26. }, function () {
  27. // Unable to obtain lock...
  28. return $this->release(10);
  29. });
  30. }
  31. }

In this example, the job is released for ten seconds if the application is unable to obtain a Redis lock and will continue to be retried up to 25 times. However, the job will fail if three unhandled exceptions are thrown by the job.

Timeout

{note} The pcntl PHP extension must be installed in order to specify job timeouts.

Likewise, the maximum number of seconds that jobs can run may be specified using the --timeout switch on the Artisan command line:

  1. php artisan queue:work --timeout=30

However, you may also define the maximum number of seconds a job should be allowed to run on the job class itself. If the timeout is specified on the job, it will take precedence over any timeout specified on the command line:

  1. <?php
  2. namespace App\Jobs;
  3. class ProcessPodcast implements ShouldQueue
  4. {
  5. /**
  6. * The number of seconds the job can run before timing out.
  7. *
  8. * @var int
  9. */
  10. public $timeout = 120;
  11. }

Sometimes, IO blocking processes such as sockets or outgoing HTTP connections may not respect your specified timeout. Therefore, when using these features, you should always attempt to specify a timeout using their APIs as well. For example, when using Guzzle, you should always specify a connection and request timeout value.

Rate Limiting

{note} This feature requires that your application can interact with a Redis server.

If your application interacts with Redis, you may throttle your queued jobs by time or concurrency. This feature can be of assistance when your queued jobs are interacting with APIs that are also rate limited.

For example, using the throttle method, you may throttle a given type of job to only run 10 times every 60 seconds. If a lock can not be obtained, you should typically release the job back onto the queue so it can be retried later:

  1. Redis::throttle('key')->allow(10)->every(60)->then(function () {
  2. // Job logic...
  3. }, function () {
  4. // Could not obtain lock...
  5. return $this->release(10);
  6. });

{tip} In the example above, the key may be any string that uniquely identifies the type of job you would like to rate limit. For example, you may wish to construct the key based on the class name of the job and the IDs of the Eloquent models it operates on.

{note} Releasing a throttled job back onto the queue will still increment the job’s total number of attempts.

Alternatively, you may specify the maximum number of workers that may simultaneously process a given job. This can be helpful when a queued job is modifying a resource that should only be modified by one job at a time. For example, using the funnel method, you may limit jobs of a given type to only be processed by one worker at a time:

  1. Redis::funnel('key')->limit(1)->then(function () {
  2. // Job logic...
  3. }, function () {
  4. // Could not obtain lock...
  5. return $this->release(10);
  6. });

{tip} When using rate limiting, the number of attempts your job will need to run successfully can be hard to determine. Therefore, it is useful to combine rate limiting with time based attempts.

Error Handling

If an exception is thrown while the job is being processed, the job will automatically be released back onto the queue so it may be attempted again. The job will continue to be released until it has been attempted the maximum number of times allowed by your application. The maximum number of attempts is defined by the --tries switch used on the queue:work Artisan command. Alternatively, the maximum number of attempts may be defined on the job class itself. More information on running the queue worker can be found below.

Job Batching

Laravel’s job batching feature allows you to easily execute a batch of jobs and then perform some action when the batch of jobs has completed executing. Before getting started, you should create a database migration to build a table that will contain your job batch meta information. This migration may be generated using the queue:batches-table Artisan command:

  1. php artisan queue:batches-table
  2. php artisan migrate

Defining Batchable Jobs

To build a batchable job, you should create a queueable job as normal; however, you should add the Illuminate\Bus\Batchable trait to the job class. This trait provides access to a batch method which may be used to retrieve the current batch that the job is executing in:

  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Podcast;
  4. use App\Services\AudioProcessor;
  5. use Illuminate\Bus\Batchable;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. class ProcessPodcast implements ShouldQueue
  12. {
  13. use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  14. /**
  15. * Execute the job.
  16. *
  17. * @return void
  18. */
  19. public function handle()
  20. {
  21. if ($this->batch()->cancelled()) {
  22. // Detected cancelled batch...
  23. return;
  24. }
  25. // Batched job executing...
  26. }
  27. }

Dispatching Batches

To dispatch a batch of jobs, you should use batch method of the Bus facade. Of course, batching is primarily useful when combined with completion callbacks. So, you may use the then, catch, and finally methods to define completion callbacks for the batch. Each of these callbacks will receive an Illuminate\Bus\Batch instance when they are invoked:

  1. use App\Jobs\ProcessPodcast;
  2. use App\Podcast;
  3. use Illuminate\Bus\Batch;
  4. use Illuminate\Support\Facades\Bus;
  5. use Throwable;
  6. $batch = Bus::batch([
  7. new ProcessPodcast(Podcast::find(1)),
  8. new ProcessPodcast(Podcast::find(2)),
  9. new ProcessPodcast(Podcast::find(3)),
  10. new ProcessPodcast(Podcast::find(4)),
  11. new ProcessPodcast(Podcast::find(5)),
  12. ])->then(function (Batch $batch) {
  13. // All jobs completed successfully...
  14. })->catch(function (Batch $batch, Throwable $e) {
  15. // First batch job failure detected...
  16. })->finally(function (Batch $batch) {
  17. // The batch has finished executing...
  18. })->dispatch();
  19. return $batch->id;

Naming Batches

Some tools such as Laravel Horizon and Laravel Telescope may provide more user-friendly debug information for batches if batches are named. To assign an arbitrary name to a batch, you may call the name method while defining the batch:

  1. $batch = Bus::batch([
  2. // ...
  3. ])->then(function (Batch $batch) {
  4. // All jobs completed successfully...
  5. })->name('Process Podcasts')->dispatch();

Batch Connection & Queue

If you would like to specify the connection and queue that should be used for the batched jobs, you may use the onConnection and onQueue methods:

  1. $batch = Bus::batch([
  2. // ...
  3. ])->then(function (Batch $batch) {
  4. // All jobs completed successfully...
  5. })->onConnection('redis')->onQueue('podcasts')->dispatch();

Adding Jobs To Batches

Sometimes it may be useful to add additional jobs to a batch from within a batched job. This pattern can be useful when you need to batch thousands of jobs which may take too long to dispatch during a web request. So, instead, you may wish to dispatch an initial batch of “loader” jobs that hydrate the batch with more jobs:

  1. $batch = Bus::batch([
  2. new LoadImportBatch,
  3. new LoadImportBatch,
  4. new LoadImportBatch,
  5. ])->then(function (Batch $batch) {
  6. // All jobs completed successfully...
  7. })->name('Import Contacts')->dispatch();

In this example, we will use the LoadImportBatch job to hydrate the batch with additional jobs. To accomplish this, we may use the add method on the batch instance that can be accessed within the job:

  1. use App\Jobs\ImportContacts;
  2. use Illuminate\Support\Collection;
  3. /**
  4. * Execute the job.
  5. *
  6. * @return void
  7. */
  8. public function handle()
  9. {
  10. if ($this->batch()->cancelled()) {
  11. return;
  12. }
  13. $this->batch()->add(Collection::times(1000, function () {
  14. return new ImportContacts;
  15. }));
  16. }

{note} You may only add jobs to a batch from within a job that belongs to the same batch.

Inspecting Batches

The Illuminate\Bus\Batch method that is provided to batch completion callbacks has a variety of properties and methods to assist you in interacting with and inspecting a given batch of jobs.

  1. // The UUID of the batch...
  2. $batch->id;
  3. // The name of the batch (if applicable)...
  4. $batch->name;
  5. // The number of jobs assigned to the batch...
  6. $batch->totalJobs;
  7. // The number of jobs that have not been processed by the queue...
  8. $batch->pendingJobs;
  9. // The number of jobs that have failed...
  10. $batch->failedJobs;
  11. // The number of jobs that have been processed thus far...
  12. $batch->processedJobs();
  13. // The completion percentage of the batch (0-100)...
  14. $batch->progress();
  15. // Indicates if the batch has finished executing...
  16. $batch->finished();
  17. // Cancel the execution of the batch...
  18. $batch->cancel();
  19. // Indicates if the batch has been cancelled...
  20. $batch->cancelled();

Returning Batches From Routes

All Illuminate\Bus\Batch instances are JSON serializable, meaning you can return them directly from one of your application’s routes to retrieve a JSON payload containing information about the batch, including its completion progress. To retrieve a batch by its ID, you may use the Bus facade’s findBatch method:

  1. use Illuminate\Support\Facades\Bus;
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/batch/{batchId}', function (string $batchId) {
  4. return Bus::findBatch($batchId);
  5. });

Cancelling Batches

Sometimes you may need to cancel a given batch’s execution. This can be accomplished by calling the cancel method on the Illuminate\Bus\Batch instance:

  1. /**
  2. * Execute the job.
  3. *
  4. * @return void
  5. */
  6. public function handle()
  7. {
  8. if ($this->user->exceedsImportLimit()) {
  9. return $this->batch()->cancel();
  10. }
  11. if ($this->batch()->cancelled()) {
  12. return;
  13. }
  14. }

Batch Failures

When a batch job fails, the catch callback (if assigned) will be invoked. This callback is only invoked for the job that fails within the batch.

Allowing Failures

When a job within a batch fails, Laravel will automatically mark the batch as “cancelled”. If you wish, you may disable this behavior so that a job failure does not automatically mark the batch as cancelled. This may be accomplished by calling the allowFailures method while dispatching the batch:

  1. $batch = Bus::batch([
  2. // ...
  3. ])->then(function (Batch $batch) {
  4. // All jobs completed successfully...
  5. })->allowFailures()->dispatch();

Retrying Failed Batch Jobs

For convenience, Laravel provides a queue:retry-batch Artisan command that allows you to easily retry all of the failed jobs for a given batch. The queue:retry-batch command accepts the UUID of the batch whose failed jobs should be retried:

  1. php artisan queue:retry-batch 32dbc76c-4f82-4749-b610-a639fe0099b5

Queueing Closures

Instead of dispatching a job class to the queue, you may also dispatch a Closure. This is great for quick, simple tasks that need to be executed outside of the current request cycle. When dispatching Closures to the queue, the Closure’s code contents is cryptographically signed so it can not be modified in transit:

  1. $podcast = App\Podcast::find(1);
  2. dispatch(function () use ($podcast) {
  3. $podcast->publish();
  4. });

Using the catch method, you may provide a Closure that should be executed if the queued Closure fails to complete successfully after exhausting all of your queue’s configured retry attempts:

  1. use Throwable;
  2. dispatch(function () use ($podcast) {
  3. $podcast->publish();
  4. })->catch(function (Throwable $e) {
  5. // This job has failed...
  6. });

Running The Queue Worker

Laravel includes a queue worker that will process new jobs as they are pushed onto the queue. You may run the worker using the queue:work Artisan command. Note that once the queue:work command has started, it will continue to run until it is manually stopped or you close your terminal:

  1. php artisan queue:work

{tip} To keep the queue:work process running permanently in the background, you should use a process monitor such as Supervisor to ensure that the queue worker does not stop running.

Remember, queue workers are long-lived processes and store the booted application state in memory. As a result, they will not notice changes in your code base after they have been started. So, during your deployment process, be sure to restart your queue workers. In addition, remember that any static state created or modified by your application will not be automatically reset between jobs.

Alternatively, you may run the queue:listen command. When using the queue:listen command, you don’t have to manually restart the worker when you want to reload your updated code or reset the application state; however, this command is not as efficient as queue:work:

  1. php artisan queue:listen

Specifying The Connection & Queue

You may also specify which queue connection the worker should utilize. The connection name passed to the work command should correspond to one of the connections defined in your config/queue.php configuration file:

  1. php artisan queue:work redis

You may customize your queue worker even further by only processing particular queues for a given connection. For example, if all of your emails are processed in an emails queue on your redis queue connection, you may issue the following command to start a worker that only processes that queue:

  1. php artisan queue:work redis --queue=emails

Processing A Specified Number Of Jobs

The --once option may be used to instruct the worker to only process a single job from the queue:

  1. php artisan queue:work --once

The --max-jobs option may be used to instruct the worker to process the given number of jobs and then exit. This option may be useful when combined with Supervisor so that your workers are automatically restarted after processing a given number of jobs:

  1. php artisan queue:work --max-jobs=1000

Processing All Queued Jobs & Then Exiting

The --stop-when-empty option may be used to instruct the worker to process all jobs and then exit gracefully. This option can be useful when working Laravel queues within a Docker container if you wish to shutdown the container after the queue is empty:

  1. php artisan queue:work --stop-when-empty

Processing Jobs For A Given Number Of Seconds

The --max-time option may be used to instruct the worker to process jobs for the given number of seconds and then exit. This option may be useful when combined with Supervisor so that your workers are automatically restarted after processing jobs for a given amount of time:

  1. // Process jobs for one hour and then exit...
  2. php artisan queue:work --max-time=3600

Resource Considerations

Daemon queue workers do not “reboot” the framework before processing each job. Therefore, you should free any heavy resources after each job completes. For example, if you are doing image manipulation with the GD library, you should free the memory with imagedestroy when you are done.

Queue Priorities

Sometimes you may wish to prioritize how your queues are processed. For example, in your config/queue.php you may set the default queue for your redis connection to low. However, occasionally you may wish to push a job to a high priority queue like so:

  1. dispatch((new Job)->onQueue('high'));

To start a worker that verifies that all of the high queue jobs are processed before continuing to any jobs on the low queue, pass a comma-delimited list of queue names to the work command:

  1. php artisan queue:work --queue=high,low

Queue Workers & Deployment

Since queue workers are long-lived processes, they will not pick up changes to your code without being restarted. So, the simplest way to deploy an application using queue workers is to restart the workers during your deployment process. You may gracefully restart all of the workers by issuing the queue:restart command:

  1. php artisan queue:restart

This command will instruct all queue workers to gracefully “die” after they finish processing their current job so that no existing jobs are lost. Since the queue workers will die when the queue:restart command is executed, you should be running a process manager such as Supervisor to automatically restart the queue workers.

{tip} The queue uses the cache to store restart signals, so you should verify a cache driver is properly configured for your application before using this feature.

Job Expirations & Timeouts

Job Expiration

In your config/queue.php configuration file, each queue connection defines a retry_after option. This option specifies how many seconds the queue connection should wait before retrying a job that is being processed. For example, if the value of retry_after is set to 90, the job will be released back onto the queue if it has been processing for 90 seconds without being deleted. Typically, you should set the retry_after value to the maximum number of seconds your jobs should reasonably take to complete processing.

{note} The only queue connection which does not contain a retry_after value is Amazon SQS. SQS will retry the job based on the Default Visibility Timeout which is managed within the AWS console.

Worker Timeouts

The queue:work Artisan command exposes a --timeout option. The --timeout option specifies how long the Laravel queue master process will wait before killing off a child queue worker that is processing a job. Sometimes a child queue process can become “frozen” for various reasons. The --timeout option removes frozen processes that have exceeded that specified time limit:

  1. php artisan queue:work --timeout=60

The retry_after configuration option and the --timeout CLI option are different, but work together to ensure that jobs are not lost and that jobs are only successfully processed once.

{note} The --timeout value should always be at least several seconds shorter than your retry_after configuration value. This will ensure that a worker processing a given job is always killed before the job is retried. If your --timeout option is longer than your retry_after configuration value, your jobs may be processed twice.

Worker Sleep Duration

When jobs are available on the queue, the worker will keep processing jobs with no delay in between them. However, the sleep option determines how long (in seconds) the worker will “sleep” if there are no new jobs available. While sleeping, the worker will not process any new jobs - the jobs will be processed after the worker wakes up again.

  1. php artisan queue:work --sleep=3

Supervisor Configuration

Installing Supervisor

Supervisor is a process monitor for the Linux operating system, and will automatically restart your queue:work process if it fails. To install Supervisor on Ubuntu, you may use the following command:

  1. sudo apt-get install supervisor

{tip} If configuring Supervisor yourself sounds overwhelming, consider using Laravel Forge, which will automatically install and configure Supervisor for your Laravel projects.

Configuring Supervisor

Supervisor configuration files are typically stored in the /etc/supervisor/conf.d directory. Within this directory, you may create any number of configuration files that instruct supervisor how your processes should be monitored. For example, let’s create a laravel-worker.conf file that starts and monitors a queue:work process:

  1. [program:laravel-worker]
  2. process_name=%(program_name)s_%(process_num)02d
  3. command=php /home/forge/app.com/artisan queue:work sqs --sleep=3 --tries=3
  4. autostart=true
  5. autorestart=true
  6. user=forge
  7. numprocs=8
  8. redirect_stderr=true
  9. stdout_logfile=/home/forge/app.com/worker.log
  10. stopwaitsecs=3600

In this example, the numprocs directive will instruct Supervisor to run 8 queue:work processes and monitor all of them, automatically restarting them if they fail. You should change the queue:work sqs portion of the command directive to reflect your desired queue connection.

{note} You should ensure that the value of stopwaitsecs is greater than the number of seconds consumed by your longest running job. Otherwise, Supervisor may kill the job before it is finished processing.

Starting Supervisor

Once the configuration file has been created, you may update the Supervisor configuration and start the processes using the following commands:

  1. sudo supervisorctl reread
  2. sudo supervisorctl update
  3. sudo supervisorctl start laravel-worker:*

For more information on Supervisor, consult the Supervisor documentation.

Dealing With Failed Jobs

Sometimes your queued jobs will fail. Don’t worry, things don’t always go as planned! Laravel includes a convenient way to specify the maximum number of times a job should be attempted. After a job has exceeded this amount of attempts, it will be inserted into the failed_jobs database table. To create a migration for the failed_jobs table, you may use the queue:failed-table command:

  1. php artisan queue:failed-table
  2. php artisan migrate

Then, when running your queue worker, you can specify the maximum number of times a job should be attempted using the --tries switch on the queue:work command. If you do not specify a value for the --tries option, jobs will only be attempted once:

  1. php artisan queue:work redis --tries=3

In addition, you may specify how many seconds Laravel should wait before retrying a job that has failed using the --backoff option. By default, a job is retried immediately:

  1. php artisan queue:work redis --tries=3 --backoff=3

If you would like to configure the failed job retry delay on a per-job basis, you may do so by defining a backoff property on your queued job class:

  1. /**
  2. * The number of seconds to wait before retrying the job.
  3. *
  4. * @var int
  5. */
  6. public $backoff = 3;

If you require more complex logic for determining the retry delay, you may define a backoff method on your queued job class:

  1. /**
  2. * Calculate the number of seconds to wait before retrying the job.
  3. *
  4. * @return int
  5. */
  6. public function backoff()
  7. {
  8. return 3;
  9. }

You may easily configure “exponential” backoffs by returning an array of backoff values from the backoff method. In this example, the retry delay will be 1 seconds for the first retry, 5 seconds for the second retry, and 10 seconds for the third retry:

  1. /**
  2. * Calculate the number of seconds to wait before retrying the job.
  3. *
  4. * @return array
  5. */
  6. public function backoff()
  7. {
  8. return [1, 5, 10];
  9. }

Cleaning Up After Failed Jobs

You may define a failed method directly on your job class, allowing you to perform job specific clean-up when a failure occurs. This is the perfect location to send an alert to your users or revert any actions performed by the job. The Throwable exception that caused the job to fail will be passed to the failed method:

  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Podcast;
  4. use App\Services\AudioProcessor;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Throwable;
  10. class ProcessPodcast implements ShouldQueue
  11. {
  12. use InteractsWithQueue, Queueable, SerializesModels;
  13. protected $podcast;
  14. /**
  15. * Create a new job instance.
  16. *
  17. * @param \App\Models\Podcast $podcast
  18. * @return void
  19. */
  20. public function __construct(Podcast $podcast)
  21. {
  22. $this->podcast = $podcast;
  23. }
  24. /**
  25. * Execute the job.
  26. *
  27. * @param \App\Services\AudioProcessor $processor
  28. * @return void
  29. */
  30. public function handle(AudioProcessor $processor)
  31. {
  32. // Process uploaded podcast...
  33. }
  34. /**
  35. * Handle a job failure.
  36. *
  37. * @param \Throwable $exception
  38. * @return void
  39. */
  40. public function failed(Throwable $exception)
  41. {
  42. // Send user notification of failure, etc...
  43. }
  44. }

Failed Job Events

If you would like to register an event that will be called when a job fails, you may use the Queue::failing method. This event is a great opportunity to notify your team via email or Slack. For example, we may attach a callback to this event from the AppServiceProvider that is included with Laravel:

  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\Facades\Queue;
  4. use Illuminate\Support\ServiceProvider;
  5. use Illuminate\Queue\Events\JobFailed;
  6. class AppServiceProvider extends ServiceProvider
  7. {
  8. /**
  9. * Register any application services.
  10. *
  11. * @return void
  12. */
  13. public function register()
  14. {
  15. //
  16. }
  17. /**
  18. * Bootstrap any application services.
  19. *
  20. * @return void
  21. */
  22. public function boot()
  23. {
  24. Queue::failing(function (JobFailed $event) {
  25. // $event->connectionName
  26. // $event->job
  27. // $event->exception
  28. });
  29. }
  30. }

Retrying Failed Jobs

To view all of your failed jobs that have been inserted into your failed_jobs database table, you may use the queue:failed Artisan command:

  1. php artisan queue:failed

The queue:failed command will list the job ID, connection, queue, failure time, and other information about the job. The job ID may be used to retry the failed job. For instance, to retry a failed job that has an ID of 5, issue the following command:

  1. php artisan queue:retry 5

If necessary, you may pass multiple IDs or an ID range (when using numeric IDs) to the command:

  1. php artisan queue:retry 5 6 7 8 9 10
  2. php artisan queue:retry --range=5-10

To retry all of your failed jobs, execute the queue:retry command and pass all as the ID:

  1. php artisan queue:retry all

If you would like to delete a failed job, you may use the queue:forget command:

  1. php artisan queue:forget 5

To delete all of your failed jobs, you may use the queue:flush command:

  1. php artisan queue:flush

Ignoring Missing Models

When injecting an Eloquent model into a job, it is automatically serialized before being placed on the queue and restored when the job is processed. However, if the model has been deleted while the job was waiting to be processed by a worker, your job may fail with a ModelNotFoundException.

For convenience, you may choose to automatically delete jobs with missing models by setting your job’s deleteWhenMissingModels property to true:

  1. /**
  2. * Delete the job if its models no longer exist.
  3. *
  4. * @var bool
  5. */
  6. public $deleteWhenMissingModels = true;

Job Events

Using the before and after methods on the Queue facade, you may specify callbacks to be executed before or after a queued job is processed. These callbacks are a great opportunity to perform additional logging or increment statistics for a dashboard. Typically, you should call these methods from a service provider. For example, we may use the AppServiceProvider that is included with Laravel:

  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\Facades\Queue;
  4. use Illuminate\Support\ServiceProvider;
  5. use Illuminate\Queue\Events\JobProcessed;
  6. use Illuminate\Queue\Events\JobProcessing;
  7. class AppServiceProvider extends ServiceProvider
  8. {
  9. /**
  10. * Register any application services.
  11. *
  12. * @return void
  13. */
  14. public function register()
  15. {
  16. //
  17. }
  18. /**
  19. * Bootstrap any application services.
  20. *
  21. * @return void
  22. */
  23. public function boot()
  24. {
  25. Queue::before(function (JobProcessing $event) {
  26. // $event->connectionName
  27. // $event->job
  28. // $event->job->payload()
  29. });
  30. Queue::after(function (JobProcessed $event) {
  31. // $event->connectionName
  32. // $event->job
  33. // $event->job->payload()
  34. });
  35. }
  36. }

Using the looping method on the Queue facade, you may specify callbacks that execute before the worker attempts to fetch a job from a queue. For example, you might register a Closure to rollback any transactions that were left open by a previously failed job:

  1. Queue::looping(function () {
  2. while (DB::transactionLevel() > 0) {
  3. DB::rollBack();
  4. }
  5. });