فهرست منبع

Adds celeryd_init signal that can be used to configure workers by hostname. Thanks to zllak

Ask Solem 13 سال پیش
والد
کامیت
78cf9bce78
2فایلهای تغییر یافته به همراه43 افزوده شده و 0 حذف شده
  1. 2 0
      celery/signals.py
  2. 41 0
      docs/userguide/signals.rst

+ 2 - 0
celery/signals.py

@@ -24,6 +24,8 @@ task_failure = Signal(providing_args=["task_id", "exception",
                                       "args", "kwargs", "traceback",
                                       "einfo"])
 
+celeryd_init = Signal(providing_args=["instance"])
+
 worker_init = Signal(providing_args=[])
 worker_process_init = Signal(providing_args=[])
 worker_ready = Signal(providing_args=[])

+ 41 - 0
docs/userguide/signals.rst

@@ -165,6 +165,47 @@ Provides arguments:
 Worker Signals
 --------------
 
+.. signal:: celeryd_init
+
+celeryd_init
+~~~~~~~~~~~~
+
+This is the first signal sent when :program:`celeryd` starts up.
+The ``sender`` is the host name of the worker, so this signal can be used
+to setup worker specific configuration:
+
+.. code-block:: python
+
+    from celery.signals import celeryd_init
+
+    @celeryd_init.connect(sender="worker12.example.com")
+    def configure_worker12(conf=None, **kwargs):
+        conf.CELERY_DEFAULT_RATE_LIMIT = "10/m"
+
+or to set up configuration for multiple workers you can omit specifying a
+sender when you connect:
+
+.. code-block:: python
+
+    from celery.signals import celeryd_init
+
+    @celeryd_init.connect
+    def configure_workers(sender=None, conf=None, **kwargs):
+        if sender in ("worker1.example.com", "worker2.example.com"):
+            conf.CELERY_DEFAULT_RATE_LIMIT = "10/m"
+        if sender == "worker3.example.com":
+            conf.CELERYD_PREFETCH_MULTIPLIER = 0
+
+Provides arguments:
+
+* 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:: worker_init
 
 worker_init