agentleFS
kwhornekwhorne/elyra-skills/skills/laravel-queue-design/SKILL.md

laravel-queue-design

Skill1 starsChanged 6 months agoMITView on GitHub

Design Laravel queue jobs that survive production - idempotency, retries and backoff, failure handling, batching, chaining, and Horizon tuning. Use when the user creates or reviews queued jobs, debugs stuck or duplicated jobs, asks about retries or failed_jobs, or moves slow work out of the request cycle.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    At-least-once means duplicates: Every handler must tolerate running twice — idempotency is the entry fee, not a nice-to-have.

    line 26
  2. Very specific:

    Don't retry the unretryable: validation-type failures → $this->fail($e) immediately

    line 68

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Laravel Queue Design

A queued job will run twice, run late, or run after the world changed under it. Design every job for those three facts and production gets boring — in the good way.

When to use

  • Creating or reviewing queued jobs, listeners, or notifications
  • "Jobs are stuck / duplicated / silently failing"
  • Moving slow work (mail, exports, API calls) out of the request
  • Setting up or tuning Horizon / queue workers

Principles

  • At-least-once means duplicates. Every handler must tolerate running twice — idempotency is the entry fee, not a nice-to-have.
  • Small payload, fresh state. Pass IDs, re-fetch inside the job. The world at dispatch time is not the world at run time.
  • Failure is a feature. Decide per job what retry, backoff, and final failure mean — the defaults are guesses.
  • One job, one outcome. A job that does five things fails five ways and retries all of them.

Process

1. Design the job

class ProcessRefund implements ShouldQueue
{
    public function __construct(public int $orderId) {}   // ID, not model state you'll trust later

    public $tries = 3;
    public $backoff = [10, 60, 300];          // seconds; spread retries out
    public $timeout = 120;                     // must be < retry_after in queue config!

    public function handle(): void
    {
        $order = Order::findOrFail($this->orderId);   // fresh state
        if ($order->refunded) return;                  // idempotency check
        // ...
    }
}
  • Eloquent models in constructors are fine (SerializesModels re-fetches), but decisions made on dispatch-time state are stale — re-check conditions in handle()
  • $timeout < retry_after (queue config) or the job runs twice concurrently — the classic Laravel queue foot-gun

2. Make it idempotent

Pick the mechanism that fits:

  • State check: if ($order->refunded) return; — re-read, then act
  • Unique jobs: ShouldBeUnique to prevent duplicate dispatch
  • DB constraint: unique index + firstOrCreate/upsert absorbs the duplicate
  • External calls: pass an idempotency key to the payment/API provider

3. Decide the failure story

  • failed(Throwable $e) method: notify, mark the domain object, compensate
  • Don't retry the unretryable: validation-type failures → $this->fail($e) immediately
  • Poison-pill protection: $tries/$maxExceptions so one bad payload doesn't loop forever
  • Monitor failed_jobs — a table nobody looks at is a black hole. Alert on growth; queue:retry after fixing the cause

4. Compose bigger work

NeedTool
Steps in order, abort on failureJob chain (Bus::chain)
Fan-out N items + completion hookBatch (Bus::batch(...)->then(...)->catch(...))
Per-tenant/user serializationWithoutOverlapping middleware
External API rate limitsRateLimited middleware, dedicated queue

For batches: items must be independently retryable; allowFailures() if one bad item shouldn't sink the batch.

5. Separate queues by SLA

  • high (user-facing: mail verification, receipts), default, low (exports, sync)
  • Workers: --queue=high,default,low priority order, or dedicated workers per queue
  • One slow queue must not starve fast ones — that's the whole point of separation

6. Operate it

  • Horizon: balance/processes per queue, alert on wait time, watch failed rate
  • Deploys: queue:restart (workers hold old code in memory until restarted)
  • Track queue wait time (latency from dispatch to start) — it's the user-facing number

Output format

## Queue review: <job/feature>

| Job | Idempotent via | tries/backoff | timeout<retry_after | On final failure |
|-----|----------------|---------------|---------------------|------------------|
| …   | state check    | 3 / 10-60-300 | ✅ 120<300          | notify + mark    |

### Gaps (ranked)
1. <job>: <risk> → <fix>

### Queue topology
high: …, default: …, low: … — workers: …

Anti-patterns

  • ❌ Serializing a model and trusting its attributes at run time (stale decisions)
  • ❌ $timeout ≥ retry_after — concurrent double execution by design
  • ❌ Default retries on a non-idempotent job touching money
  • ❌ One giant job processing 10 000 rows instead of a batch of small ones
  • ❌ failed_jobs as a write-only table with no alerting
  • ❌ Forgetting queue:restart on deploy, then debugging "old code" for an hour
  • ❌ Everything on the default queue — exports starving password-reset mails

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.