2016-06-26 18:49:35 +02:00
|
|
|
# Queue processors
|
2016-04-01 07:23:05 +02:00
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
Zulip uses RabbitMQ to manage a system of internal queues. These are
|
2016-04-01 07:23:05 +02:00
|
|
|
used for a variety of purposes:
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Asynchronously doing expensive operations like sending email
|
2016-04-01 07:23:05 +02:00
|
|
|
notifications which can take seconds per email and thus would
|
2024-07-04 12:33:43 +02:00
|
|
|
otherwise time out when 100s are triggered at once (e.g., inviting a
|
2016-04-01 07:23:05 +02:00
|
|
|
lot of new users to a realm).
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Asynchronously doing non-time-critical somewhat expensive operations
|
2024-07-04 12:33:43 +02:00
|
|
|
like updating analytics tables (e.g., UserActivityInternal) which
|
2016-04-01 07:23:05 +02:00
|
|
|
don't have any immediate runtime effect.
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Communicating events to push to clients (browsers, etc.) from the
|
2016-04-01 07:23:05 +02:00
|
|
|
main Zulip Django application process to the Tornado-based events
|
2021-08-20 21:53:28 +02:00
|
|
|
system. Example events might be that a new message was sent, a user
|
2016-04-01 07:23:05 +02:00
|
|
|
has changed their subscriptions, etc.
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Processing mobile push notifications and email mirroring system
|
2016-04-01 07:23:05 +02:00
|
|
|
messages.
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Processing various errors, frontend tracebacks, and slow database
|
2016-04-01 07:23:05 +02:00
|
|
|
queries in a batched fashion.
|
|
|
|
|
|
|
|
Needless to say, the RabbitMQ-based queuing system is an important
|
|
|
|
part of the overall Zulip architecture, since it's in critical code
|
|
|
|
paths for everything from signing up for account, to rendering
|
|
|
|
messages, to delivering updates to clients.
|
|
|
|
|
|
|
|
We use the `pika` library to interface with RabbitMQ, using a simple
|
|
|
|
custom integration defined in `zerver/lib/queue.py`.
|
|
|
|
|
|
|
|
### Adding a new queue processor
|
|
|
|
|
|
|
|
To add a new queue processor:
|
|
|
|
|
2024-04-16 20:49:37 +02:00
|
|
|
- Define the processor in `zerver/worker/` using the `@assign_queue` decorator;
|
|
|
|
it's pretty easy to get the template for an existing similar queue
|
|
|
|
processor. This suffices to test your queue worker in the Zulip development
|
|
|
|
environment (`tools/run-dev` will automatically restart the queue processors
|
|
|
|
and start running your new queue processor code). You can also run a single
|
2024-07-09 17:45:31 +02:00
|
|
|
queue processor manually using, for example,
|
|
|
|
`./manage.py process_queue --queue=user_activity`.
|
2016-04-01 07:23:05 +02:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- So that supervisord will know to run the queue processor in
|
2020-10-20 09:28:17 +02:00
|
|
|
production, you will need to add to the `queues` variable in
|
|
|
|
`puppet/zulip/manifests/app_frontend_base.pp`; the list there is
|
|
|
|
used to generate `/etc/supervisor/conf.d/zulip.conf`.
|
2017-02-19 06:36:57 +01:00
|
|
|
|
2017-02-17 07:16:43 +01:00
|
|
|
The queue will automatically be added to the list of queues tracked by
|
|
|
|
`scripts/nagios/check-rabbitmq-consumers`, so Nagios can properly
|
2021-08-20 21:53:28 +02:00
|
|
|
check whether a queue processor is running for your queue. You still
|
2024-02-06 21:40:19 +01:00
|
|
|
need to update the sample Nagios configuration in `puppet/kandra`
|
2017-02-17 07:16:43 +01:00
|
|
|
manually.
|
2016-04-01 07:23:05 +02:00
|
|
|
|
|
|
|
### Publishing events into a queue
|
|
|
|
|
|
|
|
You can publish events to a RabbitMQ queue using the
|
|
|
|
`queue_json_publish` function defined in `zerver/lib/queue.py`.
|
|
|
|
|
2017-11-26 20:49:42 +01:00
|
|
|
An interesting challenge with queue processors is what should happen
|
2021-08-20 21:53:28 +02:00
|
|
|
when queued events in Zulip's backend tests. Our current solution is
|
2017-11-26 20:49:42 +01:00
|
|
|
that in the tests, `queue_json_publish` will (by default) simple call
|
2021-08-20 21:53:28 +02:00
|
|
|
the `consume` method for the relevant queue processor. However,
|
2017-11-26 20:49:42 +01:00
|
|
|
`queue_json_publish` also supports being passed a function that should
|
|
|
|
be called in the tests instead of the queue processor's `consume`
|
2021-08-20 21:53:28 +02:00
|
|
|
method. Where possible, we prefer the model of calling `consume` in
|
2017-11-26 20:49:42 +01:00
|
|
|
tests since that's more predictable and automatically covers the queue
|
|
|
|
processor's code path, but it isn't always possible.
|
|
|
|
|
2016-04-01 07:23:05 +02:00
|
|
|
### Clearing a RabbitMQ queue
|
|
|
|
|
|
|
|
If you need to clear a queue (delete all the events in it), run
|
|
|
|
`./manage.py purge_queue <queue_name>`, for example:
|
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```bash
|
2016-04-01 07:23:05 +02:00
|
|
|
./manage.py purge_queue user_activity
|
|
|
|
```
|
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
You can also use the amqp tools directly. Install `amqp-tools` from
|
2016-04-01 07:23:05 +02:00
|
|
|
apt and then run:
|
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```bash
|
2016-04-01 07:23:05 +02:00
|
|
|
amqp-delete-queue --username=zulip --password='...' --server=localhost \
|
|
|
|
--queue=user_presence
|
|
|
|
```
|
|
|
|
|
|
|
|
with the RabbitMQ password from `/etc/zulip/zulip-secrets.conf`.
|