Advanced

Batches

Introduction

Through this feature you can monitor the assign jobs. This job section will show you the records of completed jobs only. In this section we can see progress of the Batches of jobs like if batch have failed or batch has completed executing or when has this batch completed or what percentage of the batch has completed etc

Visit following url you will see the Batches section:

<public-url>/backend#/vaah/advanced/batches

Below is an example of Batch records.

Usage

Bulk delete

By selecting checkbox present at starting of each row one can delete the records. Also there is delete all option for make it empty.

delete by selecting

delete all

Sorting

You can sort records by ID and with the date it was created.

Details

In the details column information related to the batch can be seen by clicking on view button.

Step-1 Create Batch Table

  • Jobs can be batched in order to be executed in bulk.
  • queue:batches-table helps you to create migration file for batching.
php artisan queue:batches-table

create table in database

php artisan migrate

Batch table will contain meta information about your job batches, such as their completion percentage.

Step-2 Create Batch Class

To make a Job Batchable use

use Illuminate\Bus\Batchable;

Imports the job class.

<?php
 
namespace App\Jobs;
 
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
 
class ImportCsv implements ShouldQueue
{
    use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if ($this->batch()->cancelled()) {
            // Determine if the batch has been cancelled...
 
            return;
        }
 
        // Import a portion of the CSV file...
    }
}

Step-3 Create a Batch of Jobs

  • Include use Illuminate\Support\Facades\Bus; to use batch() function.
  • Call your jobs in side the batch() function. And these jobs will add into batches.
$batch = Bus::batch([
    new MatchSendEmail(),
    new MatchSendEmail(),
    new MatchSendEmail(),
])->then(function (Batch $batch) {
    // All jobs completed successfully...
})->catch(function (Batch $batch, Throwable $e) {
    // First batch job failure detected...
})->finally(function (Batch $batch) {
    // The batch has finished executing...
})->dispatch();
 
return $batch->id;

Visit following url to see the added batches in table

<public-url>/backend#/vaah/advanced/batches

Step-4 Run batches

  • Batches are group of jobs for run batches you need to run jobs.

Run jobs

php artisan queue:work

or

php artisan queue:work --queue=high,medium,low,default --env=env_filename

If you want to run without cache use following command:

php artisan queue:listen --queue=high,medium,low,default --env=env_filename
php artisan queue:listen --queue=high,medium,low,default --env=develop

Copyright © 2024