Showing posts with label Integration Of Queue In Laravel. Show all posts
Showing posts with label Integration Of Queue In Laravel. 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.





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...