Mercurial > public > src > rhodecode
comparison pylons_app/lib/celerylib/__init__.py @ 547:ac32a026c306
simplified task locking, and fixed some bugs for keyworded arguments
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Thu, 23 Sep 2010 21:25:30 +0200 |
parents | fb0c3af6031b |
children | d5efb83590ef |
comparison
equal
deleted
inserted
replaced
546:207f5f4d3f93 | 547:ac32a026c306 |
---|---|
29 log.error(traceback.format_exc()) | 29 log.error(traceback.format_exc()) |
30 #pure sync version | 30 #pure sync version |
31 return ResultWrapper(task(*args, **kwargs)) | 31 return ResultWrapper(task(*args, **kwargs)) |
32 | 32 |
33 | 33 |
34 class LockTask(object): | 34 def locked_task(func): |
35 """LockTask decorator""" | 35 def __wrapper(func, *fargs, **fkwargs): |
36 | 36 params = list(fargs) |
37 def __init__(self, func): | 37 params.extend(['%s-%s' % ar for ar in fkwargs.items()]) |
38 self.func = func | 38 |
39 | |
40 def __call__(self, func): | |
41 return decorator(self.__wrapper, func) | |
42 | |
43 def __wrapper(self, func, *fargs, **fkwargs): | |
44 params = [] | |
45 params.extend(fargs) | |
46 params.extend(fkwargs.values()) | |
47 lockkey = 'task_%s' % \ | 39 lockkey = 'task_%s' % \ |
48 md5(str(self.func) + '-' + '-'.join(map(str, params))).hexdigest() | 40 md5(str(func.__name__) + '-' + \ |
41 '-'.join(map(str, params))).hexdigest() | |
49 log.info('running task with lockkey %s', lockkey) | 42 log.info('running task with lockkey %s', lockkey) |
50 try: | 43 try: |
51 l = DaemonLock(lockkey) | 44 l = DaemonLock(lockkey) |
52 return func(*fargs, **fkwargs) | 45 return func(*fargs, **fkwargs) |
53 l.release() | 46 l.release() |
54 except LockHeld: | 47 except LockHeld: |
55 log.info('LockHeld') | 48 log.info('LockHeld') |
56 return 'Task with key %s already running' % lockkey | 49 return 'Task with key %s already running' % lockkey |
57 | 50 |
58 | 51 return decorator(__wrapper, func) |
59 | 52 |
60 | 53 |
61 | 54 |
62 | 55 |
63 | 56 |