Skip to content

[Discussion] [3.x] Message bus: Envelope + per-queue middleware for cakephp/queue #208

Description

@skie

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 PHP serialize())
    replace magic metadata keys.
  • Named assembly: dispatch building moves out of QueueManager::push() into
    dispatch middlewares; worker assembly moves out of WorkerCommand into
    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 (?string messageId); data payloads
    are byte-identical; delay/expire/priority stay transport headers.
  • Workers, Processor.message.* events, clientClass and processor config
    overrides keep working.
  • Transports (redis, fs, dbal, …) need no changes: the body is still an opaque
    JSON string. queue_failed_jobs needs no migration: stamps land in the existing
    metadata JSON column.

Activity

  1. markstory commented on Nov 25, 2026

    @markstory
    Member

    Seems reasonable to me to have middleware stacks per queue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions