1

I'm trying to write a base task class that will check the availability of workers before it does something. By reading this post here, I've come up with the following:

class BaseTask(Task):
    def apply_async(self, *args, **kwargs):
        if not celery.control.inspect().stats():
            raise Exception("workersDown")
        Task.apply_async(self, *args, **kwargs)

However, this seems to work only for the first time. I know that a task is not instantiated each time, but it is related? Is there any other way to achieve what I want?

EDIT:

I have found that setting the base task as abstract helps but still produces some false positives (sometimes the exception is raised although the workers are up):

class AnotherTask(Task):
    abstract = True

    def apply_async(self, *args, **kwargs):
        if not celery.control.inspect().stats():
            raise Exception("workersDown")
        Task.apply_async(self, *args, **kwargs)
Community
  • 1
  • 1
Ofir
  • 1,565
  • 3
  • 23
  • 41
  • possible duplicate of [Celery scheduled list returns None](http://stackoverflow.com/questions/24236131/celery-scheduled-list-returns-none) – daniula Jul 31 '14 at 22:13

1 Answers1

0

You can call inspect() only once for given celery app instance. Below you have small code snippet, which you can use in your task:

from celery import Celery

def inspect(method):
    app = Celery('app', broker='amqp://')
    return getattr(app.control.inspect(), method)()


class BaseTask(Task):
    def apply_async(self, *args, **kwargs):
        if not inspect('stats'):
            raise Exception("workersDown")
        Task.apply_async(self, *args, **kwargs)
daniula
  • 6,898
  • 4
  • 32
  • 49
  • Why can I inspect only once for a given celery app? Also, can you point me to the relevant documentation for that? – Ofir Aug 01 '14 at 05:49
  • @Ofir, I have no idea why is that. I suppose it's a bug and my solution is just a workaround. I haven't seen any documentation about, I came up with this solution after digging into djcelery command code. – daniula Aug 01 '14 at 05:54