author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 09 Apr 2014 17:15:25 +0200 | |
changeset 9747 | 10108d9f502a |
parent 9610 | 29450466273a |
child 10095 | 200bd6a601dc |
permissions | -rw-r--r-- |
9167
c05652b108ce
[rql rewrite] move some code from querier to rqlrewrite where it makes more sense.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8912
diff
changeset
|
1 |
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
6128 | 18 |
"""Some utilities for the CubicWeb server.""" |
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7573
diff
changeset
|
19 |
|
0 | 20 |
__docformat__ = "restructuredtext en" |
21 |
||
22 |
import sys |
|
6765
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
23 |
import logging |
0 | 24 |
from threading import Timer, Thread |
25 |
from getpass import getpass |
|
2105
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
26 |
|
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
27 |
from passlib.utils import handlers as uh, to_hash_str |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
28 |
from passlib.context import CryptContext |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
29 |
|
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
30 |
from cubicweb.md5crypt import crypt as md5crypt |
0 | 31 |
|
32 |
||
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
33 |
class CustomMD5Crypt(uh.HasSalt, uh.GenericHandler): |
8398
a9fe30c953be
[server/utils] passlib 1.6 is now less tolerant wrt handler names (closes #2349330)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8317
diff
changeset
|
34 |
name = 'cubicwebmd5crypt' |
a9fe30c953be
[server/utils] passlib 1.6 is now less tolerant wrt handler names (closes #2349330)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8317
diff
changeset
|
35 |
setting_kwds = ('salt',) |
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
36 |
min_salt_size = 0 |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
37 |
max_salt_size = 8 |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
38 |
salt_chars = uh.H64_CHARS |
0 | 39 |
|
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
40 |
@classmethod |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
41 |
def from_string(cls, hash): |
8414
e7243ed7bb0a
Fixes for compatibility with passlib 1.6 (closes #2356393)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8398
diff
changeset
|
42 |
salt, chk = uh.parse_mc2(hash, u'') |
e7243ed7bb0a
Fixes for compatibility with passlib 1.6 (closes #2356393)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8398
diff
changeset
|
43 |
if chk is None: |
e7243ed7bb0a
Fixes for compatibility with passlib 1.6 (closes #2356393)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8398
diff
changeset
|
44 |
raise ValueError('missing checksum') |
e7243ed7bb0a
Fixes for compatibility with passlib 1.6 (closes #2356393)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8398
diff
changeset
|
45 |
return cls(salt=salt, checksum=chk) |
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
46 |
|
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
47 |
def to_string(self): |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
48 |
return to_hash_str(u'%s$%s' % (self.salt, self.checksum or u'')) |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
49 |
|
8414
e7243ed7bb0a
Fixes for compatibility with passlib 1.6 (closes #2356393)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8398
diff
changeset
|
50 |
# passlib 1.5 wants calc_checksum, 1.6 wants _calc_checksum |
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
51 |
def calc_checksum(self, secret): |
8414
e7243ed7bb0a
Fixes for compatibility with passlib 1.6 (closes #2356393)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8398
diff
changeset
|
52 |
return md5crypt(secret, self.salt.encode('ascii')).decode('utf-8') |
e7243ed7bb0a
Fixes for compatibility with passlib 1.6 (closes #2356393)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8398
diff
changeset
|
53 |
_calc_checksum = calc_checksum |
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
54 |
|
8546
3d2038d6f20d
[sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents:
8446
diff
changeset
|
55 |
_CRYPTO_CTX = CryptContext(['sha512_crypt', CustomMD5Crypt, 'des_crypt', 'ldap_salted_sha1'], |
3d2038d6f20d
[sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents:
8446
diff
changeset
|
56 |
deprecated=['cubicwebmd5crypt', 'des_crypt']) |
3d2038d6f20d
[sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents:
8446
diff
changeset
|
57 |
verify_and_update = _CRYPTO_CTX.verify_and_update |
0 | 58 |
|
59 |
def crypt_password(passwd, salt=None): |
|
60 |
"""return the encrypted password using the given salt or a generated one |
|
61 |
""" |
|
62 |
if salt is None: |
|
8398
a9fe30c953be
[server/utils] passlib 1.6 is now less tolerant wrt handler names (closes #2349330)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8317
diff
changeset
|
63 |
return _CRYPTO_CTX.encrypt(passwd) |
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
64 |
# empty hash, accept any password for backwards compat |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
65 |
if salt == '': |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
66 |
return salt |
8550
63260486de89
[server/utils] catch ValueError from password verification
Julien Cristau <julien.cristau@logilab.fr>
parents:
8546
diff
changeset
|
67 |
try: |
63260486de89
[server/utils] catch ValueError from password verification
Julien Cristau <julien.cristau@logilab.fr>
parents:
8546
diff
changeset
|
68 |
if _CRYPTO_CTX.verify(passwd, salt): |
63260486de89
[server/utils] catch ValueError from password verification
Julien Cristau <julien.cristau@logilab.fr>
parents:
8546
diff
changeset
|
69 |
return salt |
63260486de89
[server/utils] catch ValueError from password verification
Julien Cristau <julien.cristau@logilab.fr>
parents:
8546
diff
changeset
|
70 |
except ValueError: # e.g. couldn't identify hash |
63260486de89
[server/utils] catch ValueError from password verification
Julien Cristau <julien.cristau@logilab.fr>
parents:
8546
diff
changeset
|
71 |
pass |
8317
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
72 |
# wrong password |
9c59258e7798
[security] use a stronger encryption algorythm for password, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
73 |
return '' |
0 | 74 |
|
75 |
||
9610
29450466273a
[server] eschema_eid needs a connection, not a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9468
diff
changeset
|
76 |
def eschema_eid(cnx, eschema): |
5066
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
77 |
"""get eid of the CWEType entity for the given yams type. You should use |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
78 |
this because when schema has been loaded from the file-system, not from the |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
79 |
database, (e.g. during tests), eschema.eid is not set. |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
80 |
""" |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
81 |
if eschema.eid is None: |
9610
29450466273a
[server] eschema_eid needs a connection, not a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9468
diff
changeset
|
82 |
eschema.eid = cnx.execute( |
5066
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
83 |
'Any X WHERE X is CWEType, X name %(name)s', |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
84 |
{'name': str(eschema)})[0][0] |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
85 |
return eschema.eid |
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
86 |
|
bf5cbc351e99
[repo] move eschema_eid function from hooks.metadata to server.utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
87 |
|
0 | 88 |
DEFAULT_MSG = 'we need a manager connection on the repository \ |
89 |
(the server doesn\'t have to run, even should better not)' |
|
90 |
||
1910
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
91 |
def manager_userpasswd(user=None, msg=DEFAULT_MSG, confirm=False, |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
92 |
passwdmsg='password'): |
0 | 93 |
if not user: |
3701
104b7c326172
check we've some message to display
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3585
diff
changeset
|
94 |
if msg: |
104b7c326172
check we've some message to display
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3585
diff
changeset
|
95 |
print msg |
0 | 96 |
while not user: |
97 |
user = raw_input('login: ') |
|
98 |
user = unicode(user, sys.stdin.encoding) |
|
1910
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
99 |
passwd = getpass('%s: ' % passwdmsg) |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
100 |
if confirm: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
101 |
while True: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
102 |
passwd2 = getpass('confirm password: ') |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
103 |
if passwd == passwd2: |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
104 |
break |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
105 |
print 'password doesn\'t match' |
864aa3ea0db5
[server] refactor server.utils.manager_userpasswd
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
106 |
passwd = getpass('password: ') |
0 | 107 |
# XXX decode password using stdin encoding then encode it using appl'encoding |
108 |
return user, passwd |
|
109 |
||
110 |
||
9468
39b7a91a3f4c
[repo] pylint cleanup, mainly of imports, with a bit of style
Julien Cristau <julien.cristau@logilab.fr>
parents:
9467
diff
changeset
|
111 |
_MARKER = object() |
6381
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
112 |
def func_name(func): |
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
113 |
name = getattr(func, '__name__', _MARKER) |
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
114 |
if name is _MARKER: |
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
115 |
name = getattr(func, 'func_name', _MARKER) |
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
116 |
if name is _MARKER: |
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
117 |
name = repr(func) |
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
118 |
return name |
2105
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
119 |
|
0 | 120 |
class LoopTask(object): |
121 |
"""threaded task restarting itself once executed""" |
|
8320
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
122 |
def __init__(self, tasks_manager, interval, func, args): |
8911
2dd134a86996
[task] allow interval=0 for looping tasks
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8887
diff
changeset
|
123 |
if interval < 0: |
2dd134a86996
[task] allow interval=0 for looping tasks
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8887
diff
changeset
|
124 |
raise ValueError('Loop task interval must be >= 0 ' |
5602
277b15d6d3ed
forbid looping tasks with an interval of 0
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5601
diff
changeset
|
125 |
'(current value: %f for %s)' % \ |
6381
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
126 |
(interval, func_name(func))) |
8320
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
127 |
self._tasks_manager = tasks_manager |
0 | 128 |
self.interval = interval |
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2105
diff
changeset
|
129 |
def auto_restart_func(self=self, func=func, args=args): |
7573
c8f8762c986d
[repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6765
diff
changeset
|
130 |
restart = True |
0 | 131 |
try: |
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2105
diff
changeset
|
132 |
func(*args) |
7573
c8f8762c986d
[repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6765
diff
changeset
|
133 |
except Exception: |
6765
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
134 |
logger = logging.getLogger('cubicweb.repository') |
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
135 |
logger.exception('Unhandled exception in LoopTask %s', self.name) |
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
136 |
raise |
7573
c8f8762c986d
[repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6765
diff
changeset
|
137 |
except BaseException: |
c8f8762c986d
[repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6765
diff
changeset
|
138 |
restart = False |
0 | 139 |
finally: |
8320
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
140 |
if restart and tasks_manager.running: |
7573
c8f8762c986d
[repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6765
diff
changeset
|
141 |
self.start() |
0 | 142 |
self.func = auto_restart_func |
6381
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
143 |
self.name = func_name(func) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
144 |
|
4714
fccda6dd91bf
merge debug and info views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
145 |
def __str__(self): |
fccda6dd91bf
merge debug and info views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
146 |
return '%s (%s seconds)' % (self.name, self.interval) |
fccda6dd91bf
merge debug and info views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
147 |
|
0 | 148 |
def start(self): |
149 |
self._t = Timer(self.interval, self.func) |
|
5601
92cf309672ca
/siteinfo page: display information about the names of the running threads
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5424
diff
changeset
|
150 |
self._t.setName('%s-%s[%d]' % (self._t.getName(), self.name, self.interval)) |
0 | 151 |
self._t.start() |
152 |
||
153 |
def cancel(self): |
|
154 |
self._t.cancel() |
|
155 |
||
156 |
def join(self): |
|
5581
0aae5216f99e
[repo] ensure thread is alive before calling .join. Closes #963580
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
157 |
if self._t.isAlive(): |
0aae5216f99e
[repo] ensure thread is alive before calling .join. Closes #963580
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
158 |
self._t.join() |
0 | 159 |
|
160 |
||
161 |
class RepoThread(Thread): |
|
162 |
"""subclass of thread so it auto remove itself from a given list once |
|
163 |
executed |
|
164 |
""" |
|
165 |
def __init__(self, target, running_threads): |
|
166 |
def auto_remove_func(self=self, func=target): |
|
167 |
try: |
|
168 |
func() |
|
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7573
diff
changeset
|
169 |
except Exception: |
6765
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
170 |
logger = logging.getLogger('cubicweb.repository') |
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
171 |
logger.exception('Unhandled exception in RepoThread %s', self._name) |
b922e3a817e9
fix ticket #1382716 (problem was actually more subtle than I originally thought)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6427
diff
changeset
|
172 |
raise |
0 | 173 |
finally: |
174 |
self.running_threads.remove(self) |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1134
diff
changeset
|
175 |
Thread.__init__(self, target=auto_remove_func) |
0 | 176 |
self.running_threads = running_threads |
6381
c9eed5037223
[repo threads] Add several safety when looking for a callable name.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6128
diff
changeset
|
177 |
self._name = func_name(target) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
178 |
|
0 | 179 |
def start(self): |
180 |
self.running_threads.append(self) |
|
3585
cd437d24aa65
use daemon thread
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
181 |
self.daemon = True |
0 | 182 |
Thread.start(self) |
183 |
||
5376
2c3f14bc2590
[python2.6] don't add a name property on Thread
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5066
diff
changeset
|
184 |
def getName(self): |
0 | 185 |
return '%s(%s)' % (self._name, Thread.getName(self)) |
8320
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
186 |
|
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
187 |
class TasksManager(object): |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
188 |
"""Object dedicated manage background task""" |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
189 |
|
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
190 |
def __init__(self): |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
191 |
self.running = False |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
192 |
self._tasks = [] |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
193 |
self._looping_tasks = [] |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
194 |
|
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
195 |
def add_looping_task(self, interval, func, *args): |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
196 |
"""register a function to be called every `interval` seconds. |
8912
da2007002dca
[task] allow negative intervals for add_looping_task (closes #2818280)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8911
diff
changeset
|
197 |
|
da2007002dca
[task] allow negative intervals for add_looping_task (closes #2818280)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8911
diff
changeset
|
198 |
If interval is negative, no looping task is registered. |
8320
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
199 |
""" |
8912
da2007002dca
[task] allow negative intervals for add_looping_task (closes #2818280)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8911
diff
changeset
|
200 |
if interval < 0: |
da2007002dca
[task] allow negative intervals for add_looping_task (closes #2818280)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8911
diff
changeset
|
201 |
self.debug('looping task %s ignored due to interval %f < 0', |
da2007002dca
[task] allow negative intervals for add_looping_task (closes #2818280)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8911
diff
changeset
|
202 |
func_name(func), interval) |
da2007002dca
[task] allow negative intervals for add_looping_task (closes #2818280)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8911
diff
changeset
|
203 |
return |
8446
cae198371548
[server/utils] add_looping_task can be used any time (closes #2408814)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8425
diff
changeset
|
204 |
task = LoopTask(self, interval, func, args) |
8320
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
205 |
if self.running: |
8446
cae198371548
[server/utils] add_looping_task can be used any time (closes #2408814)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8425
diff
changeset
|
206 |
self._start_task(task) |
cae198371548
[server/utils] add_looping_task can be used any time (closes #2408814)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8425
diff
changeset
|
207 |
else: |
cae198371548
[server/utils] add_looping_task can be used any time (closes #2408814)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8425
diff
changeset
|
208 |
self._tasks.append(task) |
cae198371548
[server/utils] add_looping_task can be used any time (closes #2408814)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8425
diff
changeset
|
209 |
|
cae198371548
[server/utils] add_looping_task can be used any time (closes #2408814)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8425
diff
changeset
|
210 |
def _start_task(self, task): |
cae198371548
[server/utils] add_looping_task can be used any time (closes #2408814)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8425
diff
changeset
|
211 |
self._looping_tasks.append(task) |
cae198371548
[server/utils] add_looping_task can be used any time (closes #2408814)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8425
diff
changeset
|
212 |
self.info('starting task %s with interval %.2fs', task.name, |
cae198371548
[server/utils] add_looping_task can be used any time (closes #2408814)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8425
diff
changeset
|
213 |
task.interval) |
cae198371548
[server/utils] add_looping_task can be used any time (closes #2408814)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8425
diff
changeset
|
214 |
task.start() |
8320
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
215 |
|
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
216 |
def start(self): |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
217 |
"""Start running looping task""" |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
218 |
assert self.running == False # bw compat purpose maintly |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
219 |
while self._tasks: |
8446
cae198371548
[server/utils] add_looping_task can be used any time (closes #2408814)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8425
diff
changeset
|
220 |
task = self._tasks.pop() |
cae198371548
[server/utils] add_looping_task can be used any time (closes #2408814)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8425
diff
changeset
|
221 |
self._start_task(task) |
8320
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
222 |
self.running = True |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
223 |
|
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
224 |
def stop(self): |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
225 |
"""Stop all running task. |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
226 |
|
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
227 |
returns when all task have been cancel and none are running anymore""" |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
228 |
if self.running: |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
229 |
while self._looping_tasks: |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
230 |
looptask = self._looping_tasks.pop() |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
231 |
self.info('canceling task %s...', looptask.name) |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
232 |
looptask.cancel() |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
233 |
looptask.join() |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
234 |
self.info('task %s finished', looptask.name) |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
235 |
|
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
236 |
from logging import getLogger |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
237 |
from cubicweb import set_log_methods |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
238 |
set_log_methods(TasksManager, getLogger('cubicweb.repository')) |
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7823
diff
changeset
|
239 |