Queues

Introduction

The Lumen queue service provides a unified API across a variety of different queue back-ends. Queues allow you to defer the processing of a time consuming task, such as performing a task on a remote server, until a later time which drastically speeds up web requests to your application.

Like many other parts of the framework, Lumen's queued jobs function identically to Laravel's queued jobs. So, to learn more about queuing jobs in Lumen, please review the full Laravel queue documentation.

Configuration

The queue configuration options are in the .env file.

If you would like to thoroughly customize the queue configuration, then you must copy the entire vendor/laravel/lumen-framework/config/queue.php file to the config directory in the root of your project and adjust the necessary configuration options as needed. If the config directory does not exist, then you should create it.

Driver Prerequisites

Database

In order to use the database queue driver, you will need database tables to hold the jobs and failures:

  1. Schema::create('jobs', function (Blueprint $table) {
  2. $table->bigIncrements('id');
  3. $table->string('queue');
  4. $table->longText('payload');
  5. $table->tinyInteger('attempts')->unsigned();
  6. $table->unsignedInteger('reserved_at')->nullable();
  7. $table->unsignedInteger('available_at');
  8. $table->unsignedInteger('created_at');
  9. $table->index(['queue', 'reserved_at']);
  10. });
  11. Schema::create('failed_jobs', function (Blueprint $table) {
  12. $table->increments('id');
  13. $table->text('connection');
  14. $table->text('queue');
  15. $table->longText('payload');
  16. $table->longText('exception');
  17. $table->timestamp('failed_at')->useCurrent();
  18. });

Redis

In order to use the Redis queue driver, you will need to install the illuminate/redis (5.5.*) package via Composer. Then, you should register the Illuminate\Redis\RedisServiceProvider in your bootstrap/app.php file.

Other Queue Dependencies

The following dependencies are needed for the listed queue drivers:

  • Amazon SQS: aws/aws-sdk-php ~3.0
  • Beanstalkd: pda/pheanstalk ~3.0

Differences From Laravel

Like many other parts of the framework, Lumen's queued jobs function identically to Laravel's queued jobs. So, to learn more about queuing jobs in Lumen, please review the full Laravel queue documentation.

However, there are a few minor differences that we will discuss now. First, let's talk about how queued jobs are generated in Lumen.

Generators

Lumen does not include generators for automatically creating new Job classes. Instead, you should copy the ExampleJob class that is included with the framework. This class provides the basic structure that is shared by every Job class. The base Job that is used by the ExampleJob already includes the needed InteractsWithQueue, Queueable, and SerializesModels traits:

  1. <?php
  2. namespace App\Jobs;
  3. class ExampleJob extends Job
  4. {
  5. /**
  6. * Create a new job instance.
  7. *
  8. * @return void
  9. */
  10. public function __construct()
  11. {
  12. //
  13. }
  14. /**
  15. * Execute the job.
  16. *
  17. * @return void
  18. */
  19. public function handle()
  20. {
  21. //
  22. }
  23. }

Dispatching Jobs

Again, you should consult the full Laravel documentation for complete information on dispatching queued jobs; however, just like in the Laravel framework, you may use the dispatch function to dispatch jobs from anywhere within your Lumen application:

  1. dispatch(new ExampleJob);

Of course, you may also use the Queue facade. If you choose to use the facade, be sure to uncomment the call to $app->withFacades() in your bootstrap/app.php file:

  1. Queue::push(new ExampleJob);