home / skills / fusengine / agents / laravel-queues

This skill helps you implement Laravel queue patterns with jobs, batches, chains, and middleware to improve reliability and async processing.

npx playbooks add skill fusengine/agents --skill laravel-queues

Review the files below or copy the command above to add this skill to your agents.

Files (16)
SKILL.md
4.9 KB
---
name: laravel-queues
description: Implement background jobs with queues, workers, batches, chains, middleware, and failure handling. Use when processing async tasks or handling long-running operations.
versions:
  laravel: "12.46"
  horizon: "5.43"
  php: "8.5"
user-invocable: false
references: references/jobs.md, references/dispatching.md, references/workers.md, references/batching.md, references/chaining.md, references/middleware.md, references/failed-jobs.md, references/horizon.md, references/testing.md, references/troubleshooting.md, references/templates/QueueableJob.php.md, references/templates/BatchJob.php.md, references/templates/ChainedJobs.php.md, references/templates/JobMiddleware.php.md, references/templates/JobTest.php.md
related-skills: laravel-architecture, laravel-eloquent
---

# Laravel Queues

## Agent Workflow (MANDATORY)

Before ANY implementation, use `TeamCreate` to spawn 3 agents:

1. **fuse-ai-pilot:explore-codebase** - Analyze existing job patterns
2. **fuse-ai-pilot:research-expert** - Verify Queue docs via Context7
3. **mcp__context7__query-docs** - Check job and worker patterns

After implementation, run **fuse-ai-pilot:sniper** for validation.

---

## Overview

| Component | Purpose |
|-----------|---------|
| **Jobs** | Background tasks with retries, timeouts |
| **Workers** | Process jobs from queues |
| **Batches** | Group jobs with progress tracking |
| **Chains** | Sequential job execution |
| **Middleware** | Rate limiting, deduplication |
| **Horizon** | Redis queue monitoring dashboard |

---

## Decision Guide: Queue Driver

```
Which driver?
├── Development → sync (instant execution)
├── Small app → database (simple, no Redis)
├── Production → redis (fast, Horizon support)
├── AWS → sqs (managed, scalable)
└── High volume → redis + Horizon (monitoring)
```

---

## Decision Guide: Job Design

```
Job type?
├── Simple async → Standard Job
├── Group processing → Batch (progress, cancel)
├── Sequential steps → Chain (A → B → C)
├── Rate limited → Middleware + RateLimiter
├── Unique execution → UniqueJob / WithoutOverlapping
└── Long running → Timeout + Retry settings
```

---

## Critical Rules

1. **Use ShouldQueue** for async processing
2. **Set tries and backoff** for resilience
3. **Implement failed()** method for error handling
4. **Use database transactions** carefully with jobs
5. **Monitor with Horizon** in production

---

## Reference Guide

### Concepts

| Topic | Reference | When to Consult |
|-------|-----------|-----------------|
| **Jobs** | [jobs.md](references/jobs.md) | Creating job classes |
| **Dispatching** | [dispatching.md](references/dispatching.md) | Sending jobs to queues |
| **Workers** | [workers.md](references/workers.md) | Running queue workers |
| **Batching** | [batching.md](references/batching.md) | Grouping jobs |
| **Chaining** | [chaining.md](references/chaining.md) | Sequential jobs |
| **Middleware** | [middleware.md](references/middleware.md) | Rate limiting, dedup |
| **Failed Jobs** | [failed-jobs.md](references/failed-jobs.md) | Error handling |
| **Horizon** | [horizon.md](references/horizon.md) | Monitoring dashboard |
| **Testing** | [testing.md](references/testing.md) | Job testing |
| **Troubleshooting** | [troubleshooting.md](references/troubleshooting.md) | Common issues |

### Templates

| Template | When to Use |
|----------|-------------|
| [QueueableJob.php.md](references/templates/QueueableJob.php.md) | Standard job with retries |
| [BatchJob.php.md](references/templates/BatchJob.php.md) | Batchable job |
| [ChainedJobs.php.md](references/templates/ChainedJobs.php.md) | Job chain implementation |
| [JobMiddleware.php.md](references/templates/JobMiddleware.php.md) | Custom middleware |
| [JobTest.php.md](references/templates/JobTest.php.md) | Testing jobs |

---

## Quick Reference

### Basic Job

```php
final class ProcessOrder implements ShouldQueue
{
    use Queueable;

    public int $tries = 3;
    public int $backoff = 60;
    public int $timeout = 120;

    public function __construct(
        public readonly Order $order,
    ) {}

    public function handle(OrderService $service): void
    {
        $service->process($this->order);
    }

    public function failed(\Throwable $e): void
    {
        Log::error('Order failed', ['id' => $this->order->id]);
    }
}
```

### Dispatch

```php
// Immediate
ProcessOrder::dispatch($order);

// Delayed
ProcessOrder::dispatch($order)->delay(now()->addMinutes(5));

// On specific queue
ProcessOrder::dispatch($order)->onQueue('orders');
```

---

## Best Practices

### DO
- Use `final` for job classes
- Implement `failed()` method
- Set appropriate `timeout` values
- Use `Unique` for one-at-a-time jobs
- Monitor with Horizon in production

### DON'T
- Dispatch inside database transactions (use `afterCommit`)
- Store large objects in job properties
- Forget to handle failures
- Use sync driver in production

Overview

This skill implements Laravel-style background job processing with queues, workers, batches, chains, middleware, and failure handling. It provides a clear workflow for spawning agents to analyze the codebase and verify queue patterns, then delivers job templates, decision guidance, and monitoring recommendations. Use it to convert synchronous or long-running operations into resilient, observable background tasks. The skill emphasizes production-safe defaults and operational visibility.

How this skill works

Before any implementation, spawn three agents via TeamCreate: fuse-ai-pilot:explore-codebase, fuse-ai-pilot:research-expert, and mcp__context7__query-docs to analyze existing job patterns and validate documentation. Implement jobs using ShouldQueue, configure retries/backoff/timeouts, and add middleware for rate limiting or deduplication. Use batches for grouped work, chains for sequential steps, and Horizon + Redis for production monitoring. After implementation, run fuse-ai-pilot:sniper to validate behavior and coverage.

When to use it

  • Offload slow or CPU-bound tasks to avoid web request timeouts
  • Process large datasets or group operations with progress tracking (batches)
  • Coordinate multi-step workflows where order matters (chains)
  • Apply rate limits, deduplication, or one-at-a-time execution via middleware
  • Scale workers in production and monitor with Horizon/Redis

Best practices

  • Always use ShouldQueue for asynchronous jobs and mark job classes final
  • Set tries, backoff, and timeout values tailored to the job's complexity
  • Implement failed() to capture and react to terminal errors
  • Avoid dispatching within DB transactions; use afterCommit
  • Prefer Redis + Horizon in production for performance and observability
  • Use Unique jobs or WithoutOverlapping to prevent concurrent conflicts

Example use cases

  • Order processing: dispatch background ProcessOrder jobs with retries and failure logging
  • Bulk import: run a Batch of import jobs with progress and cancel support
  • Media pipeline: chain encoding, thumbnailing, and metadata extraction jobs
  • Rate-limited API sync: apply middleware to limit calls per minute
  • High-throughput event handling: use Redis driver with Horizon for monitoring

FAQ

Which queue driver should I pick?

Use sync for local dev, database for small apps, Redis for production (Horizon support), and SQS for AWS-managed scaling.

How do I handle long-running jobs?

Set an appropriate timeout, configure retries and backoff, and break work into smaller jobs or batches when possible.