Queue Laravel Command Tutorial Example

  • 01-10-2022
  • 929
  • Laravel 9
  • Haresh Chauhan

The laravel queue provided services with a unified API across a variety all different queue backends. The queue allows you to reduce consumption time from long-take, such as email sending and mobile SMS sending, and the things that you want to do action after the response sent from the server. This will work as a backend after the services. The laravel queue provided queue services in building laravel applications with different connections and many various ways to use.

The queue in the laravel is very comfortable to use and easy to understand. sometimes handle a queue in laravel you must knowledge about some important commands to handle it. So in this tutorial, we will discuss some important queue commands.

Laravel queue, If you have a question like :

  • How to run the queue automatically?
  • How to run the queue?
  • How to restart the queue?
  • How to stop the queue?
  • How to timeout set in the queue?
  • How to sleep set in the queue?
  • How to attempts set in the queue?
  • How to set the connection settings in the queue?
  • How to set priority in the queue?

These are all the answers you will find in this post. apart from this, there are many questions included but this is enough to understand.

Starting Queue

Start queue job using the below command in your project terminal. This will start executing jobs from the server.

php artisan queue:work
Specifying Max Job Attempts / Timeout Values

When you queue in running mode, and if the queue job encounters any error, you likely don't want to keep it retrying indefinitely. therefore laravel provided specifically how many times as well as how long the job to be attempted.

php artisan queue:work --tries=3

Often, May take more time, the queue attempting the job event counters the error. It's taking too long to execute the job. You may specify the job-specific time out using --timeout=30, This will expire once the given time is over. The job stops working once the time expires.

php artisan queue:work --timeout=30
Worker Sleep Duration

When the jobs are available in the queue, If you want to delay in between jobs, add a sleep option. This will determine that each tome perform take delay or sleep between tow job time given. This queue will sleep for a given time after time is over it will start executing the next job.

php artisan queue:work --sleep=3
Specifying The Connection & Queue

You can specify queue connection worker should utilize. This connection name passed after the work command. also you can define in queue connection config file config/queue.php

php artisan queue:work redis

One more example of which queue connection utilize in the given command jobs. provided database connection after queue:work command. This will run the given connection job only.

php artisan queue:work database
Processing A Specified Number Of Jobs

The --once option command may specify to the workers, to attempt only the first jobs only.

php artisan queue:work --once   

You can also specify the --once option with the connection.

php artisan queue:work database --once

The --max-jobs option may be used instruct to workers to process the number of the job executed only then after exit. This option may use when you work with the Supervisor so that your workers restart after a given job attempt. This is useful in releasing your server memory consumption.

php artisan queue:work --max-jobs=1000
Specifying The Connection & Queue

By default queue:work command for whole connection jobs. But you can specify the queue name only that should execute. Menas while you insert your queue job that time you need to give the name of each queued job, Then after executing that given queue name only gives the --queue option. You can also specify the connection of the queue.

php artisan queue:work redis --queue=emails

This command uses when you are in development mode. So can your queue starts you may not have to restart your queue job? The made change auto effect to the queue when you start your queue using the queue:listen to the command. This live update code will be executed when the new jobs start work. The s command is less effective than the queue:work.

php artisan queue:listen
Processing All Queued Jobs & Then Exiting

The --stop-when-empty option may use the stop your jobs from executing after successfully done all the jobs. By default, the queue does not stop after doing all the jobs. So in case you want to stop the queue after doing all the jobs successfully, you can use this command. This command will exit after all the jobs are done.

php artisan queue:work --stop-when-empty

You can specify connection with the jobs exit after successfully executing all the jobs.

php artisan queue:work database --stop-when-empty

Sometimes you may wish to restart your queue jobs to make it more effective execution. If your change does not affect your queue, you can restart your queue using the below command.

php artisan queue:restart
keep Laravel Queue system running on server

Using the below command to keep running your queue even you close your server terminal. This will always stay alert even if you don't stay connected with your server terminal. This will always stay alert for an upcoming job.

nohup php artisan queue:work database --daemon > /dev/null 2>&1 & 
Queue Priorities

Sometimes you wish to prioritize queue jobs, So define your queue dispatch like the below given example and with their priorities name according to your word.

dispatch((new Job)->onQueue('high'));

Start queue work based on the priorities, The queue will start executing jobs according to your priorities high to lower.

php artisan queue:work --queue=high,low

Email Send Using Queue In Laravel

How To Configuration Laravel Supervisor


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :