Laravel queues

From AsyrafHussin/agent-skills · View interactive version

Laravel queues is an AI agent skill from the GitHub repository AsyrafHussin/agent-skills. Laravel 13 queue and job patterns — driver choice, job design (idempotency, ShouldQueue, model serialisation), retry and failure handling, worker scaling, Bus batching and chaining, Horizon when warranted, queue testing. Use when designing async jobs, scheduling background work, configuring Horizon,

How to use this skill

Skill file

Laravel Queues & Jobs

Production-grade queue patterns for Laravel 13 (MySQL + Redis). Contains 20 rules across 6 categories covering driver choice, job design, retry/failure handling, worker scaling, batching/chaining, and testing. Targets the failure modes that pass code review but break under load — silent re-dispatch on retry, models stale on serialisation, missing idempotency on payment jobs, workers leaking memory, and "we forgot ShouldQueue and the request takes 12 seconds in production."

Metadata

How to Use

When the user asks "design this background job", "audit our queue setup", "why is this job not running", or anything queue-related — work through this skill's rules as a checklist against the relevant files (job classes, config/queue.php, config/horizon.php, supervisor config, the dispatching call sites).

For audit mode, output per-rule verdicts:

End with a top-priority fix list (idempotency on payment jobs, missing ShouldQueue, supervisor stopwaitsecs too low — these are the most common production-bite issues).

When to Apply

Reference this skill when:

Step 1: Detect Queue Setup

Inspect:

| File | What to learn |

|---|---|

| config/queue.php | Default connection (QUEUE_CONNECTION env), failed-jobs storage, after_commit setting |

| config/horizon.php (if present) | Horizon environments, balance strategy, worker counts, timeouts |

| app/Jobs/*.php | Job classes, ShouldQueue usage, tries/backoff, failed() methods |

| app/Console/Kernel.php or routes/console.php | Scheduled jobs (Schedule::job(...), withoutOverlapping) |

| database/migrations/*_create_failed_jobs_table.php | Failed-jobs storage migration; or DynamoDB driver in config |

| supervisor*.conf / /etc/supervisor/conf.d/ | Worker process management, numprocs, --max-time, stopwaitsecs |

Typical setups:

| Stack | Queue driver | Failed driver | Worker manager |

|---|---|---|---|

| Small Laravel + MySQL | database | database | Supervisor (or systemd) |

| Production Laravel + Redis | redis | database (or dynamodb for serverless) | Supervisor + Horizon |

| Laravel Vapor | sqs | dynamodb | Vapor-managed |

Rule Categories by Priority

| Priority | Category | Impact | Prefix |

|----------|----------|--------|--------|

| 1 | Driver & Config | CRITICAL | config- |

| 2 | Job Design | CRITICAL | design- |

| 3 | Retry & Failure | HIGH | retry- |

| 4 | Scaling & Workers | HIGH | scaling- |

| 5 | Batching & Chaining | HIGH | bus- |

| 6 | Testing & Operations | MEDIUM | ops- |

Quick Reference

1. Driver & Config (CRITICAL)

2. Job Design (CRITICAL)

3. Retry & Failure (HIGH)

4. Scaling & Workers (HIGH)

5. Batching & Chaining (HIGH)

6. Testing & Operations (MEDIUM)

Essential Patterns

Minimum-viable job class (Laravel 13)

<?php

namespace App\Jobs;

use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\Attributes\Backoff;
use Illuminate\Queue\Attributes\FailOnTimeout;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Throwable;

#[Backoff([1, 5, 30])]
#[FailOnTimeout]
class ChargeOrder implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public int $tries = 5;
    public int $timeout = 30;

    public function __construct(public readonly int $orderId) {}

    public function handle(StripeGateway $stripe): void
    {
        $order = Order::findOrFail($this->orderId);   // refetch — don't trust serialised state
        if ($order->status === 'paid') return;        // idempotency guard

        $charge = $stripe->charge($order);
        $order->markPaid($charge->id);
    }

    public function failed(Throwable $e): void
    {
        Order::find($this->orderId)?->markPaymentFailed($e->getMessage());
    }
}

Dispatching

ChargeOrder::dispatch($order->id);                       // default queue
ChargeOrder::dispatch($order->id)->onQueue('high');      // priority lane
ChargeOrder::dispatch($order->id)->delay(now()->addMinutes(5));
ChargeOrder::dispatchAfterResponse($order->id);          // run after HTTP response sent

Supervisor config (recommended)

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/app/artisan queue:work redis --queue=high,default,low --sleep=3 --tries=3 --max-time=3600 --backoff=3
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=forge
numprocs=8
redirect_stderr=true
stdout_logfile=/var/www/app/storage/logs/worker.log
stopwaitsecs=3600

Critical: stopwaitsecs must be greater than your longest job's timeout, or Supervisor will kill mid-job on deploy.

References

Full Compiled Document

For the complete guide with all rules expanded: AGENTS.md

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.