routing.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. ===============
  2. Routing Tasks
  3. ===============
  4. **NOTE** This document refers to functionality only available in brokers
  5. using AMQP. Other brokers may implement some functionality, see their
  6. respective documenation for more information, or contact the `mailinglist`_.
  7. .. _`mailinglist`: http://groups.google.com/group/celery-users
  8. .. contents::
  9. :local:
  10. Basics
  11. ======
  12. Say you have two servers, ``x``, and ``y`` that handles regular tasks,
  13. and one server ``z``, that only handles feed related tasks, you can use this
  14. configuration:
  15. .. code-block:: python
  16. CELERY_DEFAULT_QUEUE = "default"
  17. CELERY_QUEUES = {
  18. "default": {
  19. "binding_key": "task.#",
  20. },
  21. "feed_tasks": {
  22. "binding_key": "feed.#",
  23. },
  24. }
  25. CELERY_DEFAULT_EXCHANGE = "tasks"
  26. CELERY_DEFAULT_EXCHANGE_TYPE = "topic"
  27. CELERY_DEFAULT_ROUTING_KEY = "task.default"
  28. ``CELERY_QUEUES`` is a map of queue names and their exchange/type/binding_key,
  29. if you don't set exchange or exchange type, they will be taken from the
  30. ``CELERY_DEFAULT_EXCHANGE``/``CELERY_DEFAULT_EXCHANGE_TYPE`` settings.
  31. To route a task to the ``feed_tasks`` queue, you can add an entry in the
  32. ``CELERY_ROUTES`` setting:
  33. .. code-block:: python
  34. CELERY_ROUTES = {
  35. "feeds.tasks.import_feed": {
  36. "queue": "feed_tasks",
  37. "routing_key": "feed.import",
  38. },
  39. }
  40. You can also override this using the ``routing_key`` argument to
  41. :func:`~celery.execute.apply_async`, or :func:`~celery.execute.send_task`:
  42. >>> from feeds.tasks import import_feed
  43. >>> import_feed.apply_async(args=["http://cnn.com/rss"],
  44. ... queue="feed_tasks",
  45. ... routing_key="feed.import")
  46. To make server ``z`` consume from the feed queue exclusively you can
  47. start it with the ``-Q`` option::
  48. (z)$ celeryd -Q feed_tasks --hostname=z.example.com
  49. Servers ``x`` and ``y`` must be configured to consume from the default queue::
  50. (x)$ celeryd -Q default --hostname=x.example.com
  51. (y)$ celeryd -Q default --hostname=y.example.com
  52. If you want, you can even have your feed processing worker handle regular
  53. tasks as well, maybe in times when there's a lot of work to do::
  54. (z)$ celeryd -Q feed_tasks,default --hostname=z.example.com
  55. If you have another queue but on another exchange you want to add,
  56. just specify a custom exchange and exchange type:
  57. .. code-block:: python
  58. CELERY_QUEUES = {
  59. "feed_tasks": {
  60. "binding_key": "feed.#",
  61. },
  62. "regular_tasks": {
  63. "binding_key": "task.#",
  64. }
  65. "image_tasks": {
  66. "binding_key": "image.compress",
  67. "exchange": "mediatasks",
  68. "exchange_type": "direct",
  69. },
  70. }
  71. If you're confused about these terms, you should read up on AMQP concepts.
  72. In addition to the :ref:`AMQP Primer` below, there's
  73. `Rabbits and Warrens`_, an excellent blog post describing queues and
  74. exchanges. There's also AMQP in 10 minutes*: `Flexible Routing Model`_,
  75. and `Standard Exchange Types`_. For users of RabbitMQ the `RabbitMQ FAQ`_
  76. could be useful as a source of information.
  77. .. _`Rabbits and Warrens`: http://blogs.digitar.com/jjww/2009/01/rabbits-and-warrens/
  78. .. _`Flexible Routing Model`: http://bit.ly/95XFO1
  79. .. _`Standard Exchange Types`: http://bit.ly/EEWca
  80. .. _`RabbitMQ FAQ`: http://www.rabbitmq.com/faq.html
  81. .. _`AMQP Primer`:
  82. AMQP Primer
  83. ===========
  84. Messages
  85. --------
  86. A message consists of headers and a body. Celery uses headers to store
  87. the content type of the message and its content encoding. In Celery the
  88. content type is usually the serialization format used to serialize the
  89. message, and the body contains the name of the task to execute, the
  90. task id (UUID), the arguments to execute it with and some additional
  91. metadata - like the number of retries and its ETA (if any).
  92. This is an example task message represented as a Python dictionary:
  93. .. code-block:: python
  94. {"task": "myapp.tasks.add",
  95. "id":
  96. "args": [4, 4],
  97. "kwargs": {}}
  98. Producers, consumers and brokers
  99. --------------------------------
  100. The client sending messages is typically called a *publisher*, or
  101. a *producer*, while the entity receiving messages is called
  102. a *consumer*.
  103. The *broker* is the message server, routing messages from producers
  104. to consumers.
  105. You are likely to see these terms used a lot in AMQP related material.
  106. Exchanges, queues and routing keys.
  107. -----------------------------------
  108. 1. Messages are sent to exchanges.
  109. 2. An exchange routes messages to one or more queues. Several exchange types
  110. exists, providing different ways to do routing.
  111. 3. The message waits in the queue until someone consumes from it.
  112. 4. The message is deleted from the queue when it has been acknowledged.
  113. The steps required to send and receive messages are:
  114. 1. Create an exchange
  115. 2. Create a queue
  116. 3. Bind the queue to the exchange.
  117. Celery automatically creates the entities necessary for the queues in
  118. ``CELERY_QUEUES`` to work (except if the queue's ``auto_declare`` setting
  119. is set to :const:`False`).
  120. Here's an example queue configuration with three queues;
  121. One for video, one for images and finally, one default queue for everything else:
  122. .. code-block:: python
  123. CELERY_QUEUES = {
  124. "default": {
  125. "exchange": "default",
  126. "binding_key": "default"},
  127. "videos": {
  128. "exchange": "media",
  129. "binding_key": "media.video",
  130. },
  131. "images": {
  132. "exchange": "media",
  133. "binding_key": "media.image",
  134. }
  135. }
  136. CELERY_DEFAULT_QUEUE = "default"
  137. CELERY_DEFAULT_EXCHANGE_TYPE = "direct"
  138. CELERY_DEFAULT_ROUTING_KEY = "default"
  139. **NOTE**: In Celery the ``routing_key`` is the key used to send the message,
  140. while ``binding_key`` is the key the queue is bound with. In the AMQP API
  141. they are both referred to as the routing key.
  142. Exchange types
  143. --------------
  144. The exchange type defines how the messages are routed through the exchange.
  145. The exchange types defined in the standard are ``direct``, ``topic``,
  146. ``fanout`` and ``headers``. Also non-standard exchange types are available
  147. as plugins to RabbitMQ, like the `last-value-cache plug-in`_ by Michael
  148. Bridgen.
  149. .. _`last-value-cache plug-in`:
  150. http://github.com/squaremo/rabbitmq-lvc-plugin
  151. Direct exchanges
  152. ~~~~~~~~~~~~~~~~
  153. Direct exchanges match by exact routing keys, so a queue bound with
  154. the routing key ``video`` only receives messages with the same routing key.
  155. Topic exchanges
  156. ~~~~~~~~~~~~~~~
  157. Topic exchanges matches routing keys using dot-separated words, and can
  158. include wildcard characters: ``*`` matches a single word, ``#`` matches
  159. zero or more words.
  160. With routing keys like ``usa.news``, ``usa.weather``, ``norway.news`` and
  161. ``norway.weather``, bindings could be ``*.news`` (all news), ``usa.#`` (all
  162. items in the USA) or ``usa.weather`` (all USA weather items).
  163. Related API commands
  164. --------------------
  165. exchange.declare(exchange_name, type, passive, durable, auto_delete, internal)
  166. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  167. Declares an exchange by name.
  168. * ``passive`` means the exchange won't be created, but you can use this to
  169. check if the exchange already exists.
  170. * Durable exchanges are persistent. That is - they survive a broker restart.
  171. * ``auto_delete`` means the queue will be deleted by the broker when there
  172. are no more queues using it.
  173. queue.declare(queue_name, passive, durable, exclusive, auto_delete)
  174. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  175. Declares a queue by name.
  176. * exclusive queues can only be consumed from by the current connection.
  177. implies ``auto_delete``.
  178. queue.bind(queue_name, exchange_name, routing_key)
  179. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  180. Binds a queue to an exchange with a routing key.
  181. Unbound queues will not receive messages, so this is necessary.
  182. queue.delete(name, if_unused, if_empty)
  183. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  184. Deletes a queue and its binding.
  185. exchange.delete(name, if_unused)
  186. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  187. Deletes an exchange.
  188. **NOTE**: Declaring does not necessarily mean "create". When you declare you
  189. *assert* that the entity exists and that it's operable. There is no rule as to
  190. whom should initially create the exchange/queue/binding, whether consumer
  191. or producer. Usually the first one to need it will be the one to create it.
  192. Hands-on with the API
  193. ---------------------
  194. Celery comes with a tool called ``camqadm`` (short for celery AMQP admin).
  195. It's used for simple admnistration tasks like creating/deleting queues and
  196. exchanges, purging queues and sending messages. In short it's for simple
  197. command-line access to the AMQP API.
  198. You can write commands directly in the arguments to ``camqadm``, or just start
  199. with no arguments to start it in shell-mode::
  200. $ camqadm
  201. -> connecting to amqp://guest@localhost:5672/.
  202. -> connected.
  203. 1>
  204. Here ``1>`` is the prompt. The number is counting the number of commands you
  205. have executed. Type ``help`` for a list of commands. It also has
  206. autocompletion, so you can start typing a command and then hit the
  207. ``tab`` key to show a list of possible matches.
  208. Now let's create a queue we can send messages to::
  209. 1> exchange.declare testexchange direct
  210. ok.
  211. 2> queue.declare testqueue
  212. ok. queue:testqueue messages:0 consumers:0.
  213. 3> queue.bind testqueue testexchange testkey
  214. ok.
  215. This created the direct exchange ``testexchange``, and a queue
  216. named ``testqueue``. The queue is bound to the exchange using
  217. the routing key ``testkey``.
  218. From now on all messages sent to the exchange ``testexchange`` with routing
  219. key ``testkey`` will be moved to this queue. We can send a message by
  220. using the ``basic.publish`` command::
  221. 4> basic.publish "This is a message!" testexchange testkey
  222. ok.
  223. Now that the message is sent we can retrieve it again. We use the
  224. ``basic.get`` command here, which pops a single message off the queue,
  225. this command is not recommended for production as it implies polling, any
  226. real application would declare consumers instead.
  227. Pop a message off the queue::
  228. 5> basic.get testqueue
  229. {'body': 'This is a message!',
  230. 'delivery_info': {'delivery_tag': 1,
  231. 'exchange': u'testexchange',
  232. 'message_count': 0,
  233. 'redelivered': False,
  234. 'routing_key': u'testkey'},
  235. 'properties': {}}
  236. AMQP uses acknowledgment to signify that a message has been received
  237. and processed successfully. The message is sent to the next receiver
  238. if it has not been acknowledged before the client connection is closed.
  239. Note the delivery tag listed in the structure above; Within a connection channel,
  240. every received message has a unique delivery tag,
  241. This tag is used to acknowledge the message. Note that
  242. delivery tags are not unique across connections, so in another client
  243. the delivery tag ``1`` might point to a different message than in this channel.
  244. You can acknowledge the message we received using ``basic.ack``::
  245. 6> basic.ack 1
  246. ok.
  247. To clean up after our test session we should delete the entities we created::
  248. 7> queue.delete testqueue
  249. ok. 0 messages deleted.
  250. 8> exchange.delete testexchange
  251. ok.
  252. Routing Tasks
  253. =============
  254. Defining queues
  255. ---------------
  256. In Celery the queues are defined by the ``CELERY_QUEUES`` setting.
  257. Here's an example queue configuration with three queues;
  258. One for video, one for images and finally, one default queue for everything else:
  259. .. code-block:: python
  260. CELERY_QUEUES = {
  261. "default": {
  262. "exchange": "default",
  263. "binding_key": "default"},
  264. "videos": {
  265. "exchange": "media",
  266. "exchange_type": "topic",
  267. "binding_key": "media.video",
  268. },
  269. "images": {
  270. "exchange": "media",
  271. "exchange_type": "topic",
  272. "binding_key": "media.image",
  273. }
  274. }
  275. CELERY_DEFAULT_QUEUE = "default"
  276. CELERY_DEFAULT_EXCHANGE = "default"
  277. CELERY_DEFAULT_EXCHANGE_TYPE = "direct"
  278. CELERY_DEFAULT_ROUTING_KEY = "default"
  279. Here, the ``CELERY_DEFAULT_QUEUE`` will be used to route tasks that doesn't
  280. have an explicit route.
  281. The default exchange, exchange type and routing key will be used as the
  282. default routing values for tasks, and as the default values for entries
  283. in ``CELERY_QUEUES``.
  284. Specifying task destination
  285. ---------------------------
  286. The destination for a task is decided by the following (in order):
  287. 1. The :ref:`routers` defined in ``CELERY_ROUTES``.
  288. 2. The routing arguments to :func:`~celery.execute.apply_async`.
  289. 3. Routing related attributes defined on the :class:`~celery.task.base.Task` itself.
  290. It is considered best practice to not hard-code these settings, but rather
  291. leave that as configuration options by using :ref:`routers`;
  292. This is the most flexible approach, but sensible defaults can still be set
  293. as task attributes.
  294. .. _routers:
  295. Routers
  296. -------
  297. A router is a class that decides the routing options for a task.
  298. All you need to define a new router is to create a class with a
  299. ``route_for_task`` method:
  300. .. code-block:: python
  301. class MyRouter(object):
  302. def route_for_task(task, task_id=None, args=None, kwargs=None):
  303. if task == "myapp.tasks.compress_video":
  304. return {"exchange": "video",
  305. "exchange_type": "topic",
  306. "routing_key": "video.compress"}
  307. If you return the ``queue`` key, it will expand with the defined settings of
  308. that queue in ``CELERY_QUEUES``::
  309. {"queue": "video", "routing_key": "video.compress"}
  310. becomes -->
  311. {"queue": "video",
  312. "exchange": "video",
  313. "exchange_type": "topic",
  314. "routing_key": "video.compress"}
  315. You install router classes by adding it to the ``CELERY_ROUTES`` setting::
  316. CELERY_ROUTES = (MyRouter, )
  317. Router classes can also be added by name::
  318. CELERY_ROUTES = ("myapp.routers.MyRouter", )
  319. For simple task name -> route mappings like the router example above, you can simply
  320. drop a dict into ``CELERY_ROUTES`` to get the same result::
  321. CELERY_ROUTES = ({"myapp.tasks.compress_video": {
  322. "queue": "video",
  323. "routing_key": "video.compress"}}, )
  324. The routers will then be traversed in order, it will stop at the first router
  325. returning a value and use that as the final route for the task.