浏览代码

Make sure all queues are declared before publishing.

Ask Solem 15 年之前
父节点
当前提交
3faf618af2
共有 2 个文件被更改,包括 13 次插入1 次删除
  1. 12 0
      celery/messaging.py
  2. 1 1
      docs/userguide/tasks.rst

+ 12 - 0
celery/messaging.py

@@ -22,6 +22,8 @@ get_msg_options = mitemgetter(*MSG_OPTIONS)
 extract_msg_options = lambda d: dict(zip(MSG_OPTIONS, get_msg_options(d)))
 default_queue = conf.routing_table[conf.DEFAULT_QUEUE]
 
+_queues_declared = False
+
 
 class TaskPublisher(Publisher):
     """Publish tasks."""
@@ -30,6 +32,16 @@ class TaskPublisher(Publisher):
     routing_key = conf.DEFAULT_ROUTING_KEY
     serializer = conf.TASK_SERIALIZER
 
+    def __init__(self, *args, **kwargs):
+        super(TaskPublisher, self).__init__(*args, **kwargs)
+
+        # Make sure all queues are declared.
+        global _queues_declared
+        if not _queues_declared:
+            consumers = get_consumer_set(self.connection)
+            consumers.close()
+            _queues_declared = True
+
     def delay_task(self, task_name, task_args=None, task_kwargs=None,
             task_id=None, taskset_id=None, **kwargs):
         """Delay task for execution by the celery nodes."""

+ 1 - 1
docs/userguide/tasks.rst

@@ -247,7 +247,7 @@ Let's take a real wold example; A blog where comments posted needs to be
 filtered for spam. When the comment is created, we run the spam filter in the
 background, so the user doesn't have to wait for it to finish.
 
-We hvae a Django blog application allowing comments
+We have a Django blog application allowing comments
 on blog posts. We'll describe parts of the models/views and tasks for this
 application.