Created
September 7, 2017 16:00
-
-
Save anonymous/ab184e1f6f06510bb487cba8bd7ebc78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.task(bind=True) | |
def celery_task_that_does_a_thing(self, first_argument, optional_argument='something'): | |
lock_key = "celery_task_that_does_a_thing_{}".format(first_argument) | |
if acquire_task_lock(lock_key, self.request.id): | |
# lock acquired | |
try: | |
return _do_the_thing(first_argument,optional_argument) | |
except Exception as e: | |
return { | |
'error': e | |
} | |
finally: | |
release_task_lock(lock_key) | |
else: | |
# task already locked | |
return { | |
'locked': True, | |
'resume': resume_task_code(lock_key) | |
} | |
def _do_the_thing(first_argument, optional_argument='something'): | |
return { | |
'result': "a thing" | |
} |
someone suggested an abstract base class, it's still fairly boilerplatey but maybe the cleanest?
@app.task(bind=True)
def celery_task_that_does_a_thing(self, first_argument, optional_argument='something'):
task = CeleryTaskThatDoesAThing(first_argument, optional_argument=optional_argument)
return task()
class CeleryTaskThatDoesAThing(LockedTask):
def get_task_key(self, first_argument, *args, **kwargs)
return "{}_{}".format(self.__name__, first_argument)
def run(self):
return {
'result': "a thing"
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was thinking key_function could be replaced by something like key_name="celery_task_that_does_a_thing", as long as the key_function basically remains the same and just formats the function name together with the first argument.