author | Julien Cristau <julien.cristau@logilab.fr> |
Wed, 04 Dec 2013 19:14:43 +0100 | |
branch | stable |
changeset 9353 | aa8be290374e |
parent 8322 | cb838b126b07 |
child 9354 | 523273da6738 |
permissions | -rw-r--r-- |
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
|
1 |
# copyright 2003-2011 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 os |
|
23 |
import sys |
|
24 |
import select |
|
25 |
import warnings |
|
26 |
from time import localtime, mktime |
|
27 |
||
28 |
from cubicweb.cwconfig import CubicWebConfiguration |
|
8322
cb838b126b07
[repository] move task manager instantiation outside repository.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7879
diff
changeset
|
29 |
from cubicweb.server.utils import TasksManager |
0 | 30 |
from cubicweb.server.repository import Repository |
31 |
||
32 |
class Finished(Exception): |
|
33 |
"""raise to remove an event from the event loop""" |
|
34 |
||
35 |
class TimeEvent: |
|
36 |
"""base event""" |
|
37 |
# timefunc = staticmethod(localtime) |
|
38 |
timefunc = localtime |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
39 |
|
0 | 40 |
def __init__(self, absolute=None, period=None): |
41 |
# local time tuple |
|
42 |
if absolute is None: |
|
43 |
absolute = self.timefunc() |
|
44 |
self.absolute = absolute |
|
45 |
# optional period in seconds |
|
46 |
self.period = period |
|
47 |
||
48 |
def is_ready(self): |
|
49 |
"""return true if the event is ready to be fired""" |
|
50 |
now = self.timefunc() |
|
51 |
if self.absolute < now: |
|
52 |
return True |
|
53 |
return False |
|
54 |
||
55 |
def fire(self, server): |
|
56 |
"""fire the event |
|
57 |
must be overridden by concrete events |
|
58 |
""" |
|
59 |
raise NotImplementedError() |
|
60 |
||
61 |
def update(self): |
|
62 |
"""update the absolute date for the event or raise a finished exception |
|
63 |
""" |
|
64 |
if self.period is None: |
|
65 |
raise Finished |
|
66 |
self.absolute = localtime(mktime(self.absolute) + self.period) |
|
67 |
||
68 |
||
69 |
class QuitEvent(TimeEvent): |
|
70 |
"""stop the server""" |
|
71 |
def fire(self, server): |
|
72 |
server.repo.shutdown() |
|
73 |
server.quiting = True |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
74 |
|
0 | 75 |
|
76 |
class RepositoryServer(object): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
77 |
|
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
|
78 |
def __init__(self, config): |
0 | 79 |
"""make the repository available as a PyRO object""" |
80 |
self.config = config |
|
8322
cb838b126b07
[repository] move task manager instantiation outside repository.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7879
diff
changeset
|
81 |
self.repo = Repository(config, TasksManager()) |
0 | 82 |
self.ns = None |
83 |
self.quiting = None |
|
84 |
# event queue |
|
85 |
self.events = [] |
|
86 |
||
87 |
def add_event(self, event): |
|
88 |
"""add an event to the loop""" |
|
89 |
self.info('adding event %s', event) |
|
90 |
self.events.append(event) |
|
91 |
||
92 |
def trigger_events(self): |
|
93 |
"""trigger ready events""" |
|
94 |
for event in self.events[:]: |
|
95 |
if event.is_ready(): |
|
96 |
self.info('starting event %s', event) |
|
97 |
event.fire(self) |
|
98 |
try: |
|
99 |
event.update() |
|
100 |
except Finished: |
|
101 |
self.events.remove(event) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
102 |
|
0 | 103 |
def run(self, req_timeout=5.0): |
104 |
"""enter the service loop""" |
|
5553 | 105 |
# 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
|
106 |
self.repo.start_looping_tasks() |
0 | 107 |
while self.quiting is None: |
108 |
try: |
|
109 |
self.daemon.handleRequests(req_timeout) |
|
110 |
except select.error: |
|
111 |
continue |
|
9353
aa8be290374e
[server/pyro] try to shutdown the repository properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
8322
diff
changeset
|
112 |
finally: |
aa8be290374e
[server/pyro] try to shutdown the repository properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
8322
diff
changeset
|
113 |
self.trigger_events() |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
114 |
|
0 | 115 |
def quit(self): |
116 |
"""stop the server""" |
|
117 |
self.add_event(QuitEvent()) |
|
118 |
||
119 |
def connect(self, host='', port=0): |
|
120 |
"""the connect method on the repository only register to pyro if |
|
121 |
necessary |
|
122 |
""" |
|
123 |
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
|
124 |
|
0 | 125 |
# server utilitities ###################################################### |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
126 |
|
0 | 127 |
def install_sig_handlers(self): |
128 |
"""install signal handlers""" |
|
129 |
import signal |
|
130 |
self.info('installing signal handlers') |
|
131 |
signal.signal(signal.SIGINT, lambda x, y, s=self: s.quit()) |
|
132 |
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
|
133 |
|
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
|
134 |
|
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 |
# 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
|
136 |
# 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
|
137 |
@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
|
138 |
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
|
139 |
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
|
140 |
|
0 | 141 |
from logging import getLogger |
142 |
from cubicweb import set_log_methods |
|
143 |
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
|
144 |
set_log_methods(RepositoryServer, LOGGER) |