Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

A Laravel package to dispatch jobs via Artisan

Original – by Freek Van der Herten – 3 minute read

Our team has released a new Laravel package: spatie/laravel-artisan-dispatchable. This package allows you to dispatch any job from an artisan command.

In this blog post, I'd like to explain why we created the package and how you can use it.

Why we created this package

Laravel's scheduler will perform all tasks sequentially. When you add a scheduled task to the scheduler, the task should perform its work as fast as possible, so no other tasks will have to wait.

If you have a task that needs to run every minute and its runtime is close to a minute, you should not use a simple Artisan command, as this will result in the delay of all other minute-ly tasks.

Long-running tasks should be performed by jobs that perform their work on the queue. Laravel has the ability to schedule queued jobs. This way, those tasks will not block the scheduler.

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

The downside of this approach is that you cannot run that job via Artisan anymore. You have to choose between using an artisan command + blocking the scheduler on the one hand, and job + not blocking the scheduler on the other hand.

Using our package, you don't have to make that choice anymore. When letting your job implement Spatie\ArtisanDispatchable\Jobs\ArtisanDispatchable, you will not block the scheduler and can still execute the logic via Artisan.

How to make jobs dispatchable via Artisan

All you need to do is let your job implement the empty ArtisanDispatchable interface.

use Spatie\ArtisanDispatchable\Jobs\ArtisanDispatchable;

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

This allows the job to be executed via Artisan using the kebab-case version of the base class name.

php artisan process-podcast

This job will not be queued but will be immediately executed inside the executed artisan command.

If you want to put your job on the queue instead of executing it immediately, add the queued option.

php artisan process-podcast --queued

If your job has constructor arguments, you can still use the package. Constructor arguments can be passed as options to the artisan command. If a constructor argument is an Eloquent model, you may pass the model's id to the artisan command option.

Here's an example:

use App\Models\Podcast;
use Spatie\ArtisanDispatchable\Jobs\ArtisanDispatchable;

class ProcessPodcast implements ArtisanDispatchable
{
    public function __construct(
        Podcast $podcast, 
    ) {}

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

Here's how you can execute this job with podcast id 1234

php artisan process-podcast --podcast=1234

In closing

This package has a few other whistles and bells, which are explained in the package's readme on GitHub.

This isn't the first package our team has created. Check our website for an extensive list of things we released previously.

If you want to support our open source work, consider picking up one of our premium products and courses.

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

Webmentions

Peter Brinck 🤘 liked on 15th June 2021
Karel liked on 15th June 2021
Karel retweeted on 15th June 2021
M. Vug liked on 15th June 2021
Sadegh 🐘 liked on 15th June 2021
Indiealistic liked on 15th June 2021
Omar liked on 15th June 2021
Khawar Hussain liked on 15th June 2021
Alex liked on 15th June 2021
René Sinnbeck liked on 15th June 2021
ketsakda liked on 15th June 2021
Philippe Pérusse liked on 15th June 2021
Denis Nikolas liked on 15th June 2021
Salman Zafar liked on 15th June 2021
Ali Mahdavi liked on 14th June 2021
Eimer liked on 14th June 2021
🇵🇸 Omar Khairy liked on 14th June 2021
Alexander Maiorov liked on 14th June 2021
Lars Schou 🇩🇰 liked on 14th June 2021
Oilmone liked on 14th June 2021
Hashman.Hashimoto liked on 14th June 2021
🇵🇸 Othman Jr. liked on 14th June 2021
dian liked on 14th June 2021
Filipe Danielski 🐘 liked on 14th June 2021
Bilal Khalid liked on 14th June 2021
Michael Rimbach liked on 14th June 2021
J.O.Y liked on 14th June 2021
Willan Correia liked on 14th June 2021
Tom Schlick liked on 14th June 2021
/dev/faisal liked on 14th June 2021
Aniket Mahadik 🔭 retweeted on 14th June 2021
José Cage retweeted on 14th June 2021
Tadhg Boyle 🪐 liked on 14th June 2021
Neeraj Tangariya liked on 14th June 2021
José Cage liked on 14th June 2021
Mohan Raj liked on 14th June 2021
Mason liked on 14th June 2021
Aniket Mahadik 🔭 liked on 14th June 2021
Michael Ishola liked on 14th June 2021
Jānis Lācis liked on 14th June 2021
Scorp974 liked on 14th June 2021
Stephen Neal liked on 14th June 2021
Λlex Wulf 🇪🇺 replied on 14th June 2021
You may also take a look at laravelactions.com. Solves a similar usecase. ☺️
Mark Topper liked on 14th June 2021
Ronnie Visser replied on 14th June 2021
What’s the difference in case you run scheduled jobs in the background? Other tasks aren’t waiting on it in that case isn’t it?
Spatie retweeted on 14th June 2021
Ali Ali liked on 14th June 2021
Sanjeeva retweeted on 14th June 2021
David Foxx Sterling liked on 14th June 2021
Jordi 🎗 liked on 14th June 2021
Zacchaeus Bolaji liked on 14th June 2021
João Alves liked on 14th June 2021
Idris liked on 14th June 2021
Darkhej Master liked on 14th June 2021
I’ve explained our use case in the blogpost 👍
Marco liked on 14th June 2021