Show More
@@ -1,302 +1,303 b'' | |||||
1 | """A simple engine that talks to a controller over 0MQ. |
|
1 | """A simple engine that talks to a controller over 0MQ. | |
2 | it handles registration, etc. and launches a kernel |
|
2 | it handles registration, etc. and launches a kernel | |
3 | connected to the Controller's Schedulers. |
|
3 | connected to the Controller's Schedulers. | |
4 |
|
4 | |||
5 | Authors: |
|
5 | Authors: | |
6 |
|
6 | |||
7 | * Min RK |
|
7 | * Min RK | |
8 | """ |
|
8 | """ | |
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 | # Copyright (C) 2010-2011 The IPython Development Team |
|
10 | # Copyright (C) 2010-2011 The IPython Development Team | |
11 | # |
|
11 | # | |
12 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | # Distributed under the terms of the BSD License. The full license is in | |
13 | # the file COPYING, distributed as part of this software. |
|
13 | # the file COPYING, distributed as part of this software. | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
16 | from __future__ import print_function |
|
16 | from __future__ import print_function | |
17 |
|
17 | |||
18 | import sys |
|
18 | import sys | |
19 | import time |
|
19 | import time | |
20 | from getpass import getpass |
|
20 | from getpass import getpass | |
21 |
|
21 | |||
22 | import zmq |
|
22 | import zmq | |
23 | from zmq.eventloop import ioloop, zmqstream |
|
23 | from zmq.eventloop import ioloop, zmqstream | |
24 |
|
24 | |||
25 | from IPython.external.ssh import tunnel |
|
25 | from IPython.external.ssh import tunnel | |
26 | # internal |
|
26 | # internal | |
27 | from IPython.utils.traitlets import ( |
|
27 | from IPython.utils.traitlets import ( | |
28 | Instance, Dict, Integer, Type, Float, Integer, Unicode, CBytes, Bool |
|
28 | Instance, Dict, Integer, Type, Float, Integer, Unicode, CBytes, Bool | |
29 | ) |
|
29 | ) | |
30 | from IPython.utils.py3compat import cast_bytes |
|
30 | from IPython.utils.py3compat import cast_bytes | |
31 |
|
31 | |||
32 | from IPython.parallel.controller.heartmonitor import Heart |
|
32 | from IPython.parallel.controller.heartmonitor import Heart | |
33 | from IPython.parallel.factory import RegistrationFactory |
|
33 | from IPython.parallel.factory import RegistrationFactory | |
34 | from IPython.parallel.util import disambiguate_url |
|
34 | from IPython.parallel.util import disambiguate_url | |
35 |
|
35 | |||
36 | from IPython.zmq.session import Message |
|
36 | from IPython.zmq.session import Message | |
37 | from IPython.zmq.ipkernel import Kernel, IPKernelApp |
|
37 | from IPython.zmq.ipkernel import Kernel, IPKernelApp | |
38 |
|
38 | |||
39 | class EngineFactory(RegistrationFactory): |
|
39 | class EngineFactory(RegistrationFactory): | |
40 | """IPython engine""" |
|
40 | """IPython engine""" | |
41 |
|
41 | |||
42 | # configurables: |
|
42 | # configurables: | |
43 | out_stream_factory=Type('IPython.zmq.iostream.OutStream', config=True, |
|
43 | out_stream_factory=Type('IPython.zmq.iostream.OutStream', config=True, | |
44 | help="""The OutStream for handling stdout/err. |
|
44 | help="""The OutStream for handling stdout/err. | |
45 | Typically 'IPython.zmq.iostream.OutStream'""") |
|
45 | Typically 'IPython.zmq.iostream.OutStream'""") | |
46 | display_hook_factory=Type('IPython.zmq.displayhook.ZMQDisplayHook', config=True, |
|
46 | display_hook_factory=Type('IPython.zmq.displayhook.ZMQDisplayHook', config=True, | |
47 | help="""The class for handling displayhook. |
|
47 | help="""The class for handling displayhook. | |
48 | Typically 'IPython.zmq.displayhook.ZMQDisplayHook'""") |
|
48 | Typically 'IPython.zmq.displayhook.ZMQDisplayHook'""") | |
49 | location=Unicode(config=True, |
|
49 | location=Unicode(config=True, | |
50 | help="""The location (an IP address) of the controller. This is |
|
50 | help="""The location (an IP address) of the controller. This is | |
51 | used for disambiguating URLs, to determine whether |
|
51 | used for disambiguating URLs, to determine whether | |
52 | loopback should be used to connect or the public address.""") |
|
52 | loopback should be used to connect or the public address.""") | |
53 | timeout=Float(5.0, config=True, |
|
53 | timeout=Float(5.0, config=True, | |
54 | help="""The time (in seconds) to wait for the Controller to respond |
|
54 | help="""The time (in seconds) to wait for the Controller to respond | |
55 | to registration requests before giving up.""") |
|
55 | to registration requests before giving up.""") | |
56 | max_heartbeat_misses=Integer(0, config=True, |
|
56 | max_heartbeat_misses=Integer(0, config=True, | |
57 | help="""The maximum number of times a check for the heartbeat ping of a |
|
57 | help="""The maximum number of times a check for the heartbeat ping of a | |
58 | controller can be missed before shutting down the engine. |
|
58 | controller can be missed before shutting down the engine. | |
59 |
|
59 | |||
60 | If set to 0, the check is disabled.""") |
|
60 | If set to 0, the check is disabled.""") | |
61 | sshserver=Unicode(config=True, |
|
61 | sshserver=Unicode(config=True, | |
62 | help="""The SSH server to use for tunneling connections to the Controller.""") |
|
62 | help="""The SSH server to use for tunneling connections to the Controller.""") | |
63 | sshkey=Unicode(config=True, |
|
63 | sshkey=Unicode(config=True, | |
64 | help="""The SSH private key file to use when tunneling connections to the Controller.""") |
|
64 | help="""The SSH private key file to use when tunneling connections to the Controller.""") | |
65 | paramiko=Bool(sys.platform == 'win32', config=True, |
|
65 | paramiko=Bool(sys.platform == 'win32', config=True, | |
66 | help="""Whether to use paramiko instead of openssh for tunnels.""") |
|
66 | help="""Whether to use paramiko instead of openssh for tunnels.""") | |
67 |
|
67 | |||
68 |
|
68 | |||
69 | # not configurable: |
|
69 | # not configurable: | |
70 | connection_info = Dict() |
|
70 | connection_info = Dict() | |
71 | user_ns = Dict() |
|
71 | user_ns = Dict() | |
72 | id = Integer(allow_none=True) |
|
72 | id = Integer(allow_none=True) | |
73 | registrar = Instance('zmq.eventloop.zmqstream.ZMQStream') |
|
73 | registrar = Instance('zmq.eventloop.zmqstream.ZMQStream') | |
74 | kernel = Instance(Kernel) |
|
74 | kernel = Instance(Kernel) | |
75 | hb_check_period=Integer() |
|
75 | hb_check_period=Integer() | |
76 |
|
76 | |||
77 | # States for the heartbeat monitoring |
|
77 | # States for the heartbeat monitoring | |
78 | # Initial values for monitored and pinged must satisfy "monitored > pinged == False" so that |
|
78 | # Initial values for monitored and pinged must satisfy "monitored > pinged == False" so that | |
79 | # during the first check no "missed" ping is reported. Must be floats for Python 3 compatibility. |
|
79 | # during the first check no "missed" ping is reported. Must be floats for Python 3 compatibility. | |
80 | _hb_last_pinged = 0.0 |
|
80 | _hb_last_pinged = 0.0 | |
81 | _hb_last_monitored = 0.0 |
|
81 | _hb_last_monitored = 0.0 | |
82 | _hb_missed_beats = 0 |
|
82 | _hb_missed_beats = 0 | |
83 | # The zmq Stream which receives the pings from the Heart |
|
83 | # The zmq Stream which receives the pings from the Heart | |
84 | _hb_listener = None |
|
84 | _hb_listener = None | |
85 |
|
85 | |||
86 | bident = CBytes() |
|
86 | bident = CBytes() | |
87 | ident = Unicode() |
|
87 | ident = Unicode() | |
88 | def _ident_changed(self, name, old, new): |
|
88 | def _ident_changed(self, name, old, new): | |
89 | self.bident = cast_bytes(new) |
|
89 | self.bident = cast_bytes(new) | |
90 | using_ssh=Bool(False) |
|
90 | using_ssh=Bool(False) | |
91 |
|
91 | |||
92 |
|
92 | |||
93 | def __init__(self, **kwargs): |
|
93 | def __init__(self, **kwargs): | |
94 | super(EngineFactory, self).__init__(**kwargs) |
|
94 | super(EngineFactory, self).__init__(**kwargs) | |
95 | self.ident = self.session.session |
|
95 | self.ident = self.session.session | |
96 |
|
96 | |||
97 | def init_connector(self): |
|
97 | def init_connector(self): | |
98 | """construct connection function, which handles tunnels.""" |
|
98 | """construct connection function, which handles tunnels.""" | |
99 | self.using_ssh = bool(self.sshkey or self.sshserver) |
|
99 | self.using_ssh = bool(self.sshkey or self.sshserver) | |
100 |
|
100 | |||
101 | if self.sshkey and not self.sshserver: |
|
101 | if self.sshkey and not self.sshserver: | |
102 | # We are using ssh directly to the controller, tunneling localhost to localhost |
|
102 | # We are using ssh directly to the controller, tunneling localhost to localhost | |
103 | self.sshserver = self.url.split('://')[1].split(':')[0] |
|
103 | self.sshserver = self.url.split('://')[1].split(':')[0] | |
104 |
|
104 | |||
105 | if self.using_ssh: |
|
105 | if self.using_ssh: | |
106 | if tunnel.try_passwordless_ssh(self.sshserver, self.sshkey, self.paramiko): |
|
106 | if tunnel.try_passwordless_ssh(self.sshserver, self.sshkey, self.paramiko): | |
107 | password=False |
|
107 | password=False | |
108 | else: |
|
108 | else: | |
109 | password = getpass("SSH Password for %s: "%self.sshserver) |
|
109 | password = getpass("SSH Password for %s: "%self.sshserver) | |
110 | else: |
|
110 | else: | |
111 | password = False |
|
111 | password = False | |
112 |
|
112 | |||
113 | def connect(s, url): |
|
113 | def connect(s, url): | |
114 | url = disambiguate_url(url, self.location) |
|
114 | url = disambiguate_url(url, self.location) | |
115 | if self.using_ssh: |
|
115 | if self.using_ssh: | |
116 | self.log.debug("Tunneling connection to %s via %s", url, self.sshserver) |
|
116 | self.log.debug("Tunneling connection to %s via %s", url, self.sshserver) | |
117 | return tunnel.tunnel_connection(s, url, self.sshserver, |
|
117 | return tunnel.tunnel_connection(s, url, self.sshserver, | |
118 | keyfile=self.sshkey, paramiko=self.paramiko, |
|
118 | keyfile=self.sshkey, paramiko=self.paramiko, | |
119 | password=password, |
|
119 | password=password, | |
120 | ) |
|
120 | ) | |
121 | else: |
|
121 | else: | |
122 | return s.connect(url) |
|
122 | return s.connect(url) | |
123 |
|
123 | |||
124 | def maybe_tunnel(url): |
|
124 | def maybe_tunnel(url): | |
125 | """like connect, but don't complete the connection (for use by heartbeat)""" |
|
125 | """like connect, but don't complete the connection (for use by heartbeat)""" | |
126 | url = disambiguate_url(url, self.location) |
|
126 | url = disambiguate_url(url, self.location) | |
127 | if self.using_ssh: |
|
127 | if self.using_ssh: | |
128 | self.log.debug("Tunneling connection to %s via %s", url, self.sshserver) |
|
128 | self.log.debug("Tunneling connection to %s via %s", url, self.sshserver) | |
129 | url,tunnelobj = tunnel.open_tunnel(url, self.sshserver, |
|
129 | url,tunnelobj = tunnel.open_tunnel(url, self.sshserver, | |
130 | keyfile=self.sshkey, paramiko=self.paramiko, |
|
130 | keyfile=self.sshkey, paramiko=self.paramiko, | |
131 | password=password, |
|
131 | password=password, | |
132 | ) |
|
132 | ) | |
133 | return str(url) |
|
133 | return str(url) | |
134 | return connect, maybe_tunnel |
|
134 | return connect, maybe_tunnel | |
135 |
|
135 | |||
136 | def register(self): |
|
136 | def register(self): | |
137 | """send the registration_request""" |
|
137 | """send the registration_request""" | |
138 |
|
138 | |||
139 | self.log.info("Registering with controller at %s"%self.url) |
|
139 | self.log.info("Registering with controller at %s"%self.url) | |
140 | ctx = self.context |
|
140 | ctx = self.context | |
141 | connect,maybe_tunnel = self.init_connector() |
|
141 | connect,maybe_tunnel = self.init_connector() | |
142 | reg = ctx.socket(zmq.DEALER) |
|
142 | reg = ctx.socket(zmq.DEALER) | |
143 | reg.setsockopt(zmq.IDENTITY, self.bident) |
|
143 | reg.setsockopt(zmq.IDENTITY, self.bident) | |
144 | connect(reg, self.url) |
|
144 | connect(reg, self.url) | |
145 | self.registrar = zmqstream.ZMQStream(reg, self.loop) |
|
145 | self.registrar = zmqstream.ZMQStream(reg, self.loop) | |
146 |
|
146 | |||
147 |
|
147 | |||
148 | content = dict(uuid=self.ident) |
|
148 | content = dict(uuid=self.ident) | |
149 | self.registrar.on_recv(lambda msg: self.complete_registration(msg, connect, maybe_tunnel)) |
|
149 | self.registrar.on_recv(lambda msg: self.complete_registration(msg, connect, maybe_tunnel)) | |
150 | # print (self.session.key) |
|
150 | # print (self.session.key) | |
151 | self.session.send(self.registrar, "registration_request", content=content) |
|
151 | self.session.send(self.registrar, "registration_request", content=content) | |
152 |
|
152 | |||
153 | def _report_ping(self, msg): |
|
153 | def _report_ping(self, msg): | |
154 | """Callback for when the heartmonitor.Heart receives a ping""" |
|
154 | """Callback for when the heartmonitor.Heart receives a ping""" | |
155 | #self.log.debug("Received a ping: %s", msg) |
|
155 | #self.log.debug("Received a ping: %s", msg) | |
156 | self._hb_last_pinged = time.time() |
|
156 | self._hb_last_pinged = time.time() | |
157 |
|
157 | |||
158 | def complete_registration(self, msg, connect, maybe_tunnel): |
|
158 | def complete_registration(self, msg, connect, maybe_tunnel): | |
159 | # print msg |
|
159 | # print msg | |
160 | self._abort_dc.stop() |
|
160 | self._abort_dc.stop() | |
161 | ctx = self.context |
|
161 | ctx = self.context | |
162 | loop = self.loop |
|
162 | loop = self.loop | |
163 | identity = self.bident |
|
163 | identity = self.bident | |
164 | idents,msg = self.session.feed_identities(msg) |
|
164 | idents,msg = self.session.feed_identities(msg) | |
165 | msg = self.session.unserialize(msg) |
|
165 | msg = self.session.unserialize(msg) | |
166 | content = msg['content'] |
|
166 | content = msg['content'] | |
167 | info = self.connection_info |
|
167 | info = self.connection_info | |
168 |
|
168 | |||
169 | def url(key): |
|
169 | def url(key): | |
170 | """get zmq url for given channel""" |
|
170 | """get zmq url for given channel""" | |
171 | return str(info["interface"] + ":%i" % info[key]) |
|
171 | return str(info["interface"] + ":%i" % info[key]) | |
172 |
|
172 | |||
173 | if content['status'] == 'ok': |
|
173 | if content['status'] == 'ok': | |
174 | self.id = int(content['id']) |
|
174 | self.id = int(content['id']) | |
175 |
|
175 | |||
176 | # launch heartbeat |
|
176 | # launch heartbeat | |
177 | # possibly forward hb ports with tunnels |
|
177 | # possibly forward hb ports with tunnels | |
178 | hb_ping = maybe_tunnel(url('hb_ping')) |
|
178 | hb_ping = maybe_tunnel(url('hb_ping')) | |
179 | hb_pong = maybe_tunnel(url('hb_pong')) |
|
179 | hb_pong = maybe_tunnel(url('hb_pong')) | |
180 |
|
180 | |||
181 | # Add a monitor socket which will record the last time a ping was seen |
|
|||
182 | mon = self.context.socket(zmq.SUB) |
|
|||
183 | mport = mon.bind_to_random_port('tcp://127.0.0.1') |
|
|||
184 | mon.setsockopt(zmq.SUBSCRIBE, b"") |
|
|||
185 | self._hb_listener = zmqstream.ZMQStream(mon, self.loop) |
|
|||
186 | self._hb_listener.on_recv(self._report_ping) |
|
|||
187 |
|
||||
188 | hb_monitor = None |
|
181 | hb_monitor = None | |
189 | if self.max_heartbeat_misses > 0: |
|
182 | if self.max_heartbeat_misses > 0: | |
|
183 | # Add a monitor socket which will record the last time a ping was seen | |||
|
184 | mon = self.context.socket(zmq.SUB) | |||
|
185 | mport = mon.bind_to_random_port('tcp://127.0.0.1') | |||
|
186 | mon.setsockopt(zmq.SUBSCRIBE, b"") | |||
|
187 | self._hb_listener = zmqstream.ZMQStream(mon, self.loop) | |||
|
188 | self._hb_listener.on_recv(self._report_ping) | |||
|
189 | ||||
|
190 | ||||
190 | hb_monitor = "tcp://127.0.0.1:%i"%mport |
|
191 | hb_monitor = "tcp://127.0.0.1:%i"%mport | |
191 |
|
192 | |||
192 | heart = Heart(hb_ping, hb_pong, hb_monitor , heart_id=identity) |
|
193 | heart = Heart(hb_ping, hb_pong, hb_monitor , heart_id=identity) | |
193 | heart.start() |
|
194 | heart.start() | |
194 |
|
195 | |||
195 | # create Shell Connections (MUX, Task, etc.): |
|
196 | # create Shell Connections (MUX, Task, etc.): | |
196 | shell_addrs = url('mux'), url('task') |
|
197 | shell_addrs = url('mux'), url('task') | |
197 |
|
198 | |||
198 | # Use only one shell stream for mux and tasks |
|
199 | # Use only one shell stream for mux and tasks | |
199 | stream = zmqstream.ZMQStream(ctx.socket(zmq.ROUTER), loop) |
|
200 | stream = zmqstream.ZMQStream(ctx.socket(zmq.ROUTER), loop) | |
200 | stream.setsockopt(zmq.IDENTITY, identity) |
|
201 | stream.setsockopt(zmq.IDENTITY, identity) | |
201 | shell_streams = [stream] |
|
202 | shell_streams = [stream] | |
202 | for addr in shell_addrs: |
|
203 | for addr in shell_addrs: | |
203 | connect(stream, addr) |
|
204 | connect(stream, addr) | |
204 |
|
205 | |||
205 | # control stream: |
|
206 | # control stream: | |
206 | control_addr = url('control') |
|
207 | control_addr = url('control') | |
207 | control_stream = zmqstream.ZMQStream(ctx.socket(zmq.ROUTER), loop) |
|
208 | control_stream = zmqstream.ZMQStream(ctx.socket(zmq.ROUTER), loop) | |
208 | control_stream.setsockopt(zmq.IDENTITY, identity) |
|
209 | control_stream.setsockopt(zmq.IDENTITY, identity) | |
209 | connect(control_stream, control_addr) |
|
210 | connect(control_stream, control_addr) | |
210 |
|
211 | |||
211 | # create iopub stream: |
|
212 | # create iopub stream: | |
212 | iopub_addr = url('iopub') |
|
213 | iopub_addr = url('iopub') | |
213 | iopub_socket = ctx.socket(zmq.PUB) |
|
214 | iopub_socket = ctx.socket(zmq.PUB) | |
214 | iopub_socket.setsockopt(zmq.IDENTITY, identity) |
|
215 | iopub_socket.setsockopt(zmq.IDENTITY, identity) | |
215 | connect(iopub_socket, iopub_addr) |
|
216 | connect(iopub_socket, iopub_addr) | |
216 |
|
217 | |||
217 | # disable history: |
|
218 | # disable history: | |
218 | self.config.HistoryManager.hist_file = ':memory:' |
|
219 | self.config.HistoryManager.hist_file = ':memory:' | |
219 |
|
220 | |||
220 | # Redirect input streams and set a display hook. |
|
221 | # Redirect input streams and set a display hook. | |
221 | if self.out_stream_factory: |
|
222 | if self.out_stream_factory: | |
222 | sys.stdout = self.out_stream_factory(self.session, iopub_socket, u'stdout') |
|
223 | sys.stdout = self.out_stream_factory(self.session, iopub_socket, u'stdout') | |
223 | sys.stdout.topic = cast_bytes('engine.%i.stdout' % self.id) |
|
224 | sys.stdout.topic = cast_bytes('engine.%i.stdout' % self.id) | |
224 | sys.stderr = self.out_stream_factory(self.session, iopub_socket, u'stderr') |
|
225 | sys.stderr = self.out_stream_factory(self.session, iopub_socket, u'stderr') | |
225 | sys.stderr.topic = cast_bytes('engine.%i.stderr' % self.id) |
|
226 | sys.stderr.topic = cast_bytes('engine.%i.stderr' % self.id) | |
226 | if self.display_hook_factory: |
|
227 | if self.display_hook_factory: | |
227 | sys.displayhook = self.display_hook_factory(self.session, iopub_socket) |
|
228 | sys.displayhook = self.display_hook_factory(self.session, iopub_socket) | |
228 | sys.displayhook.topic = cast_bytes('engine.%i.pyout' % self.id) |
|
229 | sys.displayhook.topic = cast_bytes('engine.%i.pyout' % self.id) | |
229 |
|
230 | |||
230 | self.kernel = Kernel(config=self.config, int_id=self.id, ident=self.ident, session=self.session, |
|
231 | self.kernel = Kernel(config=self.config, int_id=self.id, ident=self.ident, session=self.session, | |
231 | control_stream=control_stream, shell_streams=shell_streams, iopub_socket=iopub_socket, |
|
232 | control_stream=control_stream, shell_streams=shell_streams, iopub_socket=iopub_socket, | |
232 | loop=loop, user_ns=self.user_ns, log=self.log) |
|
233 | loop=loop, user_ns=self.user_ns, log=self.log) | |
233 |
|
234 | |||
234 | self.kernel.shell.display_pub.topic = cast_bytes('engine.%i.displaypub' % self.id) |
|
235 | self.kernel.shell.display_pub.topic = cast_bytes('engine.%i.displaypub' % self.id) | |
235 |
|
236 | |||
236 |
|
237 | |||
237 | # periodically check the heartbeat pings of the controller |
|
238 | # periodically check the heartbeat pings of the controller | |
238 | # Should be started here and not in "start()" so that the right period can be taken |
|
239 | # Should be started here and not in "start()" so that the right period can be taken | |
239 | # from the hubs HeartBeatMonitor.period |
|
240 | # from the hubs HeartBeatMonitor.period | |
240 | if self.max_heartbeat_misses > 0: |
|
241 | if self.max_heartbeat_misses > 0: | |
241 | # Use a slightly bigger check period than the hub signal period to not warn unnecessary |
|
242 | # Use a slightly bigger check period than the hub signal period to not warn unnecessary | |
242 | self.hb_check_period = int(content['hb_period'])+10 |
|
243 | self.hb_check_period = int(content['hb_period'])+10 | |
243 | self.log.info("Starting to monitor the heartbeat signal from the hub every %i ms." , self.hb_check_period) |
|
244 | self.log.info("Starting to monitor the heartbeat signal from the hub every %i ms." , self.hb_check_period) | |
244 | self._hb_reporter = ioloop.PeriodicCallback(self._hb_monitor, self.hb_check_period, self.loop) |
|
245 | self._hb_reporter = ioloop.PeriodicCallback(self._hb_monitor, self.hb_check_period, self.loop) | |
245 | self._hb_reporter.start() |
|
246 | self._hb_reporter.start() | |
246 | else: |
|
247 | else: | |
247 | self.log.info("Monitoring of the heartbeat signal from the hub is not enabled.") |
|
248 | self.log.info("Monitoring of the heartbeat signal from the hub is not enabled.") | |
248 |
|
249 | |||
249 |
|
250 | |||
250 | # FIXME: This is a hack until IPKernelApp and IPEngineApp can be fully merged |
|
251 | # FIXME: This is a hack until IPKernelApp and IPEngineApp can be fully merged | |
251 | app = IPKernelApp(config=self.config, shell=self.kernel.shell, kernel=self.kernel, log=self.log) |
|
252 | app = IPKernelApp(config=self.config, shell=self.kernel.shell, kernel=self.kernel, log=self.log) | |
252 | app.init_profile_dir() |
|
253 | app.init_profile_dir() | |
253 | app.init_code() |
|
254 | app.init_code() | |
254 |
|
255 | |||
255 | self.kernel.start() |
|
256 | self.kernel.start() | |
256 | else: |
|
257 | else: | |
257 | self.log.fatal("Registration Failed: %s"%msg) |
|
258 | self.log.fatal("Registration Failed: %s"%msg) | |
258 | raise Exception("Registration Failed: %s"%msg) |
|
259 | raise Exception("Registration Failed: %s"%msg) | |
259 |
|
260 | |||
260 | self.log.info("Completed registration with id %i"%self.id) |
|
261 | self.log.info("Completed registration with id %i"%self.id) | |
261 |
|
262 | |||
262 |
|
263 | |||
263 | def abort(self): |
|
264 | def abort(self): | |
264 | self.log.fatal("Registration timed out after %.1f seconds"%self.timeout) |
|
265 | self.log.fatal("Registration timed out after %.1f seconds"%self.timeout) | |
265 | if self.url.startswith('127.'): |
|
266 | if self.url.startswith('127.'): | |
266 | self.log.fatal(""" |
|
267 | self.log.fatal(""" | |
267 | If the controller and engines are not on the same machine, |
|
268 | If the controller and engines are not on the same machine, | |
268 | you will have to instruct the controller to listen on an external IP (in ipcontroller_config.py): |
|
269 | you will have to instruct the controller to listen on an external IP (in ipcontroller_config.py): | |
269 | c.HubFactory.ip='*' # for all interfaces, internal and external |
|
270 | c.HubFactory.ip='*' # for all interfaces, internal and external | |
270 | c.HubFactory.ip='192.168.1.101' # or any interface that the engines can see |
|
271 | c.HubFactory.ip='192.168.1.101' # or any interface that the engines can see | |
271 | or tunnel connections via ssh. |
|
272 | or tunnel connections via ssh. | |
272 | """) |
|
273 | """) | |
273 | self.session.send(self.registrar, "unregistration_request", content=dict(id=self.id)) |
|
274 | self.session.send(self.registrar, "unregistration_request", content=dict(id=self.id)) | |
274 | time.sleep(1) |
|
275 | time.sleep(1) | |
275 | sys.exit(255) |
|
276 | sys.exit(255) | |
276 |
|
277 | |||
277 | def _hb_monitor(self): |
|
278 | def _hb_monitor(self): | |
278 | """Callback to monitor the heartbeat from the controller""" |
|
279 | """Callback to monitor the heartbeat from the controller""" | |
279 | self._hb_listener.flush() |
|
280 | self._hb_listener.flush() | |
280 | if self._hb_last_monitored > self._hb_last_pinged: |
|
281 | if self._hb_last_monitored > self._hb_last_pinged: | |
281 | self._hb_missed_beats += 1 |
|
282 | self._hb_missed_beats += 1 | |
282 | self.log.warn("No heartbeat in the last %s ms (%s time(s) in a row).", self.hb_check_period, self._hb_missed_beats) |
|
283 | self.log.warn("No heartbeat in the last %s ms (%s time(s) in a row).", self.hb_check_period, self._hb_missed_beats) | |
283 | else: |
|
284 | else: | |
284 | #self.log.debug("Heartbeat received (after missing %s beats).", self._hb_missed_beats) |
|
285 | #self.log.debug("Heartbeat received (after missing %s beats).", self._hb_missed_beats) | |
285 | self._hb_missed_beats = 0 |
|
286 | self._hb_missed_beats = 0 | |
286 |
|
287 | |||
287 | if self._hb_missed_beats >= self.max_heartbeat_misses: |
|
288 | if self._hb_missed_beats >= self.max_heartbeat_misses: | |
288 | self.log.fatal("Maximum number of heartbeats misses reached (%s times %s ms), shutting down.", |
|
289 | self.log.fatal("Maximum number of heartbeats misses reached (%s times %s ms), shutting down.", | |
289 | self.max_heartbeat_misses, self.hb_check_period) |
|
290 | self.max_heartbeat_misses, self.hb_check_period) | |
290 | self.session.send(self.registrar, "unregistration_request", content=dict(id=self.id)) |
|
291 | self.session.send(self.registrar, "unregistration_request", content=dict(id=self.id)) | |
291 | self.loop.stop() |
|
292 | self.loop.stop() | |
292 |
|
293 | |||
293 | self._hb_last_monitored = time.time() |
|
294 | self._hb_last_monitored = time.time() | |
294 |
|
295 | |||
295 |
|
296 | |||
296 | def start(self): |
|
297 | def start(self): | |
297 | dc = ioloop.DelayedCallback(self.register, 0, self.loop) |
|
298 | dc = ioloop.DelayedCallback(self.register, 0, self.loop) | |
298 | dc.start() |
|
299 | dc.start() | |
299 | self._abort_dc = ioloop.DelayedCallback(self.abort, self.timeout*1000, self.loop) |
|
300 | self._abort_dc = ioloop.DelayedCallback(self.abort, self.timeout*1000, self.loop) | |
300 | self._abort_dc.start() |
|
301 | self._abort_dc.start() | |
301 |
|
302 | |||
302 |
|
303 |
General Comments 0
You need to be logged in to leave comments.
Login now