I have celery workers and I want them to have a time limit of 60 seconds and cleanup if timeout is reached or task finishes. I know I should use soft time limits but instead of rewriting try...catch clauses for each task, I want to use an abstract task class that would do that - something in the lines of:
class BaseTask(Task):
abstract = True
def apply(self, args=None, kwargs=None, link=None, link_error=None, **options):
try:
super(self.__class__, self).apply(args, kwargs, link, link_error, **options)
except SoftTimeLimitExceeded:
print "cleanup time!"
This code doesn't work, but is there any way to achieve this?
I have found something that might be similar to what I need:
class BaseTask(Task):
def __call__(self, *args, **kwargs):
try:
print "i'm here"
return self.run(*args, **kwargs)
except SoftTimeLimitExceeded:
print "cleanup time!"
but the exception isn't raised there ("i'm here" is printed)