Symfony Messenger

repository·8.2·Indexed 22 days ago

https://github.com/symfony/messenger

A component for sending and receiving messages to enable asynchronous processing and communication between applications via message queues. Includes tools for consuming messages (messenger:consume), debugging configurations (debug:messenger), managing failed messages (messenger:failed:show, retry, remove), and setting up transport infrastructure (messenger:setup-transports).

Tokens
4.9K
Snippets
16
Records
18
Agent score
75%

What's inside symfony-messenger

  1. What is the Messenger component

    8.2
    The Messenger component is designed to help applications send and receive messages. This can be used to communicate with other applications or to manage asynchronous processing via message queues.
  2. How to consume from all receivers or exclude specific ones

    8.2

    To consume from every configured transport, use the --all option. If you want to consume from all transports except a specific one, combine --all with --exclude-receivers.

    Note: The --exclude-receivers option cannot be used without --all.

    php bin/console messenger:consume --all
    
    # Consume from all except 'failed_messages'
    php bin/console messenger:consume --all --exclude-receivers=failed_messages
  3. How to use regular expressions for receivers

    8.2

    You can pass a regular expression as an argument to match multiple receivers. The order of receivers will match their order in your configuration.

    To ensure a specific priority order when using multiple receivers, pass each name or regular expression as a separate argument instead of a single regex.

    # Match multiple receivers via regex
    php bin/console messenger:consume "receiver1|receiver2"
    
    # Explicit priority: receiver2 first, then receiver1
    php bin/console messenger:consume receiver2 receiver1
  4. Reference: messenger:consume arguments and options

    8.2

    The following arguments and options are available for the messenger:consume command:

    Arguments

    • receivers (array): Names or regular expression patterns of the receivers/transports to consume in order of priority.

    Options

    • --limit <number> (-l): Limit the number of received messages.
    • --failure-limit <number> (-f): The number of failed messages the worker can consume before stopping.
    • --memory-limit <limit> (-m): Stop the worker if it exceeds a given memory usage limit. Supports shorthand byte values like 128M, 1G, etc.
    • --time-limit <seconds> (-t): Stop the worker after the given time limit in seconds. The worker will finish processing the current message before exiting.
    • --sleep <seconds>: Seconds to sleep before asking for new messages after no messages were found (default: 1).
    • --bus <name> (-b): Name of the bus to which received messages should be dispatched. If not provided, the bus is determined automatically.
    • --queues <queue-name> (array): Limit receivers to only consume from the specified queues (supported by some receivers).
    • --no-reset [number]: Prevents resetting container services after each message. If a number is passed, services are reset every N messages.
    • --all: Consume messages from all receivers.
    • --exclude-receivers <receiver-name> (array): Exclude specific receivers from consumption (only usable with --all).
    • --keepalive <seconds>: Whether to use the transport's keepalive mechanism if implemented (default: 5).
    • --fetch-size <number>: The number of messages to fetch per call to the transport (default: 1).
    php bin/console messenger:consume receiver1 --limit=10 --memory-limit=128M --bus=event_bus
  5. Reference: messenger:stats options and arguments

    8.2

    The messenger:stats command accepts the following input configuration:

    Arguments

    • transport_names (array, optional): List of transports' names to query.

    Options

    • --format (string, required): The output format. Available options are txt and json.
    Arguments:
      transport_names    List of transports' names
    
    Options:
      --format=VALUE    The output format ("txt", "json") [default="txt"]
  6. Reference: messenger:failed:retry options and arguments

    8.2

    The following arguments and options are available for the messenger:failed:retry command.

    Arguments:
      id (array)             Specific message id(s) to retry
    
    Options:
      --force                 Force action without confirmation
      --transport (string)    Use a specific failure transport (default: the global failure transport)
      --keepalive (int)       Whether to use the transport's keepalive mechanism if implemented (default: 5)
      --class-filter (string) Filter by a specific class name
      --failed-after (string) Only select messages that failed at or after this date
      --failed-before (string) Only select messages that failed at or before this date
  7. Reference: messenger:failed:show options and arguments

    8.2

    The messenger:failed:show command accepts the following arguments and options:

    TypeNameDescription
    ArgumentidSpecific message id to show
    Option--max=<value>Maximum number of messages to list (default: 50)
    Option--transport=<name>Use a specific failure transport
    Option--statsDisplay the message count by class
    Option--class-filter=<name>Filter by a specific class name
    Option--failed-after=<date>Only select messages that failed at or after this date (accepts DateTimeImmutable expressions)
    Option--failed-before=<date>Only select messages that failed at or before this date (accepts DateTimeImmutable expressions)
  8. Reference: messenger:failed:remove options

    8.2

    The following options and arguments are available for the messenger:failed:remove command:

    Arguments:
      id (optional, array)          Specific message id(s) to remove
    
    Options:
      --all                         Remove all failed messages from the transport
      --force                       Force the operation without confirmation
      --transport (required)        Use a specific failure transport
      --show-messages               Display messages before removing it (if multiple ids are given)
      --class-filter (required)     Filter by a specific class name
      --failed-after (required)     Only select messages that failed at or after this date
      --failed-before (required)    Only select messages that failed at or before this date
  9. Prepare transport infrastructure with messenger:setup-transports

    8.2

    The messenger:setup-transports command prepares the required infrastructure (such as database tables, queues, or other external resources) for your configured Messenger transports.

    It can be used to set up all configured transports at once, or you can specify a single transport name to target only one.

    Note that a transport can only be set up via this command if it implements the Symfony\Component\Messenger\Transport\SetupableTransportInterface.

    # Setup all configured transports
    php bin/console messenger:setup-transports
    
    # Setup a specific transport by name
    php bin/console messenger:setup-transports <transport>
    
    # Example: Setup the 'async' transport
    php bin/console messenger:setup-transports async
  10. Debug Messenger configuration with debug:messenger

    8.2

    The debug:messenger command allows you to inspect your Messenger configuration. It lists all messages that can be dispatched via your configured message buses and shows which handlers are responsible for processing them.

    Usage

    To list all messages for all available buses:

    php bin/console debug:messenger

    To list messages for a specific bus only, provide the bus ID as an argument:

    php bin/console debug:messenger <bus_id>

    Output Information

    For each bus, the command displays a table containing:

    • Message Class: The FQCN (Fully Qualified Class Name) of the message.
    • Description: If the message class has a docblock, it is displayed as a comment.
    • Handlers: The class name of the handler(s) responsible for the message.
    • Conditions: If a handler is restricted by specific conditions (e.g., using when()), these are displayed as (when key=value, ...).
    php bin/console debug:messenger
    # Or for a specific bus:
    php bin/console debug:messenger command_bus