For arlanix/notification 1.1.0
Queue, cron and command line
This page describes how notifications go through Magento's message queue, which cron jobs the modules add, and the console commands.
The queue
The modules use these message queue settings:
| Item | Value |
|---|---|
| Topic | arlanix.notification.deliver. The message body is a delivery id, as a string. |
| Queue | arlanix.notification.deliver, bound to the magento exchange. |
| Consumer | arlanix_notification_deliver, with maxMessages 1000. |
| Connection | The store's default connection. This is amqp when app/etc/env.php configures RabbitMQ (or sets queue/default_connection), and db otherwise. |
A queue message holds only a delivery id. The delivery itself is a row in arlanix_notification_delivery, and that row is the source of truth.
Before a consumer sends a delivery, it claims the row. It changes status from queued to sending and writes a claim token. Only one consumer can claim a row. So if the queue hands out the same message twice, or two consumers take it, the delivery is still sent once.
Only the consumer that holds the claim can record the result. If a consumer is still sending after 15 minutes, the cron takes the delivery back (see Cron jobs). When the slow consumer finishes, it does not overwrite anything that happened since. It logs a warning that the delivery may be sent twice.
Deliveries published inside a database transaction go on the queue only after the transaction commits. Otherwise a RabbitMQ consumer could read a delivery id before its row exists.
Running the consumer
Magento's cron starts the consumer through its consumer runner (cron_consumers_runner). Every minute, if messages are waiting, the runner starts the consumer. Each run takes up to 1000 messages, in batches of 100, and then exits.
To run the consumer yourself, or under supervisor, use one of these commands:
bin/magento queue:consumers:start arlanix_notification_deliver
bin/magento queue:consumers:start arlanix_notification_deliver --max-messages=100 --batch-size=50
Without --max-messages, the consumer keeps running and reads a batch every 10 seconds. It exits when someone saves the configuration (Magento's poison pill). Supervisor then starts it again with the new settings.
Several at once
A slow or unreachable server holds up the consumer process that sends to it, for as long as the server's timeout. If you run several consumers, the others keep sending. To have cron start two processes, add this to app/etc/env.php:
// app/etc/env.php
'cron_consumers_runner' => [
'cron_run' => true,
'max_messages' => 1000,
'multiple_processes' => [
'arlanix_notification_deliver' => 2,
],
],
Each process merges only its own batch. So a burst of messages may arrive as two or three digests instead of one, and deliveries may arrive out of order.
Digests
The consumer combines the messages that wait for the same subscription into one digest. It works in three steps:
Arlanix\Notification\Model\Queue\DeliveryMerger, registered for the consumer inMergerFactory, passes all deliveries of a batch toDeliveryHandleras one message.- The dispatcher groups the deliveries by subscription.
- Each group goes to its protocol as one
Digest. A message published several times appears once, with its count. A group with more than 25 different messages (DigestBuilder::MAX_ENTRIES) is sent as several digests.
If one digest fails, it and the digests after it wait for their retry. The digests sent before it stay sent.
The merger returns one merged message per batch, not one per subscription. When a merger returns more than one, Magento's MergedMessageProcessor acknowledges the wrong queue messages, and RabbitMQ then closes the consumer's channel.
A batch holds the messages that were waiting when the consumer read it. Under the cron runner, that is about one minute of messages. For a consumer that keeps running, it is about 10 seconds of messages.
Cron jobs
The modules add these cron jobs:
| Job | Group | Schedule | What it does |
|---|---|---|---|
arlanix_notification_requeue |
arlanix_notification |
Every minute | Puts retries that are due back on the queue. Takes back deliveries that have been in sending for 15 minutes. Each one becomes a retry, or fails if it has no attempts left. Puts back queued deliveries that were published more than 60 minutes ago. |
arlanix_notification_clean_log |
default |
03:40 | Deletes messages older than log/keep_days (30 by default), with their deliveries. A message stays while any of its deliveries is unfinished. With 0, nothing is deleted. |
arlanix_notification_monitoring_check |
arlanix_notification |
Every 5 minutes | Comes with the monitoring module. Checks for failed cron jobs, invalid indexers, a stopped cron and each store view's orders. See Monitoring. |
The arlanix_notification group runs in its own process (use_separate_process).
Delivery statuses
A delivery has one of these statuses:
| Status | Meaning |
|---|---|
queued |
The delivery is on the queue, or about to go on it. |
sending |
A consumer has claimed it. |
retry |
An attempt failed, or the service asked the store to wait (HTTP 429). next_attempt_at is when the cron puts it back on the queue. During the first 24 hours after the message was published, a 429 does not use up an attempt. |
sent |
The delivery arrived. |
failed |
All attempts failed. A delivery gets one attempt more than the number of entries in delivery/retry_minutes. |
skipped |
The subscription was deleted or switched off before the delivery was sent. |
Command line
Publish a message
To publish a message from the shell, use this command:
bin/magento arlanix:notification:publish <topic> <subject> [--body=…] [--severity=info|warning|error] [--attribute=label=value]… [--source=…]
<topic> is a topic's id or its name. The command takes these options:
| Option | Default | Meaning |
|---|---|---|
--body |
Empty | The message text. |
--severity |
info |
info, warning or error. |
--attribute |
A detail shown with the text, as label=value. Repeat the option for more details. |
|
--source |
CLI |
Who published the message. |
The command publishes the message the same way a module does, through the queue. It prints how many deliveries it queued:
bin/magento arlanix:notification:publish "Feed alerts" "Disk almost full" --severity=warning --attribute="Free=2 GB" --source=monitoring
Published to "Feed alerts": 1 delivery(ies) queued.
If no active subscription of the topic takes the message's severity, the command says so.
The command fails with a non-zero exit code, and prints the reason, in these cases:
- The topic is switched off.
- The topic does not exist.
- The message could not be recorded.
If the queue refuses the deliveries, the command still succeeds. The cron puts the deliveries on the queue again later.
Run the monitoring checks
With the monitoring module installed, this command runs its checks now:
bin/magento arlanix:notification:monitoring:check
It does the same as the cron job, in the crontab area. Every change it finds goes to the rules for that event. If one check fails, the error is logged to var/log/arlanix.log, and the other checks still run.