def got_data(self, packet): try: self.protocol.got_data(packet) except KeyboardInterrupt: raise except Exception as e: log.exception(e)
def _publish(self, events, headers): for e in events: try: json.dumps(e) except (ValueError, UnicodeDecodeError) as err: log.exception('Unable to publish event: %r %r', e, err) count = len(events) self.total += count log.info('Publish %d events (%d total)', count, self.total)
def on_message(self, body, message): try: self.qpid.send(str(body), message.headers) message.ack() self.count += 1 except Exception as e: log.exception( 'Exception while publishing message to QPID, requeueing') message.requeue() self.qpid.sender = None
def get_current_queue_depth(self): try: result = self.queue.queue_declare(passive=True) name = result.queue count = result.message_count except ChannelError: if self.count > 0: log.exception('Exception getting queue count') name = 'UNK' count = 0 return name, count, self.count
def locate_port_agent(reference_designator): try: _, data_port = CONSUL.health.service('port-agent', passing=True, tag=reference_designator) _, cmd_port = CONSUL.health.service('command-port-agent', passing=True, tag=reference_designator) if data_port and cmd_port: port = data_port[0]['Service']['Port'] addr = data_port[0]['Node']['Address'] cmd_port = cmd_port[0]['Service']['Port'] port_agent_config = {'port': port, 'cmd_port': cmd_port, 'addr': addr} return port_agent_config except StandardError: log.exception('Exception attempting to locate port agent via Consul') return None
def main(): """ This main routine will get the configuration file from the command line parameter and set the values for required URIs for the OMS, the OMS Alert Alarm Server and the qpid Server. It will then get the qpid publisher for publishing the OMS events. Finally, it will start the web service. """ global aa_publisher options = docopt(__doc__) server_config_file = options['<server_config>'] try: config = yaml.load(open(server_config_file)) except IOError: log.error('Cannot find configuration file: %r', server_config_file) return try: oms_uri = config.get('oms_uri') alert_alarm_server_uri = config.get('alert_alarm_server_uri') qpid_uri = config.get('qpid_uri') except AttributeError: log.error('Configuration file is empty: %r', server_config_file) return if not all((oms_uri, alert_alarm_server_uri, qpid_uri)): log.error('Missing mandatory configuration values missing from %r', server_config_file) else: headers = {'aaServerUri': alert_alarm_server_uri} try: aa_publisher = Publisher.from_url(qpid_uri, headers=headers) start_web_service(oms_uri, alert_alarm_server_uri) except Exception as ex: log.exception('Error starting OMS Alert and Alarm web service: %r', ex) return
def run(self): self.running = True while self.running: if not self.registered: try: ConsulServiceRegistry.register_driver(self.reference_designator, self.port) self.registered = True except StandardError: log.exception('Unable to register with Consul, ' 'will attempt again in %d secs', DRIVER_SERVICE_TTL / 2) if self.registered: try: CONSUL.agent.check.ttl_pass(self.check_id) except StandardError: # Force re-register self.registered = False log.exception('Unable to update TTL health check with Consul, ' 'will attempt again in %d secs', DRIVER_SERVICE_TTL / 2) time.sleep(DRIVER_SERVICE_TTL / 2)