webignition/symfony-messenger-delay-middleware

Maintainers

Package info

github.com/webignition/symfony-messenger-delay-middleware

pkg:composer/webignition/symfony-messenger-delay-middleware

Transparency log

Statistics

Installs: 361

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.1 2026-07-21 14:10 UTC

This package is auto-updated.

Last update: 2026-07-21 14:11:03 UTC


README

Middleware for Symfony messenger to apply per-message-class delays to messages.

A NonDelayedStamp, if applied, will prevent an otherwise-applicable delay from being applied.

Configuration

Register the Middleware

Register the middleware in your messenger configuration. The messenger.bus.default bus is given below as an example. Adjust if necessary to match your configuration.

# config/packages/messenger.yaml
framework:
    messenger:
        buses:
            messenger.bus.default:
                middleware:
                    - webignition\SymfonyMessengerDelayMiddleware\DelayMiddleware

Define Your Per-Message Delays

# config/services.yaml
services:
  webignition\SymfonyMessengerDelayMiddleware\DelayMiddleware:
    arguments:
      $delays:
        App\Message\FooMessage: 1000  # delay FooMessage instances by one second.
        App\Message\BarMessage: 30000 # delay BarMessage instances by thirty seconds.       

Usage

Dispatching Delayed Messages

Once configured, nothing is needed to apply delays to dispatched messages. In the above example configuration, all App\Message\FooMessage messages will be delayed by one second and all App\Message\BarMessage messages will be delayed by thirty seconds. Nothing else is required for this to happen.

<?php

declare(strict_types=1);

namespace App\MessageDispatcher;

use App\Message\FooMessage;
use App\Message\BarMessage;
use Symfony\Component\Messenger\MessageBusInterface;

final readonly class ExampleMessageDispatcher
{
    public function __construct(
        private MessageBusInterface $messageBus,
    ) {}
    
    public function dispatchFooMessage(): void
    {
        // Delayed by one second as per the previous example configuration.
        $this->messageBus->dispatch(new FooMessage());        
    }
    
    public function dispatchBarMessage(): void
    {
        // Delayed by thirty seconds as per the previous example configuration.
        $this->messageBus->dispatch(new BarMessage());        
    }
}

Preventing Delay of Dispatched Messages

Perhaps you want to immediately dispatch a FooMessage or BarMessage that, by the above configuration, would have a delayed dispatch.

In such cases, apply a NonDelayedStamp when dispatching.

<?php

declare(strict_types=1);

namespace App\MessageDispatcher;

use App\Message\FooMessage;
use App\Message\BarMessage;
use Symfony\Component\Messenger\MessageBusInterface;
use webignition\SymfonyMessengerDelayMiddleware\NonDelayedStamp;

final readonly class ExampleMessageDispatcher
{
    public function __construct(
        private MessageBusInterface $messageBus,
    ) {}
    
    public function dispatchFooMessageWithoutDelay(): void
    {
        // FooMessage is dispatched immediately despite being configured to delay.
        $this->messageBus->dispatch(new FooMessage(), [new NonDelayedStamp()]);        
    }
    
    public function dispatchBarMessageWithoutDelay(): void
    {
        // BarMessage is dispatched immediately despite being configured to delay.
        $this->messageBus->dispatch(new BarMessage(), [new NonDelayedStamp()]);
    }
}