spatie/laravel-artisan-dispatchable
Register Laravel jobs as Artisan commands by implementing the ArtisanDispatchable interface. Dispatch queued jobs via CLI (e.g., php artisan process-podcast) so long-running tasks won’t block the scheduler, while remaining runnable from Artisan.
ShouldQueue), reducing cognitive overhead for developers.dispatchSync or manual queue workers).php artisan job:test) and queue workers (standard Laravel testing).Artisan::call() for unit tests covering CLI dispatch paths.dispatch() in scheduled tasks where possible, or monitor queue length.ShouldQueue consistently.php artisan process-invoices → queues ProcessInvoiceJob).php artisan generate-reports dispatches a queued job with a 24-hour timeout).process() or external services.ArtisanDispatchable and ShouldQueue in a feature branch.dispatch() where possible; use Artisan only for manual triggers.@deprecated tags or alias them to the new queued workflows.ShouldBeVeryVisible).composer require spatie/laravel-artisan-dispatchable
Publish config if extending defaults (unlikely for basic use).use Spatie\ArtisanDispatchable\Jobs\ArtisanDispatchable;
class ExportUserData implements ShouldQueue, ArtisanDispatchable {
public function handle() { ... }
}
export-user-data). No manual command classes needed.// Before: Sequential Artisan call
$schedule->command('export-user-data')->daily();
// After: Queued job dispatch
$schedule->job(new ExportUserData)->daily();
Note: For hybrid cases, keep the Artisan command but ensure it dispatches the job:
// In Artisan command (if needed)
ExportUserData::dispatch();
QUEUE_LOG and QUEUE_CONNECTION env vars for visibility.ArtisanDispatchable interface or assume Artisan dispatches are synchronous.README section on hybrid execution patterns and include examples in CI/CD templates.queue:work --sleep=3 --tries=3 for visibility.failed_jobs table growth.QUEUE_WORKER_TIMEOUT).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Artisan command not found | Job fails silently | Use Artisan::queue() with fallback or wrap in try-catch. |
| Queue connection drops | Artisan dispatch hangs | Implement exponential backoff in Artisan commands. |
How can I help you explore Laravel packages today?