def connect(self): """\ Try to connect to this node's remote side. """ assert self.node_id is not None if self._server: return # already done if self._connector: # in progress: wait for it trace("remote", "Chain connect to node %d", self.node_id) yield triggeredDefer(self._connector) return if self.node_id not in self.fs.topology: trace("remote", "Not connecting to node %d: no topo", self.node_id) return if self.fs.topology[self.node_id] != self.node_id: trace("remote", "Not connecting to node %d: via %s", self.node_id, self.fs.topology[self.node_id]) return trace("remote", "Connecting to node %d", self.node_id) try: with self.fs.db() as db: try: m, = yield db.DoFn( "select method from updater where src=${src} and dest=${dest}", src=self.fs.node_id, dest=self.node_id, ) except NoData: raise NoLink(self.node_id, "No Data") m = __import__("sqlfuse.connect." + m, fromlist=("NodeClient",)) m = m.NodeClient(self) self._connector = m.connect() # Do this to avoid having a single Deferred both in the inline # callback chain and as a possible cancellation point yield triggeredDefer(self._connector) if self._server is None: raise NoLink(self.node_id, "No _server") except NoLink: raise except Exception as e: # no connection if isinstance(e, (err.ConnectionRefusedError, NoConnection)): trace("remote", "No link to %d, retrying", self.node_id) else: f = failure.Failure() log.err(f, "Connecting remote") self.queue_retry() finally: self._connector = None
def stopService(self): """Shutdown. Part of IService. Triggers a last run of the worker.""" trace('background',"StopService %s",self.__class__.__name__) try: self.do_idle = True super(BackgroundJob,self).stopService() if self.workerCall: self.workerCall.cancel() self.workerCall = None IdleWorker.add(self) d = triggeredDefer(self.workerDefer) if d is None: trace('background',"StopService %s: not running",self.__class__.__name__) else: trace('background',"StopService %s: wait for finish",self.__class__.__name__) def rep(r): trace('background',"StopService %s: finished",self.__class__.__name__) return r d.addBoth(rep) return d except Exception as e: log.err(e,"StopService "+self.__class__.__name__)