Repository navigation
Conversation
An init hook can only stop a job by throwing, and a throw is rejected: on Redis the payload stays on the failed list for the retry sweep. Appwrite Cloud refused over-limit tenants that way and kept 273k refusals (~2 GB) in one queue in a week, until the instance started evicting live jobs. Server::middleware() returns a Hook that matches groups like init() and wraps the job. The action injects `next` and calls it to run the rest of the chain; returning without calling it ends the message as handled, so the adapter commits it. Middleware runs after the init hooks, and the first one registered is the outermost. The init-hook matching moves into hooksFor() so both hook kinds share it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🔍 OpenCodeReview found 1 issue(s) in this PR.
|
| $inner = $next; | ||
| $next = function () use ($hook, $inner, $message): mixed { | ||
| $this->context()->set('next', fn(): \Closure => $inner); |
There was a problem hiding this comment.
$inner is invoked on every call to the injected continuation, so middleware can call $next() twice and execute the job (and any inner middleware) twice during one message delivery; the adapter will then commit the message as a success. Guard the continuation as one-shot (for example, throw on a second invocation) to prevent accidental duplicate side effects.
|
Release path: server-ce requires |
Benchmark resultsqueue — workload shapes across both concurrency axes (4 cores, 600 messages, median of 3)
Shared CI runners — treat absolute numbers as rough, compare modes within a run. Commit 718d341. |
|
Queue now lives in |
Why
An init hook can only stop a job by throwing, and a throw is rejected. On Redis the payload stays on the failed list for the retry sweep. Appwrite Cloud refused over-limit tenants that way in its functions worker, and fra1's queue Dragonfly ended up holding 273k refusals (~2 GB) after one week and started evicting live jobs (appwrite-labs/cloud#6062).
A refusal is an outcome, not a failure. The hook needs a way to say "handled, don't run the job" and have the broker commit the message.
What
Server::middleware()returns aHookthat matches groups the same wayinit()does and wraps the job:nextis injected like any other resource. It runs the rest of the chain and then the job, and returns the job's result.hooksFor(), so both hook kinds share one copy of it.Additive: nothing changes for servers that don't register middleware.
Tests
MiddlewareTestpushes one message throughServer::start()and the shared commit/reject phases:nextcommits and skips the jobnextruns the job and passes its result backWith the middleware loop removed, all four fail. The full queue unit suite passes (157 tests), and
bin/monorepo check queueand Vale are clean.Release
Cloud is on queue
^5.0.2, so this would ship asqueue/6.1.0and cloud would move to 6.x.🤖 Generated with Claude Code