Today I will going to show how to setup the job queue on Laravel 5.6, So first of all let me explain what is job queue?
“Queues allow you to defer the processing of a time consuming task, such as sending an email, until a later time. Deferring these time consuming tasks drastically speeds up web requests to your application.”
Job Queue is setup that laravel system have to run process in background on server without stopping / interrupting current execution. It is helping to run complex time consuming tasks.
So that the process or code will run on server side with specified site. So it will improve site speed.
Let us say you are having one site, it is having mail for new user registered user. And once user registered with site mail will be send to their address.
It will take little time while user registering with site as mail will take 4-5 seconds to send an email.
So user will experience the slowness of site, to solve this issue laravel job queue can be used, like say once user is registered successfully with site, then one new job process will be created and it will be executed in background without affection of current execution.
There are several steps involved in setup the job queue.
Step 1: Queue Configuration
The queue configuration file is stored in config/queue.php. In this file you will find connection configurations for each of the queue drivers that are included with the framework, which includes a database, Beanstalkd, Amazon SQS, Redis, and a synchronous driver that will execute jobs immediately (for local use). A null queue driver is also included which discards queued jobs.
Here I am providing database as queue driver setting up option.
In .env file add / edit following value
As it has sync as by default value in config/queue.php file.
Note: there are many other drivers are available see the following doc for more reference:
Step 2: Setting up the database table for queue
So run following command to create queue table schema migration.
“php artisan queue:table”
“php artisan migrate” to run migration for queue table.
Step 3: Create a job
To create a job there you can run
“php artisan make:job ProcessPodcast”, that will create a new job file under ‘app\Jobs’ directory.
It will have following file contents
The ‘handle’ method is called when the job is processed by the queue. Note that we are able to type-hint dependencies on the handle method of the job. The Laravel service container automatically injects these dependencies.
Step 4: Dispatch the job
Once you have written your job class, you may dispatch it using the ‘dispatch’ method on the job itself. The arguments passed to the dispatch method will be given to the job’s constructor:
From your controller file you can dispatch job by adding
Step 5: Process queue
To process created job queue need to run following command
php artisan queue:work --tries=3
One approach to specifying the maximum number of times a job may be attempted is via the
--tries switch on the Artisan command line:
You can also specify try to job file
public $tries = 5; like this way.
Once you have written your job class, you may dispatch it using the ‘dispatch’ method on the job itself. The arguments passed to the dispatch method will be given to the job’s constructor.
Alternate of step 5: Setup up the Supervisor
If you have live server then it is recommended that you should setup the supervisor on server and it will automatically run queue worker.
To configure supervisor there are below steps involved
- i) Installing Supervisor
“sudo apt-get install supervisor”command.
- ii) Configuring Supervisor
Supervisor configuration files are typically stored in the /etc/supervisor/conf.d directory. Within this directory, you may create any number of configuration files that instruct supervisor how your processes should be monitored. For example, let’s create a laravel-worker.conf file that starts and monitors a queue:work process:
In this example, the numprocs directive will instruct Supervisor to run 8 queue:work processes and monitor all of them, automatically restarting them if they fail. Of course, you should change the queue:work sqs portion of the command directive to reflect your desired queue connection.
iii) Starting Supervisor
Once the configuration file has been created, you may update the Supervisor configuration and start the processes using the following commands:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
For more information on Supervisor, consult the Supervisor documentation.
Step 6: Dealing With Failed Jobs
Sometimes your queued jobs will fail. Don’t worry, things don’t always go as planned! Laravel includes a convenient way to specify the maximum number of times a job should be attempted. After a job has exceeded this amount of attempts, it will be inserted into the failed_jobs database table. To create a migration for the failed_jobs table, you may use the queue:failed-table command:
php artisan queue:failed-table
php artisan migrate
Then, when running your queue worker, you should specify the maximum number of times a job should be attempted using the –tries switch on the queue:work command. If you do not specify a value for the –tries option, jobs will be attempted indefinitely:
php artisan queue:work redis --tries=3
Job queue is very useful in system like if you have large amount of data on one server and want to transfer using API to another server, then it is highly recommended to use job queue.