| 1 |
.. _broker-rabbitmq: |
| 2 |
|
| 3 |
================ |
| 4 |
Using RabbitMQ |
| 5 |
================ |
| 6 |
|
| 7 |
.. contents:: |
| 8 |
:local: |
| 9 |
|
| 10 |
Installation & Configuration |
| 11 |
============================ |
| 12 |
|
| 13 |
RabbitMQ is the default broker so it doesn't require any additional |
| 14 |
dependencies or initial configuration, other than the URL location of |
| 15 |
the broker instance you want to use: |
| 16 |
|
| 17 |
.. code-block:: python |
| 18 |
|
| 19 |
broker_url = 'amqp://myuser:mypassword@localhost:5672/myvhost' |
| 20 |
|
| 21 |
For a description of broker URLs and a full list of the |
| 22 |
various broker configuration options available to Celery, |
| 23 |
see :ref:`conf-broker-settings`, and see below for setting up the |
| 24 |
username, password and vhost. |
| 25 |
|
| 26 |
.. _installing-rabbitmq: |
| 27 |
|
| 28 |
Installing the RabbitMQ Server |
| 29 |
============================== |
| 30 |
|
| 31 |
See `Downloading and Installing RabbitMQ`_ over at RabbitMQ's website. For macOS |
| 32 |
see `Installing RabbitMQ on macOS`_. |
| 33 |
|
| 34 |
.. _`Downloading and Installing RabbitMQ`: https://www.rabbitmq.com/download.html |
| 35 |
|
| 36 |
.. note:: |
| 37 |
|
| 38 |
If you're getting `nodedown` errors after installing and using |
| 39 |
:command:`rabbitmqctl` then this blog post can help you identify |
| 40 |
the source of the problem: |
| 41 |
|
| 42 |
http://www.somic.org/2009/02/19/on-rabbitmqctl-and-badrpcnodedown/ |
| 43 |
|
| 44 |
.. _rabbitmq-configuration: |
| 45 |
|
| 46 |
Setting up RabbitMQ |
| 47 |
------------------- |
| 48 |
|
| 49 |
To use Celery we need to create a RabbitMQ user, a virtual host and |
| 50 |
allow that user access to that virtual host: |
| 51 |
|
| 52 |
.. code-block:: console |
| 53 |
|
| 54 |
$ sudo rabbitmqctl add_user myuser mypassword |
| 55 |
|
| 56 |
.. code-block:: console |
| 57 |
|
| 58 |
$ sudo rabbitmqctl add_vhost myvhost |
| 59 |
|
| 60 |
.. code-block:: console |
| 61 |
|
| 62 |
$ sudo rabbitmqctl set_user_tags myuser mytag |
| 63 |
|
| 64 |
.. code-block:: console |
| 65 |
|
| 66 |
$ sudo rabbitmqctl set_permissions -p myvhost myuser ".*" ".*" ".*" |
| 67 |
|
| 68 |
Substitute in appropriate values for ``myuser``, ``mypassword`` and ``myvhost`` above. |
| 69 |
|
| 70 |
See the RabbitMQ `Admin Guide`_ for more information about `access control`_. |
| 71 |
|
| 72 |
.. _`Admin Guide`: https://www.rabbitmq.com/admin-guide.html |
| 73 |
|
| 74 |
.. _`access control`: https://www.rabbitmq.com/access-control.html |
| 75 |
|
| 76 |
.. _rabbitmq-macOS-installation: |
| 77 |
|
| 78 |
Installing RabbitMQ on macOS |
| 79 |
---------------------------- |
| 80 |
|
| 81 |
The easiest way to install RabbitMQ on macOS is using `Homebrew`_ the new and |
| 82 |
shiny package management system for macOS. |
| 83 |
|
| 84 |
First, install Homebrew using the one-line command provided by the `Homebrew |
| 85 |
documentation`_: |
| 86 |
|
| 87 |
.. code-block:: console |
| 88 |
|
| 89 |
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" |
| 90 |
|
| 91 |
Finally, we can install RabbitMQ using :command:`brew`: |
| 92 |
|
| 93 |
.. code-block:: console |
| 94 |
|
| 95 |
$ brew install rabbitmq |
| 96 |
|
| 97 |
.. _`Homebrew`: https://github.com/mxcl/homebrew/ |
| 98 |
.. _`Homebrew documentation`: https://github.com/Homebrew/homebrew/wiki/Installation |
| 99 |
|
| 100 |
.. _rabbitmq-macOS-system-hostname: |
| 101 |
|
| 102 |
After you've installed RabbitMQ with :command:`brew` you need to add the following to |
| 103 |
your path to be able to start and stop the broker: add it to the start-up file for your |
| 104 |
shell (e.g., :file:`.bash_profile` or :file:`.profile`). |
| 105 |
|
| 106 |
.. code-block:: bash |
| 107 |
|
| 108 |
PATH=$PATH:/usr/local/sbin |
| 109 |
|
| 110 |
Configuring the system host name |
| 111 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 112 |
|
| 113 |
If you're using a DHCP server that's giving you a random host name, you need |
| 114 |
to permanently configure the host name. This is because RabbitMQ uses the host name |
| 115 |
to communicate with nodes. |
| 116 |
|
| 117 |
Use the :command:`scutil` command to permanently set your host name: |
| 118 |
|
| 119 |
.. code-block:: console |
| 120 |
|
| 121 |
$ sudo scutil --set HostName myhost.local |
| 122 |
|
| 123 |
Then add that host name to :file:`/etc/hosts` so it's possible to resolve it |
| 124 |
back into an IP address:: |
| 125 |
|
| 126 |
127.0.0.1 localhost myhost myhost.local |
| 127 |
|
| 128 |
If you start the :command:`rabbitmq-server`, your rabbit node should now |
| 129 |
be `rabbit@myhost`, as verified by :command:`rabbitmqctl`: |
| 130 |
|
| 131 |
.. code-block:: console |
| 132 |
|
| 133 |
$ sudo rabbitmqctl status |
| 134 |
Status of node rabbit@myhost ... |
| 135 |
[{running_applications,[{rabbit,"RabbitMQ","1.7.1"}, |
| 136 |
{mnesia,"MNESIA CXC 138 12","4.4.12"}, |
| 137 |
{os_mon,"CPO CXC 138 46","2.2.4"}, |
| 138 |
{sasl,"SASL CXC 138 11","2.1.8"}, |
| 139 |
{stdlib,"ERTS CXC 138 10","1.16.4"}, |
| 140 |
{kernel,"ERTS CXC 138 10","2.13.4"}]}, |
| 141 |
{nodes,[rabbit@myhost]}, |
| 142 |
{running_nodes,[rabbit@myhost]}] |
| 143 |
...done. |
| 144 |
|
| 145 |
This is especially important if your DHCP server gives you a host name |
| 146 |
starting with an IP address, (e.g., `23.10.112.31.comcast.net`). In this |
| 147 |
case RabbitMQ will try to use `rabbit@23`: an illegal host name. |
| 148 |
|
| 149 |
.. _rabbitmq-macOS-start-stop: |
| 150 |
|
| 151 |
Starting/Stopping the RabbitMQ server |
| 152 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 153 |
|
| 154 |
To start the server: |
| 155 |
|
| 156 |
.. code-block:: console |
| 157 |
|
| 158 |
$ sudo rabbitmq-server |
| 159 |
|
| 160 |
you can also run it in the background by adding the ``-detached`` option |
| 161 |
(note: only one dash): |
| 162 |
|
| 163 |
.. code-block:: console |
| 164 |
|
| 165 |
$ sudo rabbitmq-server -detached |
| 166 |
|
| 167 |
Never use :command:`kill` (:manpage:`kill(1)`) to stop the RabbitMQ server, |
| 168 |
but rather use the :command:`rabbitmqctl` command: |
| 169 |
|
| 170 |
.. code-block:: console |
| 171 |
|
| 172 |
$ sudo rabbitmqctl stop |
| 173 |
|
| 174 |
When the server is running, you can continue reading `Setting up RabbitMQ`_. |
| 175 |
|
| 176 |
.. _using-quorum-queues: |
| 177 |
|
| 178 |
Using Quorum Queues |
| 179 |
=================== |
| 180 |
|
| 181 |
.. versionadded:: 5.5 |
| 182 |
|
| 183 |
.. warning:: |
| 184 |
|
| 185 |
Quorum Queues require disabling global QoS which means some features won't work as expected. |
| 186 |
See `limitations`_ for details. |
| 187 |
|
| 188 |
Celery supports `Quorum Queues`_ by setting the ``x-queue-type`` header to ``quorum`` like so: |
| 189 |
|
| 190 |
.. code-block:: python |
| 191 |
|
| 192 |
from kombu import Queue |
| 193 |
|
| 194 |
task_queues = [Queue('my-queue', queue_arguments={'x-queue-type': 'quorum'})] |
| 195 |
broker_transport_options = {"confirm_publish": True} |
| 196 |
|
| 197 |
If you'd like to change the type of the default queue, set the :setting:`task_default_queue_type` setting to ``quorum``. |
| 198 |
|
| 199 |
Another way to configure `Quorum Queues`_ is by relying on default settings and using ``task_routes``: |
| 200 |
|
| 201 |
.. code-block:: python |
| 202 |
|
| 203 |
task_default_queue_type = "quorum" |
| 204 |
task_default_exchange_type = "topic" |
| 205 |
task_default_queue = "my-queue" |
| 206 |
broker_transport_options = {"confirm_publish": True} |
| 207 |
|
| 208 |
task_routes = { |
| 209 |
"*": { |
| 210 |
"routing_key": "my-queue", |
| 211 |
}, |
| 212 |
} |
| 213 |
|
| 214 |
Celery automatically detects if quorum queues are used using the :setting:`worker_detect_quorum_queues` setting. |
| 215 |
We recommend to keep the default behavior turned on. |
| 216 |
|
| 217 |
To migrate from classic mirrored queues to quorum queues, please refer to RabbitMQ's `documentation <https://www.rabbitmq.com/blog/2023/03/02/quorum-queues-migration>`_ on the subject. |
| 218 |
|
| 219 |
.. _`Quorum Queues`: https://www.rabbitmq.com/docs/quorum-queues |
| 220 |
|
| 221 |
.. _limitations: |
| 222 |
|
| 223 |
Limitations |
| 224 |
----------- |
| 225 |
|
| 226 |
Disabling global QoS means that the per-channel QoS is now static. |
| 227 |
This means that some Celery features won't work when using Quorum Queues. |
| 228 |
|
| 229 |
Autoscaling relies on increasing and decreasing the prefetch count whenever a new process is instantiated |
| 230 |
or terminated so it won't work when Quorum Queues are detected. |
| 231 |
|
| 232 |
Similarly, the :setting:`worker_enable_prefetch_count_reduction` setting will be a no-op even when set to ``True`` |
| 233 |
when Quorum Queues are detected. |
| 234 |
|
| 235 |
In addition, :ref:`ETA/Countdown <calling-eta>` will block the worker when received until the ETA arrives since |
| 236 |
we can no longer increase the prefetch count and fetch another task from the queue. |
| 237 |
|
| 238 |
In order to properly schedule ETA/Countdown tasks we automatically detect if quorum queues are used |
| 239 |
and in case they are, Celery automatically enables :ref:`Native Delayed Delivery <native-delayed-delivery>`. |
| 240 |
|
| 241 |
.. _native-delayed-delivery: |
| 242 |
|
| 243 |
Native Delayed Delivery |
| 244 |
----------------------- |
| 245 |
|
| 246 |
Since tasks with ETA/Countdown will block the worker until they are scheduled for execution, |
| 247 |
we need to use RabbitMQ's native capabilities to schedule the execution of tasks. |
| 248 |
|
| 249 |
The design is borrowed from NServiceBus. If you are interested in the implementation details, refer to their `documentation`_. |
| 250 |
|
| 251 |
.. _documentation: https://docs.particular.net/transports/rabbitmq/delayed-delivery |
| 252 |
|
| 253 |
Native Delayed Delivery is automatically enabled when quorum queues are detected. |
| 254 |
|
| 255 |
By default the Native Delayed Delivery queues are quorum queues. |
| 256 |
If you'd like to change them to classic queues you can set the :setting:`broker_native_delayed_delivery_queue_type` |
| 257 |
to classic. |