What is the queue and why use the queue
- Queue means whatever comes first go out first means first in first out is called the queue. It maintains the sequence.
- When the task is time consuming or long running task like send emails to all users, import csv to database, multiple images upload in the server etc. So we are using the queue and jobs.
- Laravel also connects queues with several drivers like database, Redis, Amazon,Beanstalkd etc. The Drives are used to store the list of tasks.
- Here we implement the demo using the database queue driver.
Configure the queue
- After setting up the laravel project first in the config folder queue.php file there. In this file connection array queue is configure.
- In this connection array we using the queue for database and table=> jobs jobs table is created and retry_after=>90 if any queue is failed so after 90 second it retry and the queue => ‘default’ is the name of the queue
- After in the terminal fire the command php artisan queue:table so the one migration file is created like 2022_05_17_051432_create_jobs_tbl_table.php
- After run the php artisan migrate command to migrate the jobs table.
- Here all the queue configuration is done.
- After configure the queue we create the jobs using the php artisan make:job JobName.
- The jobs is created under the app/Jobs folder and the job is must be implement the shouldqueue interface and also use the trait file Dispatchable, InteractsWithQueue, Queueable, SerializesModels in queue class.
- In this created queue there are __construct and handle functions. Handle function we are writing the over logic same as the controller function like and here if we want to pass the data so we use the pass the argument to the constructor.
Dispatching the Jobs
There are Two method to dispatch the jobs
- using the dispatch global helper like dispatch(new JobName);
- using the dispatch static method like JobName::dispatch();
- Job is run on the background so we can not see and browser response is very fast.
- If we are dispatching the job by giving the particular time menas giving the some delay into over jobs so JobName::dispatch()->deley(now()->addSeconds(20)); so the job will be execute after the 20 second and if not passing the any second by default job will execute the 1 second.
- If we are passing the some data into the job so we in the JobName::dispatch($some_data) and the job class pass the variable into the __constuct method if not pass then show the error.
- And in the terminal fire the command php artisan queue:work otherwise the jobs is not run and not getting the output.
If any jobs will be fail so in the failed_jobs table exception column show why the jobs are fail in the below image.
In the terminal display the failed jobs like below:-
- If we want to remove all failed jobs in failed_jobs table so we fire the php artisan queue:flush so it remove the all failed jobs otherwise when fire the php artisan queue:work command it will run the again the failed job.
- Laravel by default 60 second maximum execution time provide so if the task time take more then in the jobs class we add the property like public $timeout = 1200; so the jobs will not be terminated.
- After all jobs run successfully we need to inform the user or send message like your csv files data insert successfully.
NOTE: when we change the jobs code so every time we need to clear the cache otherwise it run from the cache and display the error.
php artisan config:cache
php artisan cache:clear
or php artisan o:c (optimize the cache)
- Laravel queues are very useful to run the application smoothly.
- In our application the task is heavy and it takes time until the user can not wait for the task to be completed. Here it allows the user to perform any other task and the process is done without the user interrupt and background.
- When the task is very heavy (ex. upload 1 million records into the database) create big jobs into smaller and create the job batching.
- You can download the one demo code for creating send email to users when add the post click here to download. Hope this blog is helpful for a basic understanding of Laravel queues and jobs.
Share via social media