Download the PHP package nguyenhiep/laravel-artisan-dispatchable without Composer
On this page you can find all versions of the php package nguyenhiep/laravel-artisan-dispatchable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nguyenhiep/laravel-artisan-dispatchable
More information about nguyenhiep/laravel-artisan-dispatchable
Files in nguyenhiep/laravel-artisan-dispatchable
Package laravel-artisan-dispatchable
Short Description Dispatch Laravel jobs via Artisan
License MIT
Homepage https://github.com/spatie/laravel-artisan-dispatchable
Informations about the package laravel-artisan-dispatchable
Dispatch Laravel jobs via Artisan
This package can register jobs as Artisan commands. All you need to do is let your job implement the empty ArtisanDispatchable
interface.
This allows the job to be executed via Artisan.
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.
`
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.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
Optionally, you can publish the config file with:
This is the contents of the published config file:
Usage
All you need to do is let your job implement the empty ArtisanDispatchable
interface.
This allows the job to be executed via Artisan.
This job will not be queued, but will be immediately executed inside the executed artisan command.
Queueing jobs via Artisan
If you want to put your job on the queue instead of executing it immediately, add the queued
option.
Passing arguments to a job
If your job has constructor arguments, you may pass those arguments via options on the artisan command.
Via artisan, you can call the job like this
Using Eloquent models as arguments
If your job argument is an eloquent model, you may pass the id of the model to the artisan command option.
Here's how you can execute this job with podcast id 1234
Customizing the name of the command
By default, the artisan command name of a job, is the base name of job in kebab-case.
You can set a custom name by setting a property named artisanName
on your job.
This job can now be executed with this command:
Customizing the description of the command
To add a description to the list of artisan commands, add a property $artisanDescription
to your job.
Prefixing all commands
You can specify a prefix in the command_name_prefix
key of the config file. When this is for example set to my-custom-prefix
, then you would be able to call MyDispatchableJob
with this command:
Caching discovered jobs
This package can automatically discover jobs that implement ArtisanDispatchable
and what their artisan command should be through looping through all classes and performing some reflection. In a local environment this is perfect, as the performance hit is not too bad, and you don't have to do anything special besides letting your job implement ArtisanDispatchable
.
In a production environment, you probably don't want to loop through all classes on every request. The package contains a command to cache all discovered jobs.
You probably want to call that command during your deployment of your app. This will create cache file at the location specified in the cache_file
key of the artisan-dispatchable
config file.
Should you want to clear the cache, you can execute this command:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Freek Van der Herten
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-artisan-dispatchable with dependencies
spatie/laravel-package-tools Version ^1.4.3
spatie/phpunit-snapshot-assertions Version ^4.2