Wondering Coder
Wondering Coder

Reputation: 1702

How to execute a job immediately using Laravel Queue

I have a sign up form which sends an email notification to the user email and I used Laravel Queue for all my background process (e.g. Mail::queue...).

My question is, say I have a 20 queued jobs in my table. And I always want to prioritize all email invitations first, I don't want it to wait. Is this possible? I still want to use the queue principle though.

Upvotes: 1

Views: 8979

Answers (2)

Sahil Jain
Sahil Jain

Reputation: 471

You can also use sync dispatching to execute job immediately if don't want to queue jobs.

From Docs - https://laravel.com/docs/9.x/queues.
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 executed immediately within the current process:

ProcessPodcast::dispatchSync($podcast);

Having multiple queues for different kind of workflows is a way to prioritise work and execute asynchronously but it won't be immediate since those queues will be having multiple jobs pending at some point. Synchronous dispatching isn't recommended for a complex job work as it will block request and thread.

Upvotes: 2

Jirennor
Jirennor

Reputation: 1289

You could setup multiple queues. For example one for background jobs with low priorities and one for email with a high priority.

See this link from the Laravel documentation regarding assign jobs to specific queues. Pushing Jobs Onto The Queue. Look into 'Specifying The Queue For A Job'.

After you have done setting this up you can assigning priorities to the queues. See the following link to the Laravel documentation. Running The Queue Listener. Look into 'Queue Priorities'.

Quote from Laravel documentation:

In this example, jobs on the high queue will always be processed before moving onto jobs from the low queue.

Upvotes: 3

Related Questions