celery icon indicating copy to clipboard operation
celery copied to clipboard

need a CELERY_TASK_ERROR_BLACKLIST setting

Open chase-seibert opened this issue 15 years ago • 5 comments

I'm not sure what the use case for CELERY_TASK_ERROR_WHITELIST is, but in my case I really need a blacklist functionality. For example, if I'm getting a bunch of errors that I don't want to get, I would prefer to not trap them, but also have them not generate email alerts.

However, it's critical that new, non-seen-before errors continue to send emails.

chase-seibert avatar Aug 09 '11 16:08 chase-seibert

I would be happy to implement the feature, if we have agreement that it's warranted, and some pointers on an implementation most likely to be accepted.

chase-seibert avatar Aug 09 '11 16:08 chase-seibert

Yeah, I have to agree that CELERY_TASK_ERROR_WHITELIST wasn't the most useful feature, and I probably shouldn't have accepted it in the first place.

I think maybe we could choose a more flexible approach than whitelist/blacklist (polluting the settings, which there are already way too many of)

Maybe rather the e-mail sending code should be moved to the task class, something like this:

class ErrorMail(object): subject = "[celery@%(hostname)s] Error: Task %(name)s (%(id)s): %(exc)s" body = ....

class Task(object):
     ErrorMail = ErrorMail

     def send_error_email(self, context, exc):
         if self.send_error_emails:
             subject = self.ErrorMail.subject % context
             body = self.ErrorMail.body % context
             self.app.mail_admins(subject, body, fail_silently=True)

Then the task could blacklist an exception by:

class MyTaskBase(Task):
    abstract = True

    def send_error_email(self, context, exc):
        if isinstance(exc, KeyError):
            return
        super(MyTaskBase, self).send_error_email(context, exc)

@task(base=MyBaseTask)
def foo(...)

Of course this doesn't enable us to change this by configuration, but I have been considering a more flexible approach for this lately too. Like the task routers enable a flexible way to route messages without changing code, I think we should have a similar approach to task attributes, so you can change the default attributes for tasks by configuration.

def send_error_emails(self, context, exc):
    if not isinstance(exc, KeyError):
          self.super.send_error_emails(context, exc)

TASK_DEFAULTS = {"myapp.add": {"ignore_result": True,   "send_error_emails": send_error_emails}}

If you would like move the send_error_email to the Task class that would be awesome!

ask avatar Sep 02 '11 15:09 ask

I will take a stab at this over the weekend.

chase-seibert avatar Sep 02 '11 15:09 chase-seibert

I wrote two implementations.

The first is a conservative change that just moves send_error_email into the task: https://github.com/chase-seibert/celery/commit/2181484c03b5b9335a9d0b3e3ba3b7262511a260

The second one moves the logic into a helper class, ErrorMailSender, which can be over-ridden on the task. https://github.com/chase-seibert/celery/commit/fa1809b7bb3a5b898ea7f07b276339552e054e20

I was able to preserve backwards compatibility for setting subject/body on the TaskRequest, and also for setting error_whitelist on the task, but the code would be cleaner if those were removed in favor of configuration of the ErrorMailSender directly.

chase-seibert avatar Sep 03 '11 20:09 chase-seibert

Nice this is a great change! I chose the second one, it is really nice to move that email mess out of celery/worker/job.py.

I cleaned up a little, ErrorMailSender -> ErrorMail, and also I removed the mail_body and mail_subject from TaskRequest, as I don't think they are used (it is not yet simple to override how TaskRequests are created).

See my changes here: a68d40df71626c04e24adda513bda56bcdf821bb + b53c1c3e25713a27bb9267ad62d921b1c4d8ec1a

ask avatar Sep 05 '11 16:09 ask