How to use Laravel Queue with database

|
| By Webner

Purpose of Laravel Queue and How to use them with database

Page load time is an important aspect of any successful website, and one should not overlook the importance of that as it affects the SEO of the site and the overall end-user experience as well. So, there are different approaches you could use to rectify slow page load issue.

Queues allow you to defer the processing of a time-consuming task, such as sending an email (execute it in backend).

Today, we’re going to explore a similar concept in the context of the Laravel web framework. Laravel provides a unified API across a variety of different queue backends, such as Beanstalk, Amazon SQS, Redis, or even a relational database.

Here are the steps to use Laravel queues with the database.

1. First of all, change the driver from sync to database in a .env file.
2. To generate migration run this command that creates a job table in the database,

Run
queue:table artisan

Once the migration is completed you can migrate the table by using migrate command.
php artisan queue:table
php artisan migrate
3. Now, create a job, all the queueable jobs are stored in app/jobs
php artisan make:job Jobname–queued
This command creates a job directory in app directory with the jobname file.

4. Job classes are very simple, normally containing only a handle method which is called when the job is processed by the queue.

5. Pushing the jobs to the queue normally uses the dispatch method. You can use the dispatch method in controller where you want to add the jobs in queue.

Send dynamic data to the queue in base_64 encode and then decode it while using.
$data = [ ‘user_id’ => base64_encode(‘1’) ];
$response =Jobname::dispatch($data);
You can also delay the job by using delay method on your job class.
$response =JsonToArrayJob::dispatch($data) ->delay(now()->addSeconds(1));
return redirect()->back()->with(‘status’,’Your request is under processing’);

6. At last, write code in /app/jobname to execute.

<?php
// use the following file path and namespace  that are provided by the laravel on job create 
class JsonToArrayJob implements ShouldQueue{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
// To access dynamic variable that is passed at the time of job dispatch
       public $user_id;
        public function __construct($data){
       	$this->jsonfilename =$data['jsonfilename'];
    } 
    public function handle() {  
// Write code to execute   
          Use variable declare above in your code  like that $this->jsonfilename; 
  }

Jobs table with the dynamic data – screenshot below

use Laravel Queue with database

Run Queue from command prompt by using this command.
php artisan queue:work

To automatically run the queue install the supervisor and then configure it, create a file in the following directory

Install command :-    sudo apt-get install supervisor          
Directory to configure :-  /etc/supervisor/conf.d/laravel-worker.conf
Filename :-  laravel-worker.conf
<!----- Open   Code to add         --->
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/projectname/artisan queue:work
autostart=true
autorestart=true
user=webner
numprocs=8
redirect_stderr=true
stdout_logfile=/var/log/laraqueue.out.log       // log file
<!----- End  Code to add         --->

Commands to reload and update supervisor:

supervisorctl reread
supervisorctl update
supervisorctl restart

Leave a Reply

Your email address will not be published. Required fields are marked *