Blog Detail

15

Jul
How to Dispatch Laravel Jobs Via Artisan Packages? cover image

arrow_back How to Dispatch Laravel Jobs Via Artisan Packages?

Spatie/laravel-artisan-dispatchable is a package that can enroll jobs as Artisan commands. The approach this package works is that you let your job implement the empty “ArtisanDispatchable” interface.

Why use this package?

When you are using Laravel’s scheduler then you’ve to append a scheduled task to the scheduler. The task should execute its work as fast as possible, so other tasks will not have to wait.

Suppose If there is a task that you need to run every minute and its runtime is close to a minute, then you should not utilize a simple Artisan command because this will end in the delay of all other minute-ly tasks. Lengthy tasks should be conducted by jobs that accomplish their work on the queue. Laravel can schedule queued jobs. Hence, those tasks will not prevent the scheduler.

$schedule->job(new ProcessPodcast)->everyFiveMinutes();

This approach has the drawback that you cannot execute that job via Artisan anymore. You have to decide between employing ( an artisan command + blocking the scheduler ) on the one hand, and (job + not blocking the scheduler ) on the other hand. But this package has resolved this problem. Now, you don’t have to make that decision anymore. When allowing your job to implement Spatie\ArtisanDispatchable\Jobs\ArtisanDispatchable, you will not block the scheduler and can still run the logic via Artisan.

Installation

You can execute this command for installation.

composer require spatie/laravel-artisan-dispatchable

Optionally, you can publish the config file by running this command.

php artisan vendor:publish --provider="Spatie\ArtisanDispatchable\ArtisanDispatchableServiceProvider" --tag="artisan-dispatchable-config"

Usage

use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\ArtisanDispatchable\Jobs\ArtisanDispatchable;

class ProcessPodcast implements ShouldQueue, ArtisanDispatchable
{
    public function handle()
    {
        // perform some work...
    }
}

The pleasing part about this package is that it appears with precepts that enable your job to be available to artisan without any extra work, using the kebab-case of the class name:

// Enables your job to be executed via Artisan.
php artisan process-podcast

// Put your job on the queue instead of performing immediately
php artisan process-podcast --queued


// Execute this job with podcast id
php artisan process-podcast --podcast="1234"

Customizing the commands

Although this package has conventions of command names, It permits you to override both the command name and description. If you wanted to add a description to the list of artisan commands, you’ve to add a property $artisanDescription to your job.

use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\ArtisanDispatchable\Jobs\ArtisanDispatchable;

class ProcessPodcast implements ShouldQueue, ArtisanDispatchable
{
    public string $artisanDescription = 'This a custom description';

    public function handle()
    {
        // perform some work...
    }
}

If you wanted to add a prefix in the command name. Then you’ve to define a prefix in the command_name_prefix key of the config file. If you set it to my-custom-prefix, then you would be ready to call MyDispatchableJob with this command:

php artisan my-custom-prefix:process-my-podcast

If you are interested to learn more about this package, You can get its full installation instructions and source code on GitHub. laravel-artisan-dispatchable

Published at : 15-07-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project