Showing posts with label Queue. Show all posts
Showing posts with label Queue. Show all posts

Saturday, August 14, 2021

Integration of Queue in Laravel

Integration of Queue in Laravel

Introduction

Laravel Queue is to enhances the laravel application performance and provides a smooth process to do your heavy tasks without the hassle and waiting time.

For example like if you have Lacs of records and wanted to import them into the database in laravel you just implement the import feature and upload the CSV or excel file and import it but it will take mins or hours to import in the database and your application will stop working until all the records not imported in the database 

Laravel gives us a queue system that helps to run complex tasks in the background without stopping the application it just takes the time to execute the request not wait for the response and you will continue your work without waiting.

Configuration of Queue

Let’s take an example for a better understanding

Go to your laravel application folder and run the below command :

php artisan queue:table php artisan queue:failed-table

It will generate the below migrations for the queue-related tables.

Jobs table  : 

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateJobsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('jobs', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('queue')->index();
            $table->longText('payload');
            $table->unsignedTinyInteger('attempts');
            $table->unsignedInteger('reserved_at')->nullable();
            $table->unsignedInteger('available_at');
            $table->unsignedInteger('created_at');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('jobs');
    }
}
 

Failed Jobs Table : 

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateFailedJobsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('failed_jobs', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->text('connection');
            $table->text('queue');
            $table->longText('payload');
            $table->longText('exception');
            $table->timestamp('failed_at')->useCurrent();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('failed_jobs');
    }
}

Run the below command to migrate above migrations and generate tables

php artisan migrate

Once the migrate command is executed, you will find the “jobs” and “failed_jobs” tables in your database.

After the migration, the next step is to change the environment file .env to set the queue driver like below : 

QUEUE_CONNECTION = database 

Now need to create a queue job and for that need to run the below command 

php artisan make:job csvImport

You will get the job file in the folder app/Jobs named csvImport.php. The file has just 1 method handle() in it. 

The file contains below code : 

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class csvImport implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
            
    }
}

You can add the logic to run through the job in the handle method

Now after setup the jobs you need to add them in the controller like below 

use App\Jobs\csvImport;

and in the controller method, you need to call the job through the below method to dispatch the job in the queue

It will add the job in the default queue : 

 csvImport::dispatch();

It will add the job in the default queue :

csvImport::dispatch()->onQueue('Customers');

We can use multiple queue names for multiple operations to differentiate it

After all the above steps just call your controller method and it will add your request to the queue and you can run the below command to execute all queues and there available jobs

For Example : 

php artisan queue:work –tries=5

“--tries” is used to defines the number of tries to run the job

Conclusion : 

Laravel queue is very easy to implement and use in your existing project, Also In case you feel that it is a daunting task to implement a laravel queue, you can reach me at “mp.inviable@gmail.com” to help you with the development process.





Friday, August 13, 2021

Integration of Supervisor in Laravel

Integration of Supervisor in Laravel


What is Supervisor in Laravel?


The supervisor is a process manager which Laravel suggests to use as a process monitor for queue workers. It will automatically start the queue worker in the background, even after the system has booted and will automatically restart the worker if the worker exits unexpectedly.
 
Why use it?

To automatically detect the jobs and process it as per the configuration. The supervisor is a client/server system that allows its users to monitor and control a number of processes.

How to use supervisor in Laravel?

Install supervisor by using the following command : 

sudo apt-get install supervisor 

After you have successfully installed supervisor, you will see a folder "supervisor" inside /etc/ with below mentioned file and folder.

supervisord.conf and conf.d folder 

If folder is not created, run this command: 

echo_supervisord_conf > /etc/supervisord.conf 

Now go to supervisor directory and run the command s below to add the configuration. 

touch laravel-worker.conf 

Now open laravel-worker.conf file and enter the configuration as per your requirements 

[program:laravel-worker] 
process_name=%(program_name)s_%(process_num)02d 
command=php <Laravel_Folder_Path>/artisan queue:work --tries=3 
autostart=true 
autorestart=true 
user=root 
numprocs=8 
redirect_stderr=true 
stdout_logfile=/var/log/supervisor/laravel-queue.log 

Save the file and scroll to the end of supervisord.conf in /etc/supervisor/supervisord.conf and change the path as mentioned below (if not available) 

files = /etc/supervisor/laravel-worker.conf 

 Now, Let's Start Supervisor to run the queue worker but before that lets check if supervisor is already running or not with the command below. 

 use ps -ef to check PID(process ID) and kill to finish the task. 

 Run : supervisord -c /etc/supervisor/supervisord.conf 

Run the following commands one by one after starting supervisor. 

sudo supervisorctl reread -- Restart all programs in configuration files 
sudo supervisorctl update -- Update configurations to supervisord 
sudo supervisorctl start laravel-worker:* -- Start a program 

 To check the status run the command: supervisorctl status

Integration of Queue in Laravel

Integration of Queue in Laravel Introduction Laravel Queue is to enhances the laravel application performance and provides a smooth proc...