routing.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. .. _guide-routing:
  2. ===============
  3. Routing Tasks
  4. ===============
  5. .. note::
  6. Alternate routing concepts like topic and fanout is not
  7. available for all transports, please consult the
  8. :ref:`transport comparison table <kombu:transport-comparison>`.
  9. .. contents::
  10. :local:
  11. .. _routing-basics:
  12. Basics
  13. ======
  14. .. _routing-automatic:
  15. Automatic routing
  16. -----------------
  17. The simplest way to do routing is to use the
  18. :setting:`task_create_missing_queues` setting (on by default).
  19. With this setting on, a named queue that's not already defined in
  20. :setting:`task_queues` will be created automatically. This makes it easy to
  21. perform simple routing tasks.
  22. Say you have two servers, `x`, and `y` that handles regular tasks,
  23. and one server `z`, that only handles feed related tasks. You can use this
  24. configuration::
  25. task_routes = {'feed.tasks.import_feed': {'queue': 'feeds'}}
  26. With this route enabled import feed tasks will be routed to the
  27. `"feeds"` queue, while all other tasks will be routed to the default queue
  28. (named `"celery"` for historical reasons).
  29. Alternatively, you can use glob pattern matching, or even regular expressions,
  30. to match all tasks in the ``feed.tasks`` name-space:
  31. .. code-block:: python
  32. app.conf.task_routes = {'feed.tasks.*': {'queue': 'feeds'}}
  33. If the order of matching patterns is important you should
  34. specify the router in *items* format instead:
  35. .. code-block:: python
  36. task_routes = ([
  37. ('feed.tasks.*': {'queue': 'feeds'}),
  38. ('web.tasks.*': {'queue': 'web'}),
  39. (re.compile(r'(video|image)\.tasks\..*'), {'queue': 'media'}),
  40. ],)
  41. .. note::
  42. The :setting:`task_routes` setting can either be a dictionary, or a
  43. list of router objects, so in this case we need to specify the setting
  44. as a tuple containing a list.
  45. After installing the router, you can start server `z` to only process the feeds
  46. queue like this:
  47. .. code-block:: console
  48. user@z:/$ celery -A proj worker -Q feeds
  49. You can specify as many queues as you want, so you can make this server
  50. process the default queue as well:
  51. .. code-block:: console
  52. user@z:/$ celery -A proj worker -Q feeds,celery
  53. .. _routing-changing-default-queue:
  54. Changing the name of the default queue
  55. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  56. You can change the name of the default queue by using the following
  57. configuration:
  58. .. code-block:: python
  59. app.conf.task_default_queue = 'default'
  60. .. _routing-autoqueue-details:
  61. How the queues are defined
  62. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  63. The point with this feature is to hide the complex AMQP protocol for users
  64. with only basic needs. However -- you may still be interested in how these queues
  65. are declared.
  66. A queue named `"video"` will be created with the following settings:
  67. .. code-block:: javascript
  68. {'exchange': 'video',
  69. 'exchange_type': 'direct',
  70. 'routing_key': 'video'}
  71. The non-AMQP backends like `Redis` or `SQS` don't support exchanges,
  72. so they require the exchange to have the same name as the queue. Using this
  73. design ensures it will work for them as well.
  74. .. _routing-manual:
  75. Manual routing
  76. --------------
  77. Say you have two servers, `x`, and `y` that handles regular tasks,
  78. and one server `z`, that only handles feed related tasks, you can use this
  79. configuration:
  80. .. code-block:: python
  81. from kombu import Queue
  82. app.conf.task_default_queue = 'default'
  83. app.conf.task_queues = (
  84. Queue('default', routing_key='task.#'),
  85. Queue('feed_tasks', routing_key='feed.#'),
  86. )
  87. task_default_exchange = 'tasks'
  88. task_default_exchange_type = 'topic'
  89. task_default_routing_key = 'task.default'
  90. :setting:`task_queues` is a list of :class:`~kombu.entitity.Queue`
  91. instances.
  92. If you don't set the exchange or exchange type values for a key, these
  93. will be taken from the :setting:`task_default_exchange` and
  94. :setting:`task_default_exchange_type` settings.
  95. To route a task to the `feed_tasks` queue, you can add an entry in the
  96. :setting:`task_routes` setting:
  97. .. code-block:: python
  98. task_routes = {
  99. 'feeds.tasks.import_feed': {
  100. 'queue': 'feed_tasks',
  101. 'routing_key': 'feed.import',
  102. },
  103. }
  104. You can also override this using the `routing_key` argument to
  105. :meth:`Task.apply_async`, or :func:`~celery.execute.send_task`:
  106. >>> from feeds.tasks import import_feed
  107. >>> import_feed.apply_async(args=['http://cnn.com/rss'],
  108. ... queue='feed_tasks',
  109. ... routing_key='feed.import')
  110. To make server `z` consume from the feed queue exclusively you can
  111. start it with the :option:`celery worker -Q` option:
  112. .. code-block:: console
  113. user@z:/$ celery -A proj worker -Q feed_tasks --hostname=z@%h
  114. Servers `x` and `y` must be configured to consume from the default queue:
  115. .. code-block:: console
  116. user@x:/$ celery -A proj worker -Q default --hostname=x@%h
  117. user@y:/$ celery -A proj worker -Q default --hostname=y@%h
  118. If you want, you can even have your feed processing worker handle regular
  119. tasks as well, maybe in times when there's a lot of work to do:
  120. .. code-block:: console
  121. user@z:/$ celery -A proj worker -Q feed_tasks,default --hostname=z@%h
  122. If you have another queue but on another exchange you want to add,
  123. just specify a custom exchange and exchange type:
  124. .. code-block:: python
  125. from kombu import Exchange, Queue
  126. app.conf.task_queues = (
  127. Queue('feed_tasks', routing_key='feed.#'),
  128. Queue('regular_tasks', routing_key='task.#'),
  129. Queue('image_tasks', exchange=Exchange('mediatasks', type='direct'),
  130. routing_key='image.compress'),
  131. )
  132. If you're confused about these terms, you should read up on AMQP.
  133. .. seealso::
  134. In addition to the :ref:`amqp-primer` below, there's
  135. `Rabbits and Warrens`_, an excellent blog post describing queues and
  136. exchanges. There's also The `CloudAMQP tutorial`,
  137. For users of RabbitMQ the `RabbitMQ FAQ`_
  138. could be useful as a source of information.
  139. .. _`Rabbits and Warrens`: http://blogs.digitar.com/jjww/2009/01/rabbits-and-warrens/
  140. .. _`CloudAMQP tutorial`: amqp in 10 minutes part 3
  141. https://www.cloudamqp.com/blog/2015-09-03-part4-rabbitmq-for-beginners-exchanges-routing-keys-bindings.html
  142. .. _`RabbitMQ FAQ`: http://www.rabbitmq.com/faq.html
  143. .. _routing-special_options:
  144. Special Routing Options
  145. =======================
  146. .. _routing-options-rabbitmq-priorities:
  147. RabbitMQ Message Priorities
  148. ---------------------------
  149. :supported transports: RabbitMQ
  150. .. versionadded:: 4.0
  151. Queues can be configured to support priorities by setting the
  152. ``x-max-priority`` argument:
  153. .. code-block:: python
  154. from kombu import Exchange, Queue
  155. app.conf.task_queues = [
  156. Queue('tasks', Exchange('tasks'), routing_key='tasks',
  157. queue_arguments={'x-max-priority': 10},
  158. ]
  159. A default value for all queues can be set using the
  160. :setting:`task_queue_max_priority` setting:
  161. .. code-block:: python
  162. app.conf.task_queue_max_priority = 10
  163. .. _amqp-primer:
  164. AMQP Primer
  165. ===========
  166. Messages
  167. --------
  168. A message consists of headers and a body. Celery uses headers to store
  169. the content type of the message and its content encoding. The
  170. content type is usually the serialization format used to serialize the
  171. message. The body contains the name of the task to execute, the
  172. task id (UUID), the arguments to apply it with and some additional
  173. meta-data -- like the number of retries or an ETA.
  174. This is an example task message represented as a Python dictionary:
  175. .. code-block:: javascript
  176. {'task': 'myapp.tasks.add',
  177. 'id': '54086c5e-6193-4575-8308-dbab76798756',
  178. 'args': [4, 4],
  179. 'kwargs': {}}
  180. .. _amqp-producers-consumers-brokers:
  181. Producers, consumers, and brokers
  182. ---------------------------------
  183. The client sending messages is typically called a *producer*,
  184. while the entity receiving messages is called a *consumer*.
  185. The *broker* is the message server, routing messages from producers
  186. to consumers.
  187. You're likely to see these terms used a lot in AMQP related material.
  188. .. _amqp-exchanges-queues-keys:
  189. Exchanges, queues, and routing keys
  190. -----------------------------------
  191. 1. Messages are sent to exchanges.
  192. 2. An exchange routes messages to one or more queues. Several exchange types
  193. exists, providing different ways to do routing, or implementing
  194. different messaging scenarios.
  195. 3. The message waits in the queue until someone consumes it.
  196. 4. The message is deleted from the queue when it has been acknowledged.
  197. The steps required to send and receive messages are:
  198. 1. Create an exchange
  199. 2. Create a queue
  200. 3. Bind the queue to the exchange.
  201. Celery automatically creates the entities necessary for the queues in
  202. :setting:`task_queues` to work (except if the queue's `auto_declare`
  203. setting is set to :const:`False`).
  204. Here's an example queue configuration with three queues;
  205. One for video, one for images, and one default queue for everything else:
  206. .. code-block:: python
  207. from kombu import Exchange, Queue
  208. app.conf.task_queues = (
  209. Queue('default', Exchange('default'), routing_key='default'),
  210. Queue('videos', Exchange('media'), routing_key='media.video'),
  211. Queue('images', Exchange('media'), routing_key='media.image'),
  212. )
  213. app.conf.task_default_queue = 'default'
  214. app.conf.task_default_exchange_type = 'direct'
  215. app.conf.task_default_routing_key = 'default'
  216. .. _amqp-exchange-types:
  217. Exchange types
  218. --------------
  219. The exchange type defines how the messages are routed through the exchange.
  220. The exchange types defined in the standard are `direct`, `topic`,
  221. `fanout` and `headers`. Also non-standard exchange types are available
  222. as plug-ins to RabbitMQ, like the `last-value-cache plug-in`_ by Michael
  223. Bridgen.
  224. .. _`last-value-cache plug-in`:
  225. https://github.com/squaremo/rabbitmq-lvc-plugin
  226. .. _amqp-exchange-type-direct:
  227. Direct exchanges
  228. ~~~~~~~~~~~~~~~~
  229. Direct exchanges match by exact routing keys, so a queue bound by
  230. the routing key `video` only receives messages with that routing key.
  231. .. _amqp-exchange-type-topic:
  232. Topic exchanges
  233. ~~~~~~~~~~~~~~~
  234. Topic exchanges matches routing keys using dot-separated words, and the
  235. wild-card characters: ``*`` (matches a single word), and ``#`` (matches
  236. zero or more words).
  237. With routing keys like ``usa.news``, ``usa.weather``, ``norway.news``, and
  238. ``norway.weather``, bindings could be ``*.news`` (all news), ``usa.#`` (all
  239. items in the USA), or ``usa.weather`` (all USA weather items).
  240. .. _amqp-api:
  241. Related API commands
  242. --------------------
  243. .. method:: exchange.declare(exchange_name, type, passive,
  244. durable, auto_delete, internal)
  245. Declares an exchange by name.
  246. See :meth:`amqp:Channel.exchange_declare <amqp.channel.Channel.exchange_declare>`.
  247. :keyword passive: Passive means the exchange won't be created, but you
  248. can use this to check if the exchange already exists.
  249. :keyword durable: Durable exchanges are persistent (i.e., they survive
  250. a broker restart).
  251. :keyword auto_delete: This means the queue will be deleted by the broker
  252. when there are no more queues using it.
  253. .. method:: queue.declare(queue_name, passive, durable, exclusive, auto_delete)
  254. Declares a queue by name.
  255. See :meth:`amqp:Channel.queue_declare <amqp.channel.Channel.queue_declare>`
  256. Exclusive queues can only be consumed from by the current connection.
  257. Exclusive also implies `auto_delete`.
  258. .. method:: queue.bind(queue_name, exchange_name, routing_key)
  259. Binds a queue to an exchange with a routing key.
  260. Unbound queues won't receive messages, so this is necessary.
  261. See :meth:`amqp:Channel.queue_bind <amqp.channel.Channel.queue_bind>`
  262. .. method:: queue.delete(name, if_unused=False, if_empty=False)
  263. Deletes a queue and its binding.
  264. See :meth:`amqp:Channel.queue_delete <amqp.channel.Channel.queue_delete>`
  265. .. method:: exchange.delete(name, if_unused=False)
  266. Deletes an exchange.
  267. See :meth:`amqp:Channel.exchange_delete <amqp.channel.Channel.exchange_delete>`
  268. .. note::
  269. Declaring doesn't necessarily mean "create". When you declare you
  270. *assert* that the entity exists and that it's operable. There's no
  271. rule as to whom should initially create the exchange/queue/binding,
  272. whether consumer or producer. Usually the first one to need it will
  273. be the one to create it.
  274. .. _amqp-api-hands-on:
  275. Hands-on with the API
  276. ---------------------
  277. Celery comes with a tool called :program:`celery amqp`
  278. that's used for command line access to the AMQP API, enabling access to
  279. administration tasks like creating/deleting queues and exchanges, purging
  280. queues or sending messages. It can also be used for non-AMQP brokers,
  281. but different implementation may not implement all commands.
  282. You can write commands directly in the arguments to :program:`celery amqp`,
  283. or just start with no arguments to start it in shell-mode:
  284. .. code-block:: console
  285. $ celery -A proj amqp
  286. -> connecting to amqp://guest@localhost:5672/.
  287. -> connected.
  288. 1>
  289. Here ``1>`` is the prompt. The number 1, is the number of commands you
  290. have executed so far. Type ``help`` for a list of commands available.
  291. It also supports auto-completion, so you can start typing a command and then
  292. hit the `tab` key to show a list of possible matches.
  293. Let's create a queue you can send messages to:
  294. .. code-block:: console
  295. $ celery -A proj amqp
  296. 1> exchange.declare testexchange direct
  297. ok.
  298. 2> queue.declare testqueue
  299. ok. queue:testqueue messages:0 consumers:0.
  300. 3> queue.bind testqueue testexchange testkey
  301. ok.
  302. This created the direct exchange ``testexchange``, and a queue
  303. named ``testqueue``. The queue is bound to the exchange using
  304. the routing key ``testkey``.
  305. From now on all messages sent to the exchange ``testexchange`` with routing
  306. key ``testkey`` will be moved to this queue. You can send a message by
  307. using the ``basic.publish`` command:
  308. .. code-block:: console
  309. 4> basic.publish 'This is a message!' testexchange testkey
  310. ok.
  311. Now that the message is sent you can retrieve it again. You can use the
  312. ``basic.get``` command here, that polls for new messages on the queue
  313. in a synchronous manner
  314. (this is OK for maintenance tasks, but for services you want to use
  315. ``basic.consume`` instead)
  316. Pop a message off the queue:
  317. .. code-block:: console
  318. 5> basic.get testqueue
  319. {'body': 'This is a message!',
  320. 'delivery_info': {'delivery_tag': 1,
  321. 'exchange': u'testexchange',
  322. 'message_count': 0,
  323. 'redelivered': False,
  324. 'routing_key': u'testkey'},
  325. 'properties': {}}
  326. AMQP uses acknowledgment to signify that a message has been received
  327. and processed successfully. If the message hasn't been acknowledged
  328. and consumer channel is closed, the message will be delivered to
  329. another consumer.
  330. Note the delivery tag listed in the structure above; Within a connection
  331. channel, every received message has a unique delivery tag,
  332. This tag is used to acknowledge the message. Also note that
  333. delivery tags aren't unique across connections, so in another client
  334. the delivery tag `1` might point to a different message than in this channel.
  335. You can acknowledge the message you received using ``basic.ack``:
  336. .. code-block:: console
  337. 6> basic.ack 1
  338. ok.
  339. To clean up after our test session you should delete the entities you created:
  340. .. code-block:: console
  341. 7> queue.delete testqueue
  342. ok. 0 messages deleted.
  343. 8> exchange.delete testexchange
  344. ok.
  345. .. _routing-tasks:
  346. Routing Tasks
  347. =============
  348. .. _routing-defining-queues:
  349. Defining queues
  350. ---------------
  351. In Celery available queues are defined by the :setting:`task_queues` setting.
  352. Here's an example queue configuration with three queues;
  353. One for video, one for images, and one default queue for everything else:
  354. .. code-block:: python
  355. default_exchange = Exchange('default', type='direct')
  356. media_exchange = Exchange('media', type='direct')
  357. app.conf.task_queues = (
  358. Queue('default', default_exchange, routing_key='default'),
  359. Queue('videos', media_exchange, routing_key='media.video'),
  360. Queue('images', media_exchange, routing_key='media.image')
  361. )
  362. app.conf.task_default_queue = 'default'
  363. app.conf.task_default_exchange = 'default'
  364. app.conf.task_default_routing_key = 'default'
  365. Here, the :setting:`task_default_queue` will be used to route tasks that
  366. doesn't have an explicit route.
  367. The default exchange, exchange type, and routing key will be used as the
  368. default routing values for tasks, and as the default values for entries
  369. in :setting:`task_queues`.
  370. Multiple bindings to a single queue are also supported. Here's an example
  371. of two routing keys that are both bound to the same queue:
  372. .. code-block:: python
  373. from kombu import Exchange, Queue, binding
  374. media_exchange = Exchange('media', type='direct')
  375. CELERY_QUEUES = (
  376. Queue('media', [
  377. binding(media_exchange, routing_key='media.video'),
  378. binding(media_exchange, routing_key='media.image'),
  379. ]),
  380. )
  381. .. _routing-task-destination:
  382. Specifying task destination
  383. ---------------------------
  384. The destination for a task is decided by the following (in order):
  385. 1. The :ref:`routers` defined in :setting:`task_routes`.
  386. 2. The routing arguments to :func:`Task.apply_async`.
  387. 3. Routing related attributes defined on the :class:`~celery.task.base.Task`
  388. itself.
  389. It's considered best practice to not hard-code these settings, but rather
  390. leave that as configuration options by using :ref:`routers`;
  391. This is the most flexible approach, but sensible defaults can still be set
  392. as task attributes.
  393. .. _routers:
  394. Routers
  395. -------
  396. A router is a function that decides the routing options for a task.
  397. All you need to define a new router is to define a function with
  398. the signature ``(name, args, kwargs, options, task=None, **kw)``:
  399. .. code-block:: python
  400. def route_task(name, args, kwargs, options, task=None, **kw):
  401. if name == 'myapp.tasks.compress_video':
  402. return {'exchange': 'video',
  403. 'exchange_type': 'topic',
  404. 'routing_key': 'video.compress'}
  405. If you return the ``queue`` key, it'll expand with the defined settings of
  406. that queue in :setting:`task_queues`:
  407. .. code-block:: javascript
  408. {'queue': 'video', 'routing_key': 'video.compress'}
  409. becomes -->
  410. .. code-block:: javascript
  411. {'queue': 'video',
  412. 'exchange': 'video',
  413. 'exchange_type': 'topic',
  414. 'routing_key': 'video.compress'}
  415. You install router classes by adding them to the :setting:`task_routes`
  416. setting:
  417. .. code-block:: python
  418. task_routes = (route_task,)
  419. Router functions can also be added by name:
  420. .. code-block:: python
  421. task_routes = ('myapp.routers.route_task',)
  422. For simple task name -> route mappings like the router example above,
  423. you can simply drop a dict into :setting:`task_routes` to get the
  424. same behavior:
  425. .. code-block:: python
  426. task_routes = {
  427. 'myapp.tasks.compress_video': {
  428. 'queue': 'video',
  429. 'routing_key': 'video.compress',
  430. },
  431. }
  432. The routers will then be traversed in order, it will stop at the first router
  433. returning a true value, and use that as the final route for the task.
  434. You can also have multiple routers defined in a sequence:
  435. .. code-block:: python
  436. task_routes = [
  437. route_task,
  438. {
  439. 'myapp.tasks.compress_video': {
  440. 'queue': 'video',
  441. 'routing_key': 'video.compress',
  442. },
  443. ]
  444. The routers will then be visited in turn, and the first to return
  445. a value will be chosen.
  446. Broadcast
  447. ---------
  448. Celery can also support broadcast routing.
  449. Here is an example exchange ``broadcast_tasks`` that delivers
  450. copies of tasks to all workers connected to it:
  451. .. code-block:: python
  452. from kombu.common import Broadcast
  453. app.conf.task_queues = (Broadcast('broadcast_tasks'),)
  454. app.conf.task_routes = {'tasks.reload_cache': {'queue': 'broadcast_tasks'}}
  455. Now the ``tasks.reload_cache`` task will be sent to every
  456. worker consuming from this queue.
  457. Here is another example of broadcast routing, this time with
  458. a :program:`celery beat` schedule:
  459. .. code-block:: python
  460. from kombu.common import Broadcast
  461. from celery.schedules import crontab
  462. app.conf.task_queues = (Broadcast('broadcast_tasks'),)
  463. app.conf.beat_schedule = {
  464. 'test-task': {
  465. 'task': 'tasks.reload_cache',
  466. 'schedule': crontab(minute=0, hour='*/3'),
  467. 'options': {'exchange': 'broadcast_tasks'}
  468. },
  469. }
  470. .. admonition:: Broadcast & Results
  471. Note that Celery result doesn't define what happens if two
  472. tasks have the same task_id. If the same task is distributed to more
  473. than one worker, then the state history may not be preserved.
  474. It's a good idea to set the ``task.ignore_result`` attribute in
  475. this case.