Queue
class. Out of the box, Alchemy provides providers for queues backed by Redis and SQL as well as an in-memory mock queue.
Configuring Queues
Like other Alchemy services, Queue conforms to theService
protocol. Configure it with the config
function.
database()
queue configuration, you’ll need to add the Queue.AddJobsMigration
migration to your database’s migrations.
Creating Jobs
To make a task to run on a queue, conform to theJob
protocol. It includes a single run
function. It also requires Codable
conformance, so that any properties will be serialized and available when the job is run.
Model
s are Codable and can thus be included and persisted as properties of a job.
Dispatching Jobs
Dispatching a job is as simple as callingdispatch()
.
finished
function to hook into the result of a completed job.
Dequeuing and Running Jobs
To actually have your jobs run after dispatching them to a queue, you’ll need to run workers that monitor your various queues for work to be done. You can spin up workers as a separate process using thequeue
command.
--workers
flag when starting your server have it run the given amount of workers in process.
queues
command in Configuration.
Channels
Sometimes you may want to prioritize running some jobs over others or have workers that only run certain kinds of jobs. Alchemy provides the concept of a “channel” to help you do so. By default, jobs run on the “default” channel, but you can specify the specific channel name to run on with the channel parameter indispatch()
.
"default"
channel, but you can tell them dequeue from another channel with the -c option.
Handling Job Failures
By default, jobs that encounter an error during execution will not be retried. If you’d like to retry jobs on failure, you can add therecoveryStrategy
property. This indicates what should happen when a job is failed.
retryBackoff
to wait the specified time amount before retrying a job.