• Stars
    star
    149
  • Rank 246,959 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 1 year ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Batteries included UI to monitor your Messenger workers, transports, schedules, and messages.

zenstruck/messenger-monitor-bundle

CI codecov

Batteries included UI to monitor your Messenger workers, transports, schedules, and messages.

Screenshot

If the packaged UI is not to your liking, you can easily build your own with the provided tools.

Installation

composer require zenstruck/messenger-monitor-bundle

messenger:monitor Command

With zero configuration, you can run the messenger:monitor command to see information about your running workers and transports. If storage is configured, it displays a historical snapshot whose period can be customized with the --period option.

Storage

Note

This step is required to use the User Interface and History.

Note

Only Doctrine ORM is currently available as a storage engine.

Configuration

  1. Create a ProcessedMessage entity that extends Zenstruck\Messenger\Monitor\History\Model\ProcessedMessage in your app:

    // src/Entity/ProcessedMessage.php
    
    namespace App\Entity;
    
    use Zenstruck\Messenger\Monitor\History\Model\ProcessedMessage as BaseProcessedMessage;
    use Doctrine\ORM\Mapping as ORM;
    
    #[ORM\Entity(readOnly: true)]
    #[ORM\Table('processed_messages')]
    class ProcessedMessage extends BaseProcessedMessage
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private ?int $id = null;
    
        public function id(): ?int
        {
            return $this->id;
        }
    }
  2. Add the entity class to the bundle config:

    # config/packages/zenstruck_messenger_monitor.yaml
    
    zenstruck_messenger_monitor:
        storage:
            orm:
                entity_class: App\Entity\ProcessedMessage
  3. Clear Cache:

    bin/console cache:clear
  4. Create and execute the migration:

    bin/console doctrine:migrations:diff
    bin/console doctrine:migrations:migrate

Usage

Once configured, consumed messages are tracked and saved. These processed messages contain a lot of useful information and can be viewed in the user interface or the provided tools.

Disable Monitoring

You may want to disable monitoring for certain messages. There are two ways to do this:

  1. When dispatching the message, add the DisableMonitoringStamp:
    use Zenstruck\Messenger\Monitor\Stamp\DisableMonitoringStamp;
    
    /** @var \Symfony\Component\Messenger\MessageBusInterface $bus */
    
    $bus->dispatch(new MyMessage(), [new DisableMonitoringStamp()])
  2. Add the DisableMonitoringStamp as a class attribute to your message:
    use Zenstruck\Messenger\Monitor\Stamp\DisableMonitoringStamp;
    
    #[DisableMonitoringStamp]
    class MyMessage
    {
    }

Description

The stored ProcessedMessage has a description property. This is helpful to differentiate between messages in the user interface. By default, this is the stringified version of the message object (if it implements \Stringable). You can add the DescriptionStamp to customize:

use Zenstruck\Messenger\Monitor\Stamp\DescriptionStamp;

/** @var \Symfony\Component\Messenger\MessageBusInterface $bus */

$bus->dispatch(new MyMessage(), [new DescriptionStamp('some custom description')])

Tags

To help with filtering processed messages, they can have one or more tags. Some tags are added automatically (like schedule if it's a scheduled message) but you can also add your own in one of two ways:

  1. When dispatching the message, add one or more TagStamp's:
    use Zenstruck\Messenger\Monitor\Stamp\TagStamp;
    
    /** @var \Symfony\Component\Messenger\MessageBusInterface $bus */
    
    $bus->dispatch(new MyMessage(), [new TagStamp('tag-1'), new TagStamp('tag-2')])
  2. Add the TagStamp as a class attribute to your message:
    use Zenstruck\Messenger\Monitor\Stamp\TagStamp;
    
    #[TagStamp('tag-1')]
    #[TagStamp('tag-2')]
    class MyMessage
    {
    }

messenger:monitor:purge Command

If your app handles a lot of messages, the processed message database table will get very large. The messenger:monitor:purge clears messages older than a specific date: See Period for allowed values.

bin/console messenger:monitor:purge # by default, purges all messages older than 1 month

bin/console messenger:monitor:purge --older-than all
bin/console messenger:monitor:purge --older-than 1-day
bin/console messenger:monitor:purge --older-than 1-week

bin/console messenger:monitor:purge --exclude-schedules # ignore messages tagged with "schedule"

Note

Schedule this command to run daily with symfony/scheduler and RunCommandMessage.

messenger:monitor:schedule:purge Command

If using symfony/scheduler, you might want to keep a specific # of these messages as they might run very infrequently. When running messenger:monitor:purge, add the --exclude-schedules option to avoid deleting schedule history. Then run messenger:monitor:schedule:purge to keep a specific number (10 by default) of task run histories.

bin/console messenger:monitor:schedule:purge # by default, keeps 10 runs for each task

bin/console messenger:monitor:schedule:purge --keep 5 # keep only 5

Use the --remove-orphans option to delete schedule task runs that are no longer associated with a schedule.

bin/console messenger:monitor:schedule:purge --remove-orphans

Note

Schedule this command to run daily with symfony/schedule and RunCommandMessage.

User Interface

Note

Storage must be configured for this feature.

Create a controller that extends Zenstruck\Messenger\Monitor\Controller\MessengerMonitorController in your app:

// src/Controller/MessengerMonitorController.php

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Zenstruck\Messenger\Monitor\Controller\MessengerMonitorController as BaseMessengerMonitorController;

#[Route('/admin/messenger')] // path prefix for the controllers
#[IsGranted('ROLE_ADMIN')] // alternatively, use a firewall
final class MessengerMonitorController extends BaseMessengerMonitorController
{
}

You can now access the dashboard at: /admin/messenger or with the route zenstruck_messenger_monitor_dashboard.

Warning

It is important that your MessengerMonitorController is only accessible by site administrators as it contains sensitive application information. Use either the IsGranted attribute on your controller as shown above and/or ensure the controller is behind an access-controlled firewall that only allows site administrators.

Note

Install knplabs/knp-time-bundle (composer require knplabs/knp-time-bundle) to display friendlier times and durations in the UI.

Note

Install lorisleiva/cron-translator (composer require lorisleiva/cron-translator) to display friendlier CRON values for your scheduled tasks.

Advanced Usage

Workers Service

WorkerInfo

Transports Service

TransportInfo

QueuedMessage

Schedules Service

ScheduleInfo

TaskInfo

MessageInfo

TriggerInfo

History

Note

Storage must be configured for this feature.

Storage Service

Specification

Snapshot

Full Default Bundle Configuration

zenstruck_messenger_monitor:
    live_components:
        enabled:              false

        # Role required to view live components.
        role:                 ROLE_MESSENGER_MONITOR
    storage:
        orm:

            # Your Doctrine entity class that extends "Zenstruck\Messenger\Monitor\History\Model\ProcessedMessage"
            entity_class:         ~

More Repositories

1

foundry

A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.
PHP
634
star
2

schedule-bundle

Schedule Cron jobs (commands/callbacks/bash scripts) within your Symfony application.
PHP
383
star
3

messenger-test

Assertions and helpers for testing your symfony/messenger queues.
PHP
222
star
4

browser

A fluent interface for your Symfony functional tests.
PHP
185
star
5

assert

Standalone, lightweight, framework agnostic, test assertion library.
PHP
56
star
6

console-extra

A modular set of features to reduce configuration boilerplate for your Symfony commands.
PHP
47
star
7

console-test

Alternative, opinionated helper for testing Symfony console commands.
PHP
34
star
8

callback

Callable wrapper to validate and inject arguments.
PHP
34
star
9

image

Image file wrapper with generic transformation support.
PHP
33
star
10

mailer-test

Alternative, opinionated helpers for testing emails sent with symfony/mailer.
PHP
33
star
11

redirect-bundle

Store redirects for your site and keeps statistics on redirects and 404 errors.
PHP
25
star
12

filesystem

Wrapper for league/flysystem with alternate API and added functionality.
PHP
17
star
13

uri

Object-oriented wrapper/manipulator for parse_url with additional features.
PHP
14
star
14

twig-service-bundle

Make functions, static methods, Symfony service methods available in your twig templates.
PHP
9
star
15

changelog

Generate pretty release changelogs using the commit log and Github API.
PHP
8
star
16

collection

Helpers for iterating/paginating/filtering collections (with Doctrine ORM/DBAL implementations and batch processing utilities).
PHP
8
star
17

signed-url-bundle

Helpers for signing and verifying urls with support for temporary and single-use urls.
PHP
6
star
18

class-metadata

Add human readable class aliases & metadata with efficient lookups.
PHP
5
star
19

dimension

Wrap quantity and unit of measure with conversions/humanizers.
PHP
4
star
20

redis

Lazy proxy for php-redis with DX helpers and utilities.
PHP
4
star
21

commonmark-extensions

A collection of CommonMark extensions.
PHP
4
star
22

phpmyadmin-server

Run phpMyAdmin in the background with a PHP webserver
PHP
3
star
23

dsn

DSN parsing library with support for complex expressions.
PHP
3
star
24

memoize

Helper trait to efficiently cache expensive methods in memory.
PHP
2
star
25

bytes

Parse, manipulate, humanize, and format bytes.
PHP
1
star
26

assert-html

Fluent html assertions plugin for zenstruck/assert.
PHP
1
star
27

temp-file

Temporary file wrapper.
PHP
1
star
28

stream

Object wrapper for PHP resources.
PHP
1
star
29

dom

DOM crawler with advanced selector API and assertions.
PHP
1
star