author | Julien Cristau <julien.cristau@logilab.fr> |
Mon, 17 Mar 2014 12:13:14 +0100 | |
changeset 9577 | c52441e4a3d7 |
parent 9468 | 39b7a91a3f4c |
permissions | -rw-r--r-- |
9468
39b7a91a3f4c
[repo] pylint cleanup, mainly of imports, with a bit of style
Julien Cristau <julien.cristau@logilab.fr>
parents:
9354
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:
4959
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:
4959
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:
4959
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:
4959
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:
4959
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:
4959
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:
4959
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:
4959
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:
4959
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:
4959
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:
4959
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:
4959
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:
4959
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:
4959
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:
4959
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5553 | 18 |
"""Pyro RQL server""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
22 |
import select |
|
23 |
from time import localtime, mktime |
|
24 |
||
8322
cb838b126b07
[repository] move task manager instantiation outside repository.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7879
diff
changeset
|
25 |
from cubicweb.server.utils import TasksManager |
0 | 26 |
from cubicweb.server.repository import Repository |
27 |
||
28 |
class Finished(Exception): |
|
29 |
"""raise to remove an event from the event loop""" |
|
30 |
||
31 |
class TimeEvent: |
|
32 |
"""base event""" |
|
33 |
# timefunc = staticmethod(localtime) |
|
34 |
timefunc = localtime |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
35 |
|
0 | 36 |
def __init__(self, absolute=None, period=None): |
37 |
# local time tuple |
|
38 |
if absolute is None: |
|
39 |
absolute = self.timefunc() |
|
40 |
self.absolute = absolute |
|
41 |
# optional period in seconds |
|
42 |
self.period = period |
|
43 |
||
44 |
def is_ready(self): |
|
45 |
"""return true if the event is ready to be fired""" |
|
46 |
now = self.timefunc() |
|
9354
523273da6738
[server] avoid race-condition in timer events
Julien Cristau <julien.cristau@logilab.fr>
parents:
9353
diff
changeset
|
47 |
if self.absolute <= now: |
0 | 48 |
return True |
49 |
return False |
|
50 |
||
51 |
def fire(self, server): |
|
52 |
"""fire the event |
|
53 |
must be overridden by concrete events |
|
54 |
""" |
|
55 |
raise NotImplementedError() |
|
56 |
||
57 |
def update(self): |
|
58 |
"""update the absolute date for the event or raise a finished exception |
|
59 |
""" |
|
60 |
if self.period is None: |
|
61 |
raise Finished |
|
62 |
self.absolute = localtime(mktime(self.absolute) + self.period) |
|
63 |
||
64 |
||
65 |
class QuitEvent(TimeEvent): |
|
66 |
"""stop the server""" |
|
67 |
def fire(self, server): |
|
68 |
server.repo.shutdown() |
|
69 |
server.quiting = True |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
70 |
|
0 | 71 |
|
72 |
class RepositoryServer(object): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
73 |
|
5814
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5692
diff
changeset
|
74 |
def __init__(self, config): |
0 | 75 |
"""make the repository available as a PyRO object""" |
76 |
self.config = config |
|
8322
cb838b126b07
[repository] move task manager instantiation outside repository.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7879
diff
changeset
|
77 |
self.repo = Repository(config, TasksManager()) |
0 | 78 |
self.ns = None |
79 |
self.quiting = None |
|
80 |
# event queue |
|
81 |
self.events = [] |
|
82 |
||
83 |
def add_event(self, event): |
|
84 |
"""add an event to the loop""" |
|
85 |
self.info('adding event %s', event) |
|
86 |
self.events.append(event) |
|
87 |
||
88 |
def trigger_events(self): |
|
89 |
"""trigger ready events""" |
|
90 |
for event in self.events[:]: |
|
91 |
if event.is_ready(): |
|
92 |
self.info('starting event %s', event) |
|
93 |
event.fire(self) |
|
94 |
try: |
|
95 |
event.update() |
|
96 |
except Finished: |
|
97 |
self.events.remove(event) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
98 |
|
0 | 99 |
def run(self, req_timeout=5.0): |
100 |
"""enter the service loop""" |
|
5553 | 101 |
# start repository looping tasks |
4959
2cb79b8a1aea
[repo] start looping task on repo only config (though I bet more work is needed to get it actually working
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
102 |
self.repo.start_looping_tasks() |
0 | 103 |
while self.quiting is None: |
104 |
try: |
|
105 |
self.daemon.handleRequests(req_timeout) |
|
106 |
except select.error: |
|
107 |
continue |
|
9353
aa8be290374e
[server/pyro] try to shutdown the repository properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
8322
diff
changeset
|
108 |
finally: |
aa8be290374e
[server/pyro] try to shutdown the repository properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
8322
diff
changeset
|
109 |
self.trigger_events() |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
110 |
|
0 | 111 |
def quit(self): |
112 |
"""stop the server""" |
|
113 |
self.add_event(QuitEvent()) |
|
114 |
||
115 |
def connect(self, host='', port=0): |
|
116 |
"""the connect method on the repository only register to pyro if |
|
117 |
necessary |
|
118 |
""" |
|
119 |
self.daemon = self.repo.pyro_register(host) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
120 |
|
0 | 121 |
# server utilitities ###################################################### |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
122 |
|
0 | 123 |
def install_sig_handlers(self): |
124 |
"""install signal handlers""" |
|
125 |
import signal |
|
126 |
self.info('installing signal handlers') |
|
127 |
signal.signal(signal.SIGINT, lambda x, y, s=self: s.quit()) |
|
128 |
signal.signal(signal.SIGTERM, lambda x, y, s=self: s.quit()) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
129 |
|
7879
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5814
diff
changeset
|
130 |
|
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5814
diff
changeset
|
131 |
# these are overridden by set_log_methods below |
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5814
diff
changeset
|
132 |
# only defining here to prevent pylint from complaining |
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5814
diff
changeset
|
133 |
@classmethod |
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5814
diff
changeset
|
134 |
def info(cls, msg, *a, **kw): |
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5814
diff
changeset
|
135 |
pass |
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5814
diff
changeset
|
136 |
|
0 | 137 |
from logging import getLogger |
138 |
from cubicweb import set_log_methods |
|
139 |
LOGGER = getLogger('cubicweb.reposerver') |
|
5550
b143444dc08a
[repo] set log methods on the actually desired class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
140 |
set_log_methods(RepositoryServer, LOGGER) |