Queues
[[toc]]
Introduction
While building your web application, you may have some tasks, such as parsing and storing an uploaded CSV file, that take too long to perform during a web request. Goravel allows you to easily create queued jobs that may be processed in the background. By moving time intensive tasks to a queue, your application can respond to web requests with blazing speed and provide a better user experience to your customers. We use facades.Queue()
to implement those functions.
Goravel's queue configuration options are stored in your application's config/queue.go
configuration file. Goravel supports two drivers: redis
and sync
.
Connections Vs. Queues
Before getting started with Goravel queues, it is important to understand the distinction between "connections" and "queues". In your config/queue.go
configuration file, there is a connections
configuration array. This option defines the connections to backend queue services such as 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:
Creating Jobs
Generating Job Classes
By default, all of the 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:
Class Structure
Job classes are very simple, containing two methods: Signature
, Handle
, Signature
is the unique identifier of the task, Handle
will be called when the queue processes the task, the []queue.Arg{}
passed when the task is called will be passed into Handle
:
Register Job
After creating the job, you need to register it on the app/provides/queue_service_provider.go
, so that it can be called correctly.
Start Queue Server
Start the queue server in main.go
in the root directory.
Different parameters can be passed in the facades.Queue().Worker
method, you can monitor multiple queues by starting multiple facades.Queue().Worker
.
Dispatching Jobs
Once you have written the job class, you can dispatch it using the dispatch
method on the job itself:
Synchronous Dispatching
If you want to dispatch a job immediately (synchronously), you can use the dispatchSync
method. When using this method, the job will not be queued and will be executed immediately within the current process:
Job Chaining
Job chaining allows you to specify a list of queued jobs that should be run in sequence. If one job in the sequence fails, the rest of the jobs will not be run. To execute a queued job chain, you can use the chain
method provided by the facades.Queue()
:
Delayed Dispatching
If you would like to specify that a job should not be immediately available for processing by a queue worker, you may use the Delay
method when dispatching the job. For example, let's specify that a job should not be available for processing until 10 minutes after it has been dispatched:
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.
Dispatching To A Particular Connection
If your application interacts with multiple queue connections, you can use the onConnection
method to specify the connection to which the task is pushed.
You may chain the onConnection
and onQueue
methods together to specify the connection and the queue for a job:
queue.Arg.Type
Supported Types
queue.Arg.Type
Supported TypesLast updated