##// END OF EJS Templates
fix SyntaxWarning: name 'CELERY_ON' is used prior to global declaration
Mads Kiilerich -
r3460:27525c5f beta
parent child Browse files
Show More
@@ -1,130 +1,130 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 rhodecode.lib.celerylib.__init__
4 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 5
6 6 celery libs for RhodeCode
7 7
8 8 :created_on: Nov 27, 2010
9 9 :author: marcink
10 10 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
11 11 :license: GPLv3, see COPYING for more details.
12 12 """
13 13 # This program is free software: you can redistribute it and/or modify
14 14 # it under the terms of the GNU General Public License as published by
15 15 # the Free Software Foundation, either version 3 of the License, or
16 16 # (at your option) any later version.
17 17 #
18 18 # This program is distributed in the hope that it will be useful,
19 19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 21 # GNU General Public License for more details.
22 22 #
23 23 # You should have received a copy of the GNU General Public License
24 24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 25
26 26 import os
27 27 import sys
28 28 import socket
29 29 import traceback
30 30 import logging
31 31 from os.path import dirname as dn, join as jn
32 32 from pylons import config
33 33
34 34 from hashlib import md5
35 35 from decorator import decorator
36 36
37 37 from rhodecode.lib.vcs.utils.lazy import LazyProperty
38 38 from rhodecode import CELERY_ON, CELERY_EAGER
39 39 from rhodecode.lib.utils2 import str2bool, safe_str
40 40 from rhodecode.lib.pidlock import DaemonLock, LockHeld
41 41 from rhodecode.model import init_model
42 42 from rhodecode.model import meta
43 43 from rhodecode.model.db import Statistics, Repository, User
44 44
45 45 from sqlalchemy import engine_from_config
46 46
47 47 from celery.messaging import establish_connection
48 48
49 49 log = logging.getLogger(__name__)
50 50
51 51
52 52 class ResultWrapper(object):
53 53 def __init__(self, task):
54 54 self.task = task
55 55
56 56 @LazyProperty
57 57 def result(self):
58 58 return self.task
59 59
60 60
61 61 def run_task(task, *args, **kwargs):
62 global CELERY_ON
62 63 if CELERY_ON:
63 64 try:
64 65 t = task.apply_async(args=args, kwargs=kwargs)
65 66 log.info('running task %s:%s' % (t.task_id, task))
66 67 return t
67 68
68 69 except socket.error, e:
69 70 if isinstance(e, IOError) and e.errno == 111:
70 71 log.debug('Unable to connect to celeryd. Sync execution')
71 global CELERY_ON
72 72 CELERY_ON = False
73 73 else:
74 74 log.error(traceback.format_exc())
75 75 except KeyError, e:
76 76 log.debug('Unable to connect to celeryd. Sync execution')
77 77 except Exception, e:
78 78 log.error(traceback.format_exc())
79 79
80 80 log.debug('executing task %s in sync mode' % task)
81 81 return ResultWrapper(task(*args, **kwargs))
82 82
83 83
84 84 def __get_lockkey(func, *fargs, **fkwargs):
85 85 params = list(fargs)
86 86 params.extend(['%s-%s' % ar for ar in fkwargs.items()])
87 87
88 88 func_name = str(func.__name__) if hasattr(func, '__name__') else str(func)
89 89
90 90 lockkey = 'task_%s.lock' % \
91 91 md5(func_name + '-' + '-'.join(map(safe_str, params))).hexdigest()
92 92 return lockkey
93 93
94 94
95 95 def locked_task(func):
96 96 def __wrapper(func, *fargs, **fkwargs):
97 97 lockkey = __get_lockkey(func, *fargs, **fkwargs)
98 98 lockkey_path = config['app_conf']['cache_dir']
99 99
100 100 log.info('running task with lockkey %s' % lockkey)
101 101 try:
102 102 l = DaemonLock(file_=jn(lockkey_path, lockkey))
103 103 ret = func(*fargs, **fkwargs)
104 104 l.release()
105 105 return ret
106 106 except LockHeld:
107 107 log.info('LockHeld')
108 108 return 'Task with key %s already running' % lockkey
109 109
110 110 return decorator(__wrapper, func)
111 111
112 112
113 113 def get_session():
114 114 if CELERY_ON:
115 115 engine = engine_from_config(config, 'sqlalchemy.db1.')
116 116 init_model(engine)
117 117 sa = meta.Session()
118 118 return sa
119 119
120 120
121 121 def dbsession(func):
122 122 def __wrapper(func, *fargs, **fkwargs):
123 123 try:
124 124 ret = func(*fargs, **fkwargs)
125 125 return ret
126 126 finally:
127 127 if CELERY_ON and CELERY_EAGER is False:
128 128 meta.Session.remove()
129 129
130 130 return decorator(__wrapper, func)
General Comments 0
You need to be logged in to leave comments. Login now