Repository navigation
Description
Add a small handmade message bus to cakephp/queue, in the spirit of Symfony
Messenger: an immutable Envelope carrying the job payload plus typed stamps,
and an ordered middleware chain on both sides of the transport
(dispatch and consume).
// per-queue user middleware, declared in queue config
'Queue' => [
'emails' => [
'dispatchMiddleware' => [BatchMiddleware::class, TenantTagMiddleware::class],
'consumeMiddleware' => [],
],
];
// a middleware just enriches the envelope — no core patches needed
final class TenantTagMiddleware implements BusMiddlewareInterface
{
public function handle(Envelope $envelope, BusStackInterface $stack): Envelope
{
return $stack->next()->handle($envelope->with(new TagStamp('tenant:acme')), $stack);
}
}Stamps travel inside the existing metadata key, so the wire format stays compatible:
{
"data": {"userId": 42},
"metadata": {
"stamps": [
{"type": "TagStamp", "tags": ["App\\Job\\SendEmailJob"]},
{"type": "CorrelationStamp", "id": "9f3a2b1c-..."}
]
}
}Why
Today the "bus" is scattered across four places: QueueManager::push() builds the
message by hand, WorkerCommand assembles an Enqueue ChainExtension inline, and
there is nowhere at all to add send-time behavior per queue — tenant tagging,
correlation ids, validation before send, batching — except patching core or
global hooks that fire for every queue. Retries and failure storage stay exactly
where they are (they need the transport Consumer, which a bus never receives);
this proposal deliberately does not move them. What it adds is the missing piece:
a dispatch pipeline with typed stamps, plus a thin consume side (named assembly,
one shared envelope parse, a home for user consume middleware).
What it gives
- One extension point: user middleware per queue on dispatch and consume sides.
- Typed metadata: stamps (
toArray/fromArray, plain JSON, no PHPserialize())
replace magicmetadatakeys. - Named assembly: dispatch building moves out of
QueueManager::push()into
dispatch middlewares; worker assembly moves out ofWorkerCommandinto
BusFactory. Transport mechanics (retry clone, redelivery delay, unique
release, failed-job row) deliberately stay Enqueue extensions — they need the
Consumer, which the bus never receives; they only learn to read stamps. - Future features: batching (
BatchableStamp+ flush), pre-send validation,
send-time rate limiting, message signing — all as middlewares, all optional.
Non-breaking by design
QueueManager::push()keeps its signature (?stringmessageId);datapayloads
are byte-identical;delay/expire/prioritystay transport headers.- Workers,
Processor.message.*events,clientClassandprocessorconfig
overrides keep working. - Transports (
redis,fs,dbal, …) need no changes: the body is still an opaque
JSON string.queue_failed_jobsneeds no migration: stamps land in the existing
metadata JSONcolumn.
Activity
markstory commentedon Nov 25, 2026
Seems reasonable to me to have middleware stacks per queue.