def start_injecting(self): self.injecting = True if self.listeners is not None: logging.info("Injector has connected: no longer listening for new connections") stopListening(self.listeners) # This breaks the reference loop self.listeners = None if self.do_pause: logging.info("We will pause between the injection of each event to simulate actual event inter-arrival times, so this may take a while") if self.logpath == '-': self.event_file = sys.stdin else: self.event_file = open(normalise_path(self.logpath), 'r') self._inject_events()
def stop_injecting(self): ''' Stop sending events to the data collector, close all connections. Depending on the protocol, closing the connection can erase all unread data. So we stop sending events, but delay the actual close for a short amount of time. This function sometimes reschedules itself using deferLater: any exceptions will be handled by errorCallback. ''' if not self.injecting: logging.debug("Ignoring request to stop injecting when injection is not in progress") return self.injecting = False if not self.pending_stop: self.pending_stop = True logging.debug("Scheduling injector stop after a short delay") # twisted's loseConnection says: # "Close my connection, after writing all pending data" # But it often closes the connection before sending data. # So we allow the event loop to run before we stop injecting. # IP retains unread data in the network stack, but unix # sockets don't. So we need to delay closing unix sockets, for # the data collector to finish reading. # we don't care about cancelling here, we're stopping anyway stop_deferred = task.deferLater(reactor, 1.0, self.stop_injecting) stop_deferred.addErrback(errorCallback) return if self.listeners is not None: stopListening(self.listeners) self.listeners = None # Count lines and events event_info = ("Read {} lines, {} valid times, sent {} events" .format(self.input_line_count, self.output_line_count, self.output_event_count)) # close the event log file if self.event_file is not None: self.event_file.close() self.event_file = None logging.warning("Connection closed before all events were sent. " + event_info) else: logging.debug(event_info) # close the connection from our server side if self.protocol is not None and self.protocol.transport is not None: self.protocol.transport.loseConnection() # stop the reactor gracefully stop_reactor()