Mercurial > kallithea
comparison rhodecode/lib/celerylib/__init__.py @ 547:1e757ac98988
renamed project to rhodecode
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 06 Oct 2010 03:18:16 +0200 |
parents | pylons_app/lib/celerylib/__init__.py@9bedaa073c23 |
children | 14559eb34003 |
comparison
equal
deleted
inserted
replaced
546:7c2f5e4d7bbf | 547:1e757ac98988 |
---|---|
1 from rhodecode.lib.pidlock import DaemonLock, LockHeld | |
2 from vcs.utils.lazy import LazyProperty | |
3 from decorator import decorator | |
4 import logging | |
5 import os | |
6 import sys | |
7 import traceback | |
8 from hashlib import md5 | |
9 log = logging.getLogger(__name__) | |
10 | |
11 class ResultWrapper(object): | |
12 def __init__(self, task): | |
13 self.task = task | |
14 | |
15 @LazyProperty | |
16 def result(self): | |
17 return self.task | |
18 | |
19 def run_task(task, *args, **kwargs): | |
20 try: | |
21 t = task.delay(*args, **kwargs) | |
22 log.info('running task %s', t.task_id) | |
23 return t | |
24 except Exception, e: | |
25 print e | |
26 if e.errno == 111: | |
27 log.debug('Unnable to connect. Sync execution') | |
28 else: | |
29 log.error(traceback.format_exc()) | |
30 #pure sync version | |
31 return ResultWrapper(task(*args, **kwargs)) | |
32 | |
33 | |
34 def locked_task(func): | |
35 def __wrapper(func, *fargs, **fkwargs): | |
36 params = list(fargs) | |
37 params.extend(['%s-%s' % ar for ar in fkwargs.items()]) | |
38 | |
39 lockkey = 'task_%s' % \ | |
40 md5(str(func.__name__) + '-' + \ | |
41 '-'.join(map(str, params))).hexdigest() | |
42 log.info('running task with lockkey %s', lockkey) | |
43 try: | |
44 l = DaemonLock(lockkey) | |
45 ret = func(*fargs, **fkwargs) | |
46 l.release() | |
47 return ret | |
48 except LockHeld: | |
49 log.info('LockHeld') | |
50 return 'Task with key %s already running' % lockkey | |
51 | |
52 return decorator(__wrapper, func) | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | |
60 |