author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Wed, 02 Jun 2010 18:12:27 +0200 | |
branch | stable |
changeset 5644 | 73d8a757db80 |
parent 5424 | 8ecbcbff9777 |
child 5550 | b143444dc08a |
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/>. |
0 | 18 |
"""Pyro RQL server |
19 |
||
20 |
""" |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
23 |
import os |
|
24 |
import sys |
|
25 |
import select |
|
26 |
import warnings |
|
27 |
from time import localtime, mktime |
|
28 |
||
29 |
from cubicweb.cwconfig import CubicWebConfiguration |
|
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 |
|
0 | 78 |
def __init__(self, config, debug=False): |
79 |
"""make the repository available as a PyRO object""" |
|
80 |
self.config = config |
|
81 |
self.repo = Repository(config, debug=debug) |
|
82 |
self.ns = None |
|
83 |
self.quiting = None |
|
84 |
# event queue |
|
85 |
self.events = [] |
|
86 |
# start repository looping tasks |
|
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""" |
|
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 |
|
112 |
self.trigger_events() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
113 |
|
0 | 114 |
def quit(self): |
115 |
"""stop the server""" |
|
116 |
self.add_event(QuitEvent()) |
|
117 |
||
118 |
def connect(self, host='', port=0): |
|
119 |
"""the connect method on the repository only register to pyro if |
|
120 |
necessary |
|
121 |
""" |
|
122 |
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
|
123 |
|
0 | 124 |
# server utilitities ###################################################### |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
125 |
|
0 | 126 |
def install_sig_handlers(self): |
127 |
"""install signal handlers""" |
|
128 |
import signal |
|
129 |
self.info('installing signal handlers') |
|
130 |
signal.signal(signal.SIGINT, lambda x, y, s=self: s.quit()) |
|
131 |
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
|
132 |
|
0 | 133 |
def daemonize(self, pid_file=None): |
134 |
"""daemonize the process""" |
|
135 |
# fork so the parent can exist |
|
136 |
if (os.fork()): |
|
137 |
return -1 |
|
138 |
# deconnect from tty and create a new session |
|
139 |
os.setsid() |
|
140 |
# fork again so the parent, (the session group leader), can exit. |
|
141 |
# as a non-session group leader, we can never regain a controlling |
|
142 |
# terminal. |
|
143 |
if (os.fork()): |
|
144 |
return -1 |
|
145 |
# move to the root to avoit mount pb |
|
146 |
os.chdir('/') |
|
147 |
# set paranoid umask |
|
148 |
os.umask(077) |
|
149 |
if pid_file is not None: |
|
150 |
# write pid in a file |
|
151 |
f = open(pid_file, 'w') |
|
152 |
f.write(str(os.getpid())) |
|
153 |
f.close() |
|
154 |
# filter warnings |
|
155 |
warnings.filterwarnings('ignore') |
|
156 |
# close standard descriptors |
|
157 |
sys.stdin.close() |
|
158 |
sys.stdout.close() |
|
159 |
sys.stderr.close() |
|
160 |
||
161 |
from logging import getLogger |
|
162 |
from cubicweb import set_log_methods |
|
163 |
LOGGER = getLogger('cubicweb.reposerver') |
|
164 |
set_log_methods(CubicWebConfiguration, LOGGER) |