Queues
Overview
In this guide, you can learn how to use MongoDB as your database for Laravel Queue. Laravel Queue allows you to create queued jobs that are processed in the background.
Configuration
To use MongoDB as your database for Laravel Queue, change
the driver in your application's config/queue.php
file:
'connections' => [ 'database' => [ 'driver' => 'mongodb', // You can also specify your jobs-specific database // in the config/database.php file 'connection' => 'mongodb', 'table' => 'jobs', 'queue' => 'default', // Optional setting // 'retry_after' => 60, ], ],
The following table describes properties that you can specify to configure the behavior of the queue:
Setting | Description |
---|---|
driver | Required Queue driver to use. The value of
this property must be mongodb . |
connection | Database connection used to store jobs. It must be a
mongodb connection. The driver uses the default connection if
a connection is not specified. |
table | Required Name of the MongoDB collection to
store jobs to process. |
queue | Required Name of the queue. |
retry_after | Specifies how many seconds the queue connection should wait
before retrying a job that is being processed. The value is
60 by default. |
To use MongoDB to handle failed jobs, create a failed
entry in your
application's config/queue.php
file and specify the database and
collection:
'failed' => [ 'driver' => 'mongodb', 'database' => 'mongodb', 'table' => 'failed_jobs', ],
The following table describes properties that you can specify to configure how to handle failed jobs:
Setting | Description |
---|---|
driver | Required Queue driver to use. The value of
this property must be mongodb . |
database | Database connection used to store jobs. It must be
a mongodb connection. The driver uses the default connection
if a connection is not specified. |
table | Name of the MongoDB collection to store failed
jobs. The value is failed_jobs by default. |
To register failed jobs, you can use the default failed job provider from Laravel. To learn more, see Dealing With Failed Jobs in the Laravel documentation on Queues.
Job Batching
Job batching is a Laravel feature that enables you to execute a batch of jobs and related actions before, after, and during the execution of the jobs from the queue. To learn more about this feature, see Job Batching in the Laravel documentation.
In MongoDB, you don't have to create a designated collection before
using job batching. The job_batches
collection is created
automatically to store metadata about your job batches, such as
their completion percentage.
To enable job batching, create the batching
entry in your
application's config/queue.php
file:
'batching' => [ 'driver' => 'mongodb', 'database' => 'mongodb', 'table' => 'job_batches', ],
The following table describes properties that you can specify to configure job batching:
Setting | Description |
---|---|
driver | Required Queue driver to use. The value of
this property must be mongodb . |
database | Database connection used to store jobs. It must be a
mongodb connection. The driver uses the default connection if
a connection is not specified. |
table | Name of the MongoDB collection to store job
batches. The value is job_batches by default. |
Then, add the service provider in your application's config/app.php
file:
The Laravel Integration automatically provides the
MongoDB\Laravel\MongoDBBusServiceProvider::class
class as the
service provider for job batching.