Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6707123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:36:07+00:00 2026-05-26T07:36:07+00:00

I have to implement a Task subclass that gracefully fails if the broker is

  • 0

I have to implement a Task subclass that gracefully fails if the broker is not running – currently I’m using RabbitMQ.
I could probably just use a try statement to catch the exception:

try:
    Mytask.delay(arg1, arg2)
except socket.error:
    # Send an notice to an admin
    pass

but I’d like to create a subclass of Task that can handle that.
I’ve tried something like that:

class MyTask(Task):
    ignore_result = True

    def __call__(self, *args, **kwargs):
        try:
            return self.run(*args, **kwargs)
        except socket.error:
            # Send an notice to an admin
            return None

but the workflow is clearly wrong. I think I need to inject maybe a backend subclass or a failure policy somehow.
Do you have any suggestion?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:36:07+00:00Added an answer on May 26, 2026 at 7:36 am

    A possible solution I came up with:

    import socket
    from celery.decorators import task
    from celery.task import Task
    from celery.backends.base import BaseBackend
    
    UNDELIVERED = 'UNDELIVERED'
    
    
    class DummyBackend(BaseBackend):
        """
        Dummy queue backend for undelivered messages (due to the broker being down).
        """
        def store_result(self, *args, **kwargs):
            pass
    
        def get_status(self, *args, **kwargs):
            return UNDELIVERED
    
        def _dummy(self, *args, **kwargs):
            return None
    
        wait_for = get_result = get_traceback = _dummy
    
    
    class SafeTask(Task):
        """
        A task not raising socket errors if the broker is down.
        """
        abstract = True
        on_broker_error = None
        errbackend = DummyBackend
    
        @classmethod
        def apply_async(cls, *args, **kwargs):
            try:
                return super(SafeTask, cls).apply_async(*args, **kwargs)
            except socket.error, err:
                if cls.on_broker_error is not None:
                    cls.on_broker_error(err, cls, *args, **kwargs)
                return cls.app.AsyncResult(None, backend=cls.errbackend(), 
                    task_name=cls.name)
    
    
    def safetask(*args, **kwargs):
        """
        Task factory returning safe tasks handling socket errors.
        When a socket error occurs, the given callable *on_broker_error*
        is called passing the exception object, the class of the task
        and the original args and kwargs.
        """
        if 'base' not in kwargs:
    
            on_broker_error = kwargs.pop('on_broker_error', SafeTask.on_broker_error)
            errbackend = kwargs.pop('errbackend', SafeTask.errbackend)
            kwargs['base'] = type('SafeTask', (SafeTask,), {
                'on_broker_error': staticmethod(on_broker_error), 
                'errbackend': errbackend,
                'abstract': True,
            })
    
        return task(*args, **kwargs)
    

    You can both subclass SafeTask or use the decorator @safetask.
    If you can think of an improvement, don’t hesitate to contribute.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have just received a task to implement a software that paints over pictures
I have found that I often have to implement some sort of a scheduler
Let's say I have to implement a piece of T-SQL code that must return
I have a WSDL file defining a service that I have to implement in
I have to implement a progressbar that show the progress of a report generation,
I have task to give in my application possibility to open document that is
I have an ugly data entry task that needs automation. The problem is that
I have a small task in Flash, that I'm having a little trouble with.
I have a task to implement service with behavior like this one. My question
I personally don't have my entities implement interfaces. For a Task class I wouldn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.