Browse Source

Adds signal: celeryd_after_setup

Ask Solem 12 years ago
parent
commit
eb6e874a55
3 changed files with 46 additions and 1 deletions
  1. 4 0
      celery/apps/worker.py
  2. 2 1
      celery/signals.py
  3. 40 0
      docs/userguide/signals.rst

+ 4 - 0
celery/apps/worker.py

@@ -163,6 +163,10 @@ class Worker(configurated):
         self.set_process_status('-active-')
 
         self.redirect_stdouts_to_logger()
+        # this signal can be used to e.g. change queues after
+        # the -Q option has been applied.
+        signals.celeryd_after_setup.send(sender=self.hostname, instance=self,
+                                         conf=self.app.conf)
         try:
             self.run_worker()
         except IGNORE_ERRORS:

+ 2 - 1
celery/signals.py

@@ -24,7 +24,8 @@ task_success = Signal(providing_args=['result'])
 task_failure = Signal(providing_args=[
     'task_id', 'exception', 'args', 'kwargs', 'traceback', 'einfo'])
 task_revoked = Signal(providing_args=['terminated', 'signum', 'expired'])
-celeryd_init = Signal(providing_args=['instance'])
+celeryd_init = Signal(providing_args=['instance', 'conf'])
+celeryd_after_setup = Signal(providing_args=['instance', 'conf'])
 worker_init = Signal(providing_args=[])
 worker_process_init = Signal(providing_args=[])
 worker_ready = Signal(providing_args=[])

+ 40 - 0
docs/userguide/signals.rst

@@ -205,6 +205,43 @@ Provides arguments:
 Worker Signals
 --------------
 
+.. signal:: celeryd_after_setup
+
+celeryd_after_setup
+~~~~~~~~~~~~~~~~~~~
+
+This signal is sent after the worker instance is set up,
+but before it calls run.  This means that any queues from the :option:`-Q`
+option is enabled, logging has been set up and so on.
+
+It can be used to e.g. add custom queues that should always be consumed
+from, disregarding the :option:`-Q` option.  Here's an example
+that sets up a direct queue for each worker, these queues can then be
+used to route a task to any specific worker:
+
+.. code-block:: python
+
+    from celery.signals import celeryd_after_setup
+
+    @celeryd_after_setup.connect
+    def setup_direct_queue(sender, instance, **kwargs):
+        queue_name = '%s.dq' % sender   # sender is the hostname of the worker
+        instance.app.queues.add(queue_name, routing_key=queue_name)
+
+Provides arguments:
+
+* sender
+  Hostname of the worker.
+
+* instance
+    This is the :class:`celery.apps.worker.Worker` instance to be initialized.
+    Note that only the :attr:`app` and :attr:`hostname` attributes have been
+    set so far, and the rest of ``__init__`` has not been executed.
+
+* conf
+    The configuration of the current app.
+
+
 .. signal:: celeryd_init
 
 celeryd_init
@@ -238,6 +275,9 @@ sender when you connect:
 
 Provides arguments:
 
+* sender
+  Hostname of the worker.
+
 * instance
     This is the :class:`celery.apps.worker.Worker` instance to be initialized.
     Note that only the :attr:`app` and :attr:`hostname` attributes have been