author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 04 Jun 2010 13:09:12 +0200 | |
branch | stable |
changeset 5670 | 80dc2135bf5f |
parent 5553 | 646737f80c88 |
child 5692 | 59ceed788bdb |
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 |
||
5553 | 28 |
from logilab.common.daemon import daemonize |
29 |
||
0 | 30 |
from cubicweb.cwconfig import CubicWebConfiguration |
31 |
from cubicweb.server.repository import Repository |
|
32 |
||
33 |
class Finished(Exception): |
|
34 |
"""raise to remove an event from the event loop""" |
|
35 |
||
36 |
class TimeEvent: |
|
37 |
"""base event""" |
|
38 |
# timefunc = staticmethod(localtime) |
|
39 |
timefunc = localtime |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
40 |
|
0 | 41 |
def __init__(self, absolute=None, period=None): |
42 |
# local time tuple |
|
43 |
if absolute is None: |
|
44 |
absolute = self.timefunc() |
|
45 |
self.absolute = absolute |
|
46 |
# optional period in seconds |
|
47 |
self.period = period |
|
48 |
||
49 |
def is_ready(self): |
|
50 |
"""return true if the event is ready to be fired""" |
|
51 |
now = self.timefunc() |
|
52 |
if self.absolute < now: |
|
53 |
return True |
|
54 |
return False |
|
55 |
||
56 |
def fire(self, server): |
|
57 |
"""fire the event |
|
58 |
must be overridden by concrete events |
|
59 |
""" |
|
60 |
raise NotImplementedError() |
|
61 |
||
62 |
def update(self): |
|
63 |
"""update the absolute date for the event or raise a finished exception |
|
64 |
""" |
|
65 |
if self.period is None: |
|
66 |
raise Finished |
|
67 |
self.absolute = localtime(mktime(self.absolute) + self.period) |
|
68 |
||
69 |
||
70 |
class QuitEvent(TimeEvent): |
|
71 |
"""stop the server""" |
|
72 |
def fire(self, server): |
|
73 |
server.repo.shutdown() |
|
74 |
server.quiting = True |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
75 |
|
0 | 76 |
|
77 |
class RepositoryServer(object): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
78 |
|
0 | 79 |
def __init__(self, config, debug=False): |
80 |
"""make the repository available as a PyRO object""" |
|
81 |
self.config = config |
|
82 |
self.repo = Repository(config, debug=debug) |
|
83 |
self.ns = None |
|
84 |
self.quiting = None |
|
85 |
# event queue |
|
86 |
self.events = [] |
|
87 |
||
88 |
def add_event(self, event): |
|
89 |
"""add an event to the loop""" |
|
90 |
self.info('adding event %s', event) |
|
91 |
self.events.append(event) |
|
92 |
||
93 |
def trigger_events(self): |
|
94 |
"""trigger ready events""" |
|
95 |
for event in self.events[:]: |
|
96 |
if event.is_ready(): |
|
97 |
self.info('starting event %s', event) |
|
98 |
event.fire(self) |
|
99 |
try: |
|
100 |
event.update() |
|
101 |
except Finished: |
|
102 |
self.events.remove(event) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
103 |
|
0 | 104 |
def run(self, req_timeout=5.0): |
105 |
"""enter the service loop""" |
|
5553 | 106 |
# 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
|
107 |
self.repo.start_looping_tasks() |
0 | 108 |
while self.quiting is None: |
109 |
try: |
|
110 |
self.daemon.handleRequests(req_timeout) |
|
111 |
except select.error: |
|
112 |
continue |
|
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 |
|
0 | 134 |
from logging import getLogger |
135 |
from cubicweb import set_log_methods |
|
136 |
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
|
137 |
set_log_methods(RepositoryServer, LOGGER) |