Overview
Queue means whatever comes first goes 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 sending emails to all users, importing CSV to the 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.
Topics
- Configure the queue and jobs
- Jobs
- Dispatching the job
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 seconds 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.
Jobs
- After configuring the queue we create the jobs using the php artisan make:job JobName.
- The jobs are created under the app/Jobs folder and the job must implement the shouldqueue interface and also use the trait file Dispatchable, InteractsWithQueue, Queueable, SerializesModels in the queue class.
- In this created queue there are __construct and handle functions. Handle function we are writing the over logic the same as the controller function 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 in the background so we can not see and browser response is very fast.
- If we are dispatching the job by giving a particular time means giving the some delay into over jobs so JobName::dispatch()->deley(now()->addSeconds(20)); so the job will be executed after the 20 seconds and if not passing the any second by default job will execute the 1 second.
- If we are passing 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 failing in the below image.
- In the terminal display the failed jobs like the below:-
- If we want to remove all failed jobs in failed_jobs table so we fire the php artisan queue:flush so it removes all failed jobs otherwise when fire the php artisan queue:work command it will run the again failed job.
- Laravel by default 60 second a maximum execution time provide so if the task time takes more than 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 a 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)
Conclusion
- 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.