Browse Source

Fixed error e-mails and documented e-mail related settings. Closes #128.

Ask Solem 14 năm trước cách đây
mục cha
commit
fd8db1e2f6
4 tập tin đã thay đổi với 147 bổ sung34 xóa
  1. 16 2
      celery/conf.py
  2. 11 12
      celery/utils/mail.py
  3. 58 4
      docs/configuration.rst
  4. 62 16
      docs/reference/celery.conf.rst

+ 16 - 2
celery/conf.py

@@ -77,6 +77,12 @@ _DEFAULTS = {
     "CELERY_RESULT_PERSISTENT": False,
     "CELERY_MAX_CACHED_RESULTS": 5000,
     "CELERY_TRACK_STARTED": False,
+
+    # Default e-mail settings.
+    "SERVER_EMAIL": "celery@localhost",
+    "EMAIL_HOST": "localhost",
+    "EMAIL_PORT": 25,
+    "ADMINS": (),
 }
 
 
@@ -138,8 +144,7 @@ CELERYD_TASK_TIME_LIMIT = _get("CELERYD_TASK_TIME_LIMIT")
 CELERYD_TASK_SOFT_TIME_LIMIT = _get("CELERYD_TASK_SOFT_TIME_LIMIT")
 CELERYD_MAX_TASKS_PER_CHILD = _get("CELERYD_MAX_TASKS_PER_CHILD")
 STORE_ERRORS_EVEN_IF_IGNORED = _get("CELERY_STORE_ERRORS_EVEN_IF_IGNORED")
-CELERY_SEND_TASK_ERROR_EMAILS = _get("CELERY_SEND_TASK_ERROR_EMAILS",
-                                     not settings.DEBUG,
+CELERY_SEND_TASK_ERROR_EMAILS = _get("CELERY_SEND_TASK_ERROR_EMAILS", False,
                                      compat=["SEND_CELERY_TASK_ERROR_EMAILS"])
 CELERYD_LOG_FORMAT = _get("CELERYD_LOG_FORMAT",
                           compat=["CELERYD_DAEMON_LOG_FORMAT"])
@@ -159,6 +164,15 @@ CELERYD_LISTENER = _get("CELERYD_LISTENER")
 CELERYD_MEDIATOR = _get("CELERYD_MEDIATOR")
 CELERYD_ETA_SCHEDULER = _get("CELERYD_ETA_SCHEDULER")
 
+# :--- Email settings                               <-   --   --- - ----- -- #
+ADMINS = _get("ADMINS")
+SERVER_EMAIL = _get("SERVER_EMAIL")
+EMAIL_HOST = _get("EMAIL_HOST")
+EMAIL_HOST_USER = _get("EMAIL_HOST_USER")
+EMAIL_HOST_PASSWORD = _get("EMAIL_HOST_PASSWORD")
+EMAIL_PORT = _get("EMAIL_PORT")
+
+
 # :--- Broker connections                           <-   --   --- - ----- -- #
 BROKER_HOST = _get("BROKER_HOST")
 BROKER_PORT = _get("BROKER_PORT")

+ 11 - 12
celery/utils/mail.py

@@ -1,22 +1,21 @@
 from mailer import Message, Mailer
 
-from celery.loaders import load_settings
-
-
 def mail_admins(subject, message, fail_silently=False):
-    """Send a message to the admins in settings.ADMINS."""
-    settings = load_settings()
-    if not settings.ADMINS:
+    """Send a message to the admins in conf.ADMINS."""
+    from celery import conf
+
+    if not conf.ADMINS:
         return
-    to = ", ".join(admin_email for _, admin_email in settings.ADMINS)
-    username = settings.EMAIL_HOST_USER
-    password = settings.EMAIL_HOST_PASSWORD
 
-    message = Message(From=settings.SERVER_EMAIL, To=to,
-                      Subject=subject, Message=message)
+    to = ", ".join(admin_email for _, admin_email in conf.ADMINS)
+    username = conf.EMAIL_HOST_USER
+    password = conf.EMAIL_HOST_PASSWORD
+
+    message = Message(From=conf.SERVER_EMAIL, To=to,
+                      Subject=subject, Body=message)
 
     try:
-        mailer = Mailer(settings.EMAIL_HOST, settings.EMAIL_PORT)
+        mailer = Mailer(conf.EMAIL_HOST, conf.EMAIL_PORT)
         username and mailer.login(username, password)
         mailer.send(message)
     except Exception:

+ 58 - 4
docs/configuration.rst

@@ -486,15 +486,69 @@ Worker: celeryd
             except SoftTimeLimitExceeded:
                 cleanup_in_a_hurry()
 
+* CELERY_STORE_ERRORS_EVEN_IF_IGNORED
+
+    If set, the worker stores all task errors in the result store even if
+    ``Task.ignore_result`` is on.
+
+Error E-Mails
+-------------
+
 * CELERY_SEND_TASK_ERROR_EMAILS
 
     If set to ``True``, errors in tasks will be sent to admins by e-mail.
-    If unset, it will send the e-mails if ``settings.DEBUG`` is False.
 
-* CELERY_STORE_ERRORS_EVEN_IF_IGNORED
+* ADMINS
 
-    If set, the worker stores all task errors in the result store even if
-    ``Task.ignore_result`` is on.
+    List of ``(name, email_address)`` tuples for the admins that should
+    receive error e-mails.
+
+* SERVER_EMAIL
+
+    The e-mail address this worker sends e-mails from.
+    Default is ``"celery@localhost"``.
+
+* MAIL_HOST
+
+    The mail server to use. Default is ``"localhost"``.
+
+* MAIL_HOST_USER
+
+    Username (if required) to log on to the mail server with.
+
+* MAIL_HOST_PASSWORD
+
+    Password (if required) to log on to the mail server with.
+
+* MAIL_PORT
+
+    The port the mail server is listening on. Default is ``25``.
+
+Example E-Mail configuration
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+This configuration enables the sending of error e-mails to
+``george@vandelay.com`` and ``kramer@vandelay.com``:
+
+.. code-block:: python
+
+    # Enables error e-mails.
+    CELERY_SEND_TASK_ERROR_EMAILS = True
+
+    # Name and e-mail addresses of recipients
+    ADMINS = (
+        ("George Costanza", "george@vandelay.com"),
+        ("Cosmo Kramer", "kosmo@vandelay.com"),
+    )
+
+    # E-mail address used as sender (From field).
+    SERVER_EMAIL = "no-reply@vandelay.com"
+
+    # Mailserver configuration
+    EMAIL_HOST = "mail.vandelay.com"
+    EMAIL_PORT = 25
+    # EMAIL_HOST_USER = "servers"
+    # EMAIL_HOST_PASSWORD = "s3cr3t"
 
 Events
 ------

+ 62 - 16
docs/reference/celery.conf.rst

@@ -6,6 +6,9 @@ Configuration - celery.conf
     :local:
 .. currentmodule:: celery.conf
 
+Queues
+======
+
 .. data:: QUEUES
 
     Queue name/options mapping.
@@ -81,10 +84,41 @@ Configuration - celery.conf
     Exchange used by the AMQP result backend to publish task results.
     Default is ``"celeryresult"``.
 
+Sending E-Mails
+===============
+
 .. data:: CELERY_SEND_TASK_ERROR_EMAILS
 
-    If set to ``True``, errors in tasks will be sent to admins by e-mail.
-    If unset, it will send the e-mails if ``settings.DEBUG`` is ``True``.
+    If set to ``True``, errors in tasks will be sent to :data:`ADMINS` by e-mail.
+
+.. data:: ADMINS
+
+    List of ``(name, email_address)`` tuples for the admins that should
+    receive error e-mails.
+
+.. data:: SERVER_EMAIL
+
+    The e-mail address this worker sends e-mails from.
+    Default is ``"celery@localhost"``.
+
+.. data:: MAIL_HOST
+
+    The mail server to use. Default is ``"localhost"``.
+
+.. data:: MAIL_HOST_USER
+
+    Username (if required) to log on to the mail server with.
+
+.. data:: MAIL_HOST_PASSWORD
+
+    Password (if required) to log on to the mail server with.
+
+.. data:: MAIL_PORT
+
+    The port the mail server is listening on. Default is ``25``.
+
+Execution
+=========
 
 .. data:: ALWAYS_EAGER
 
@@ -123,20 +157,6 @@ Configuration - celery.conf
     Total number of results to store before results are evicted from the
     result cache.
 
-.. data:: BROKER_CONNECTION_RETRY
-
-    Automatically try to re-establish the connection to the AMQP broker if
-    it's lost.
-
-.. data:: BROKER_CONNECTION_MAX_RETRIES
-
-    Maximum number of retries before we give up re-establishing a connection
-    to the broker.
-
-    If this is set to ``0`` or ``None``, we will retry forever.
-
-    Default is ``100`` retries.
-
 .. data:: TASK_SERIALIZER
 
     A string identifying the default serialization
@@ -170,6 +190,26 @@ Configuration - celery.conf
     If ``True`` all rate limits will be disabled and all tasks will be executed
     as soon as possible.
 
+Broker
+======
+
+.. data:: BROKER_CONNECTION_RETRY
+
+    Automatically try to re-establish the connection to the AMQP broker if
+    it's lost.
+
+.. data:: BROKER_CONNECTION_MAX_RETRIES
+
+    Maximum number of retries before we give up re-establishing a connection
+    to the broker.
+
+    If this is set to ``0`` or ``None``, we will retry forever.
+
+    Default is ``100`` retries.
+
+Celerybeat
+==========
+
 .. data:: CELERYBEAT_LOG_LEVEL
 
     Default log level for celerybeat.
@@ -195,6 +235,9 @@ Configuration - celery.conf
     faster (A value of 5 minutes, means the changes will take effect in 5 minutes
     at maximum).
 
+Celerymon
+=========
+
 .. data:: CELERYMON_LOG_LEVEL
 
     Default log level for celerymon.
@@ -205,6 +248,9 @@ Configuration - celery.conf
     Default log file for celerymon.
     Default is: ``None`` (stderr)
 
+Celeryd
+=======
+
 .. data:: LOG_LEVELS
 
     Mapping of log level names to :mod:`logging` module constants.