Last active
December 30, 2015 17:49
-
-
Save scragg0x/7863665 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def single_instance_task(function=None, timeout=None): | |
"""Enforce only one celery task at a time.""" | |
def _dec(run_func): | |
"""Decorator.""" | |
@wraps(run_func) | |
def _caller(*args, **kwargs): | |
"""Caller.""" | |
ret_value = None | |
have_lock = False | |
s = json.dumps({'a': args, 'kw': kwargs}) | |
key = "lock.%s.%s.%s" % (run_func.__module__, run_func.__name__, md5(s).hexdigest()) | |
lock = cache.lock(key, timeout=timeout) | |
try: | |
have_lock = lock.acquire(blocking=False) | |
if have_lock: | |
ret_value = run_func(*args, **kwargs) | |
finally: | |
if have_lock: | |
lock.release() | |
return ret_value | |
return _caller | |
return _dec(function) if function is not None else _dec | |
@periodic_task(run_every=10, time_limit=86400) | |
@single_instance_task(timeout=86400) | |
def run_stuff(): | |
# Might take awhile! | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment