author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 07 Sep 2010 22:58:31 +0200 | |
branch | stable |
changeset 6184 | da580218a5b3 |
parent 5814 | 51cc4b61f9ae |
child 7879 | 9aae456abab5 |
permissions | -rw-r--r-- |
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
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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 |
|
29 |
from cubicweb.server.repository import Repository |
|
30 |
||
31 |
class Finished(Exception): |
|
32 |
"""raise to remove an event from the event loop""" |
|
33 |
||
34 |
class TimeEvent: |
|
35 |
"""base event""" |
|
36 |
# timefunc = staticmethod(localtime) |
|
37 |
timefunc = localtime |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
38 |
|
0 | 39 |
def __init__(self, absolute=None, period=None): |
40 |
# local time tuple |
|
41 |
if absolute is None: |
|
42 |
absolute = self.timefunc() |
|
43 |
self.absolute = absolute |
|
44 |
# optional period in seconds |
|
45 |
self.period = period |
|
46 |
||
47 |
def is_ready(self): |
|
48 |
"""return true if the event is ready to be fired""" |
|
49 |
now = self.timefunc() |
|
50 |
if self.absolute < now: |
|
51 |
return True |
|
52 |
return False |
|
53 |
||
54 |
def fire(self, server): |
|
55 |
"""fire the event |
|
56 |
must be overridden by concrete events |
|
57 |
""" |
|
58 |
raise NotImplementedError() |
|
59 |
||
60 |
def update(self): |
|
61 |
"""update the absolute date for the event or raise a finished exception |
|
62 |
""" |
|
63 |
if self.period is None: |
|
64 |
raise Finished |
|
65 |
self.absolute = localtime(mktime(self.absolute) + self.period) |
|
66 |
||
67 |
||
68 |
class QuitEvent(TimeEvent): |
|
69 |
"""stop the server""" |
|
70 |
def fire(self, server): |
|
71 |
server.repo.shutdown() |
|
72 |
server.quiting = True |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
73 |
|
0 | 74 |
|
75 |
class RepositoryServer(object): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
76 |
|
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
|
77 |
def __init__(self, config): |
0 | 78 |
"""make the repository available as a PyRO object""" |
79 |
self.config = config |
|
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
|
80 |
self.repo = Repository(config) |
0 | 81 |
self.ns = None |
82 |
self.quiting = None |
|
83 |
# event queue |
|
84 |
self.events = [] |
|
85 |
||
86 |
def add_event(self, event): |
|
87 |
"""add an event to the loop""" |
|
88 |
self.info('adding event %s', event) |
|
89 |
self.events.append(event) |
|
90 |
||
91 |
def trigger_events(self): |
|
92 |
"""trigger ready events""" |
|
93 |
for event in self.events[:]: |
|
94 |
if event.is_ready(): |
|
95 |
self.info('starting event %s', event) |
|
96 |
event.fire(self) |
|
97 |
try: |
|
98 |
event.update() |
|
99 |
except Finished: |
|
100 |
self.events.remove(event) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
101 |
|
0 | 102 |
def run(self, req_timeout=5.0): |
103 |
"""enter the service loop""" |
|
5553 | 104 |
# 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
|
105 |
self.repo.start_looping_tasks() |
0 | 106 |
while self.quiting is None: |
107 |
try: |
|
108 |
self.daemon.handleRequests(req_timeout) |
|
109 |
except select.error: |
|
110 |
continue |
|
111 |
self.trigger_events() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
112 |
|
0 | 113 |
def quit(self): |
114 |
"""stop the server""" |
|
115 |
self.add_event(QuitEvent()) |
|
116 |
||
117 |
def connect(self, host='', port=0): |
|
118 |
"""the connect method on the repository only register to pyro if |
|
119 |
necessary |
|
120 |
""" |
|
121 |
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
|
122 |
|
0 | 123 |
# server utilitities ###################################################### |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
124 |
|
0 | 125 |
def install_sig_handlers(self): |
126 |
"""install signal handlers""" |
|
127 |
import signal |
|
128 |
self.info('installing signal handlers') |
|
129 |
signal.signal(signal.SIGINT, lambda x, y, s=self: s.quit()) |
|
130 |
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
|
131 |
|
0 | 132 |
from logging import getLogger |
133 |
from cubicweb import set_log_methods |
|
134 |
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
|
135 |
set_log_methods(RepositoryServer, LOGGER) |