Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Artisan Dispatchable Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Pros:
    • Aligns with Laravel’s native job queue system (ShouldQueue), reducing cognitive overhead for developers.
    • Enables hybrid execution (CLI + queue) for jobs, addressing the trade-off between synchronous (Artisan) and asynchronous (queue) workflows.
    • Leverages Laravel’s existing scheduler for orchestration while offloading heavy lifting to queues, improving scalability for time-sensitive tasks.
    • Minimal abstraction: Jobs remain standard Laravel classes, preserving existing patterns (e.g., middleware, retries, timeouts).
  • Cons:
    • No built-in concurrency control: Artisan commands are inherently sequential; dispatching via CLI bypasses queue parallelism unless explicitly managed (e.g., via dispatchSync or manual queue workers).
    • Scheduler limitations: If jobs are dispatched via Artisan in a scheduled task, they still block the scheduler’s execution thread (mitigated by queueing, but requires discipline).
    • No native event-driven triggers: Unlike pure queue-based systems, this package doesn’t natively support event-based job dispatching (e.g., from HTTP requests or observers).

Integration Feasibility

  • Low-risk for Laravel apps: Compatible with Laravel 10+ (based on release date) and follows Spatie’s reputation for maintainable packages.
  • Dependencies:
    • Requires Laravel’s queue system (database/Redis/SQS) and Artisan.
    • No external services or complex setup; single composer install suffices.
  • Testing:
    • Jobs can be tested via both Artisan (php artisan job:test) and queue workers (standard Laravel testing).
    • Mock Artisan::call() for unit tests covering CLI dispatch paths.

Technical Risk

  • Critical Risks:
    • Misalignment with scheduler expectations: Developers might forget to queue jobs when dispatching via Artisan, leading to scheduler delays. Mitigation: Enforce CI/CD checks or documentation mandates.
    • Queue worker starvation: If all jobs are dispatched via Artisan (not the queue), workers may idle. Mitigation: Use dispatch() in scheduled tasks where possible, or monitor queue length.
  • Moderate Risks:
    • Command-line vs. queue semantics: Jobs dispatched via Artisan may behave differently (e.g., no queue retries). Mitigation: Document this explicitly and use ShouldQueue consistently.
    • Error handling: Artisan errors (e.g., command not found) may not propagate to monitoring. Mitigation: Wrap Artisan calls in try-catch or use Laravel’s exception handling.
  • Minor Risks:
    • Namespace collisions: Custom Artisan commands might conflict with existing ones. Mitigation: Use unique job class names or prefixes.

Key Questions

  1. Orchestration Strategy:
    • How will jobs be triggered? Will the scheduler dispatch via Artisan, or will CLI calls be manual/automated (e.g., cron)?
    • Are there tasks where sequential execution (Artisan) is intentionally desired (e.g., setup scripts), or is this purely for queue offloading?
  2. Observability:
    • How will job execution (CLI vs. queue) be logged/monitored? Will Artisan dispatches appear in queue monitoring tools (e.g., Laravel Horizon)?
  3. Failure Modes:
    • What’s the recovery plan if Artisan dispatches fail (e.g., disk full, queue connection issues)? Will retries be handled by the queue or external systems?
  4. Performance:
    • What’s the expected runtime of jobs? If >1 minute, will Artisan dispatches cause scheduler backlogs?
  5. Team Adoption:
    • How will the team distinguish between "queue-only" jobs and "Artisan-dispatchable" jobs? Will this require new naming conventions or annotations?

Integration Approach

Stack Fit

  • Ideal Use Cases:
    • Long-running scheduled tasks: Offload work from the scheduler to queues (e.g., php artisan process-invoices → queues ProcessInvoiceJob).
    • Hybrid workflows: Combine CLI-driven jobs with queue-based retries/timeouts (e.g., php artisan generate-reports dispatches a queued job with a 24-hour timeout).
    • Migration from Artisan to queues: Gradually replace synchronous Artisan commands with queued jobs while maintaining CLI access for testing/development.
  • Anti-Patterns:
    • CPU-bound tasks: Artisan dispatches won’t benefit from queue workers; these should use Laravel’s process() or external services.
    • Real-time systems: If jobs require immediate feedback (e.g., user-facing actions), avoid Artisan dispatch entirely.

Migration Path

  1. Assessment Phase:
    • Audit existing Artisan commands to identify candidates for queue offloading (runtime >5s, I/O-bound, or blocking operations).
    • Tag jobs with ArtisanDispatchable and ShouldQueue in a feature branch.
  2. Pilot Phase:
    • Replace 1–2 critical scheduled tasks with queued jobs, dispatching via Artisan for testing.
    • Verify queue workers process jobs correctly and monitor for backlogs.
  3. Rollout Phase:
    • Update scheduler to dispatch jobs via dispatch() where possible; use Artisan only for manual triggers.
    • Deprecate old Artisan commands via @deprecated tags or alias them to the new queued workflows.
  4. Cleanup Phase:
    • Remove legacy Artisan commands post-adoption.
    • Update documentation to reflect the new hybrid pattern.

Compatibility

  • Laravel Version: Tested on Laravel 10+ (as of 2026). Backport to older versions may require adjustments (e.g., queue driver changes).
  • Queue Drivers: Compatible with all Laravel-supported drivers (database, Redis, SQS, etc.).
  • Artisan Extensions: Works alongside other Artisan packages (e.g., Laravel Zero) but may require namespace isolation.
  • Custom Job Classes: No restrictions; jobs can extend other interfaces (e.g., ShouldBeVeryVisible).

Sequencing

  1. Installation:
    composer require spatie/laravel-artisan-dispatchable
    
    Publish config if extending defaults (unlikely for basic use).
  2. Job Implementation:
    use Spatie\ArtisanDispatchable\Jobs\ArtisanDispatchable;
    
    class ExportUserData implements ShouldQueue, ArtisanDispatchable {
        public function handle() { ... }
    }
    
  3. Artisan Command Generation:
    • The package auto-generates commands (e.g., export-user-data). No manual command classes needed.
  4. Scheduler Update:
    // 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();
    

Operational Impact

Maintenance

  • Pros:
    • Reduced boilerplate: No need to maintain separate Artisan commands for queued jobs.
    • Consistent patterns: Jobs follow Laravel’s standard queue interface, easing onboarding.
  • Cons:
    • Debugging complexity: Artisan dispatches may obscure queue worker logs. Mitigation: Use QUEUE_LOG and QUEUE_CONNECTION env vars for visibility.
    • Package updates: Monitor Spatie’s releases for breaking changes (e.g., Laravel version drops).

Support

  • Developer Onboarding:
    • Ease: Low learning curve for Laravel devs familiar with queues.
    • Pitfalls: New hires may overlook the ArtisanDispatchable interface or assume Artisan dispatches are synchronous.
    • Mitigation: Add a README section on hybrid execution patterns and include examples in CI/CD templates.
  • Production Issues:
    • Common Symptoms:
      • Scheduler delays due to long-running Artisan dispatches.
      • Queue backlogs from unmonitored Artisan triggers.
    • Tools:
      • Use Laravel Horizon or queue:work --sleep=3 --tries=3 for visibility.
      • Set up alerts for failed_jobs table growth.

Scaling

  • Horizontal Scaling:
    • Queue workers scale independently of Artisan dispatches. Ensure queue workers are sized for peak load.
    • Artisan dispatches are single-threaded; avoid dispatching from high-traffic CLI tools (e.g., Tinker).
  • Vertical Scaling:
    • No direct impact, but monitor memory usage if jobs are memory-intensive (use QUEUE_WORKER_TIMEOUT).
  • Cost:
    • External queue services (e.g., SQS) may incur costs for Artisan-triggered jobs. Monitor usage patterns.

Failure Modes

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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport