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(<span class="hljs-keyword">new</span> 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 SpatieArtisanDispatchableJobsArtisanDispatchable
, 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.
<span class="hljs-keyword">use</span> <span class="hljs-title">Spatie</span><span class="hljs-title">ArtisanDispatchable</span><span class="hljs-title">Jobs</span><span class="hljs-title">ArtisanDispatchable</span>;
<p><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ProcessPodcast</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">ArtisanDispatchable</span>
</span>{
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handle</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-comment">// perform some work...</span>
}
}
</p>
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:
<span class="hljs-keyword">use</span> <span class="hljs-title">App</span><span class="hljs-title">Models<span class="hljs-title">Podcast;
<span class="hljs-keyword">use</span> <span class="hljs-title">Spatie</span><span class="hljs-title">ArtisanDispatchable<span class="hljs-title">Jobs<span class="hljs-title">ArtisanDispatchable;
<p><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ProcessPodcast</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">ArtisanDispatchable</span>
</span>{
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">(
Podcast $podcast,
)</span> </span>{}</p>
<pre><code><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handle</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-comment">// perform some work...</span>
}
}
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.