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)