def _getTraceback(self, e): """Turn an exception into a LabRAD error packet. We may send a traceback of the error, depending on the value of self.sendTracebacks. """ code = getattr(e, 'code', 0) if self.sendTracebacks: f = failure.Failure() tb = f.getTraceback(elideFrameworkCode=True) msg = 'Remote %s' % tb else: msg = e.__class__.__name__ + ': ' + getattr(e, 'msg', str(e)) msg = '[%s] %s' % (self.name, msg) return T.Error(msg, code)
def processEnded(self, reason): """Called when the server process ends. We check to see the reason why this process failed, and then call the appropriate deferred, depending on the current state. """ if isinstance(reason.value, ProcessDone): print("'%s': process closed cleanly." % self.name) elif isinstance(reason.value, ProcessTerminated): print("'%s': process terminated: %s" % (self.name, reason.value)) else: print("'%s': process ended: %s" % (self.name, reason)) self.started = False if self.starting: err = T.Error('Startup failed.', payload=self.output) self.startup.errback(err) elif self.stopping: self.shutdown.callback(None) else: # looks like this thing died on its own self.emitMessage('server_stopped')
def start(self): self.set_status('STARTING') self.logger.info("path: {}".format(self.path)) self.logger.info("args: {}".format(self.args)) msg_ctx = self.client.context() msg_id = msg_ctx[1] connected = defer.Deferred() def on_server_connect(message_ctx, msg): """Handler that will be called when labrad servers connect. If we see a server whose name matches the one we are trying to start, we assume that the server process we spawned has successfully connected. So, we fire the `connected` Deferred. """ _ID, name = msg if name == self.name: connected.callback(None) # start listening for server connect messages manager = self.client.manager manager.addListener(on_server_connect, context=msg_ctx) yield manager.subscribe_to_named_message(SERVER_CONNECTED, msg_id, True, context=msg_ctx) # start the server process self.proc = reactor.spawnProcess(self, self.executable, self.args, env=self.full_env, path=self.path) # wait for the server to connect to labrad, shutdown, or timeout, # whichever comes first. selected = yield mux.select({ 'connected': connected, 'shutdown': self.on_shutdown(), 'timeout': mux.after(self.timeout) }) # stop listening for server connect messages try: manager.removeListener(on_server_connect, context=msg_ctx) yield manager.subscribe_to_named_message(SERVER_CONNECTED, msg_id, False, context=msg_ctx) except Exception: self.logger.info('Error while unsubscribing from labrad messages', exc_info=True) if selected.key == 'timeout': yield self._kill() raise T.Error('Failed to connect to labrad in {} seconds.'.format( self.timeout), payload=self.output) if selected.key == 'shutdown': raise T.Error('Process exited before connecting to labrad.', payload=self.output) # we're connected! self.set_status('STARTED')