def report(timestamp, vin, data): logger.info('Tracking Callback Server: Report request: Time: %s, VIN: %s, Data: %s.', timestamp, vin, data) # get the vehicle record from the database try: vehicle = Vehicle.objects.get(veh_vin = vin) except Exception as e: logger.error("Tracking Callback Server: Cannot retrieve vehicle '%s' from database. Error: %s", vin, e) return {u'status': 0} location = Location() location.loc_vehicle = vehicle location.loc_time = timestamp for channel in data: key = channel['channel'] value = channel['value'] if key == 'location': location.loc_latitude = value['lat'] location.loc_longitude = value['lon'] location.loc_altitude = value['alt'] elif key == 'speed': location.loc_speed = float(value) elif key == 'odometer': location.loc_odometer = float(value) location.save() return {u'status': 0}
def run(self): while self.server_on_flag: self.m = self.cons.get_message(block=False) if (self.m is not None): self.payload = json.loads(self.m.message.value) self.vin = str(self.payload['vin']) self.time = str(self.payload['timestamp']) self.data = str(self.payload['data']) self.row_key = self.vin + self.time try: self.car_table.put(self.vin, {'user:mostrecent': self.time}) self.car_table.put(self.row_key, {'car:data': self.data}) self.count = self.count + 1 logger.info( 'HBase Server: key: %s, table: %s, car{data: %s}. Message number: %s', self.row_key, 'rvi', self.data, str(self.count)) except Exception as e: logger.info('%s,Data Push into HBase unsuccessful...', e) else: sleep(1 / 5)
def report(timestamp, vin, data): """ Log the location record """ conf = get_settings() kafka = None logger.info('Kafka MQ Server: Report Request: Time: %s, VIN: %s, Data: %s.', timestamp, vin, data) payload = {} payload['timestamp'] = timestamp payload['vin'] = vin payload['data'] = data # Connect to Kafka Message Queue Server try: kafka = KafkaClient(conf['TRACKING_MQ_URL']) except: logger.error("%s: Kafka Message Queue Server unavailable:", conf['TRACKING_MQ_URL']) kafka = None return False producer = SimpleProducer(kafka) producer.send_messages(conf['TRACKING_MQ_TOPIC'], json.dumps(payload)) logger.info("%s: Report data published to message queue.", conf['TRACKING_MQ_URL']) return True
def register_services(self): # register callback functions with RPC server self.localServer.register_function(report, self.service_id + "/report") # register services with RVI framework result = self.service_edge.register_service(service = self.service_id+'/report', network_address = self.callback_url) logger.info('Tracking Service Registration: Report service name: %s', result['service'])
def cleanup(self, *args): rvi_logger.info('RVI Server: Caught signal: %d. Shutting down...', args[0]) if self.sota_cb_server: self.sota_cb_server.shutdown() if self.sota_tx_server: self.sota_tx_server.shutdown() if self.tracking_cb_server: self.tracking_cb_server.shutdown() if self.mq_sink_server: self.mq_sink_server.shutdown() if self.hbase_server: self.hbase_server.shutdown() sys.exit(0)
def cancel_download(retry): rvi_logger.info("SOTA Callback Server: Cancel download request: retry: %s.", retry) retry_obj = None update_obj = None try: retry_obj = sota.models.Retry.objects.filter(pk=retry)[0] update_obj = retry_obj.ret_update except: rvi_logger.error("SOTA Callback Server: Cancel downlaod request: Cannot access database object: %s", retry) return {u"status": 0} retry_obj.set_status(sota.models.Status.REJECTED) update_obj.set_status(sota.models.Status.REJECTED) sota_logger.info("SOTA Callback Server: Cancel download request: %s.", retry_obj) return {u"status": 0}
def download_complete(status, retry): rvi_logger.info("SOTA Callback Server: Download complete: retry: %s, status: %s.", retry, status) retry_obj = None update_obj = None try: retry_obj = sota.models.Retry.objects.filter(pk=retry)[0] update_obj = retry_obj.ret_update except: rvi_logger.error("SOTA Callback Server: Download complete: Cannot access database object: %s", retry) return {u"status": 0} if int(status) == 0: retry_obj.set_status(sota.models.Status.SUCCESS) update_obj.set_status(sota.models.Status.SUCCESS) else: retry_obj.set_status(sota.models.Status.FAILED) update_obj.set_status(sota.models.Status.FAILED) sota_logger.info("SOTA Callback Server: Download complete: retry: %s, status: %s.", retry_obj, status) return {u"status": 0}
def cancel_download(retry): rvi_logger.info( 'SOTA Callback Server: Cancel download request: retry: %s.', retry) retry_obj = None update_obj = None try: retry_obj = sota.models.Retry.objects.filter(pk=retry)[0] update_obj = retry_obj.ret_update except: rvi_logger.error( 'SOTA Callback Server: Cancel downlaod request: Cannot access database object: %s', retry) return {u'status': 0} retry_obj.set_status(sota.models.Status.REJECTED) update_obj.set_status(sota.models.Status.REJECTED) sota_logger.info('SOTA Callback Server: Cancel download request: %s.', retry_obj) return {u'status': 0}
def register_services(self): # register callback functions with RPC server self.localServer.register_function( initiate_download, self.service_id + "/initiate_download") self.localServer.register_function( cancel_download, self.service_id + "/cancel_download") self.localServer.register_function( download_complete, self.service_id + "/download_complete") # register services with RVI framework result = self.service_edge.register_service( service=self.service_id + '/initiate_download', network_address=self.callback_url) rvi_logger.info( 'SOTA Service Registration: Initiate download service name: %s', result['service']) result = self.service_edge.register_service( service=self.service_id + '/cancel_download', network_address=self.callback_url) rvi_logger.info( 'SOTA Service Registration: Cancel download service name: %s', result['service']) result = self.service_edge.register_service( service=self.service_id + '/download_complete', network_address=self.callback_url) rvi_logger.info( 'SOTA Service Registration: Download complete service name: %s', result['service'])
def run(self): while self.server_on_flag: self.m = self.cons.get_message(block=False) if (self.m is not None): self.payload = json.loads(self.m.message.value) self.vin = str(self.payload['vin']) self.time = str(self.payload['timestamp']) self.data = str(self.payload['data']) self.row_key = self.vin+self.time try: self.car_table.put(self.vin,{'user:mostrecent':self.time}) self.car_table.put(self.row_key,{'car:data':self.data}) self.count = self.count + 1 logger.info('HBase Server: key: %s, table: %s, car{data: %s}. Message number: %s', self.row_key, 'rvi', self.data, str(self.count)) except Exception as e: logger.info('%s,Data Push into HBase unsuccessful...', e) else: sleep(1/5)
def download_complete(status, retry): rvi_logger.info( 'SOTA Callback Server: Download complete: retry: %s, status: %s.', retry, status) retry_obj = None update_obj = None try: retry_obj = sota.models.Retry.objects.filter(pk=retry)[0] update_obj = retry_obj.ret_update except: rvi_logger.error( 'SOTA Callback Server: Download complete: Cannot access database object: %s', retry) return {u'status': 0} if int(status) == 0: retry_obj.set_status(sota.models.Status.SUCCESS) update_obj.set_status(sota.models.Status.SUCCESS) else: retry_obj.set_status(sota.models.Status.FAILED) update_obj.set_status(sota.models.Status.FAILED) sota_logger.info( 'SOTA Callback Server: Download complete: retry: %s, status: %s.', retry_obj, status) return {u'status': 0}
def report(timestamp, vin, data): logger.info( 'Tracking Callback Server: Report request: Time: %s, VIN: %s, Data: %s.', timestamp, vin, data) # get the vehicle record from the database try: vehicle = Vehicle.objects.get(veh_vin=vin) except Exception as e: logger.error( "Tracking Callback Server: Cannot retrieve vehicle '%s' from database. Error: %s", vin, e) return {u'status': 0} location = Location() location.loc_vehicle = vehicle try: location.loc_time = datetime.fromtimestamp(float(timestamp)) except: location.loc_time = timestamp if isinstance(data, str) or isinstance(data, unicode): logger.info('data is string') data = ast.literal_eval(data) for channel in data: key = channel['channel'] value = channel['value'] logger.info('%s: %s', key, value) if key == 'location': location.loc_latitude = value['lat'] location.loc_longitude = value['lon'] location.loc_altitude = value['alt'] elif key == 'speed': location.loc_speed = float(value) elif key == 'odometer': location.loc_odometer = float(value) print 'Saving', location location.save() logger.info('Saved') return {u'status': 0}
def register_services(self): # register callback functions with RPC server self.localServer.register_function(initiate_download, self.service_id + "/initiate_download") self.localServer.register_function(cancel_download, self.service_id + "/cancel_download") self.localServer.register_function(download_complete, self.service_id + "/download_complete") # register services with RVI framework result = self.service_edge.register_service( service=self.service_id + "/initiate_download", network_address=self.callback_url ) rvi_logger.info("SOTA Service Registration: Initiate download service name: %s", result["service"]) result = self.service_edge.register_service( service=self.service_id + "/cancel_download", network_address=self.callback_url ) rvi_logger.info("SOTA Service Registration: Cancel download service name: %s", result["service"]) result = self.service_edge.register_service( service=self.service_id + "/download_complete", network_address=self.callback_url ) rvi_logger.info("SOTA Service Registration: Download complete service name: %s", result["service"])
def shutdown(self): self.server_on_flag = False logger.info('HBase Server shutting down...')
def run(self): while True: [package, destination, retry] = package_queue.get() rvi_logger.info("SOTA Transmission Server: Sending package %s to %s", package, destination) # accessing database objects retry_obj = None update_obj = None package_obj = None try: retry_obj = sota.models.Retry.objects.filter(pk=retry)[0] update_obj = retry_obj.ret_update package_obj = update_obj.upd_package except Exception as e: rvi_logger.error("SOTA Transmission Server: Cannot access database object: %s, Error: %s", retry, e) continue try: f = open(package_obj.pac_file.path) except Exception as e: sota_logger.error( "SOTA Transmission Server: %s: Cannot open file: %s", retry_obj, package_obj.pac_file.path ) retry_obj.set_status(sota.models.Status.FAILED) update_obj.set_status(sota.models.Status.FAILED) continue retry_obj.set_status(sota.models.Status.RUNNING) update_obj.set_status(sota.models.Status.RUNNING) f_stat = os.stat(package_obj.pac_file.path) self.transaction_id += 1 self.service_edge.message( calling_service=self.service_id, service_name=destination + "/start", transaction_id=str(self.transaction_id), timeout=int(retry_obj.get_timeout_epoch()), parameters=[{u"package": package, u"chunk_size": self.chunk_size, u"total_size": f_stat.st_size}], ) index = 0 while True: msg = f.read(self.chunk_size) if msg == "": break sota_logger.debug( "SOTA Transmission Server: %s: Sending package: %s, chunk: %d, message size: %s", retry_obj, package, index, len(msg), ) self.transaction_id += 1 self.service_edge.message( calling_service=self.service_id, service_name=destination + "/chunk", transaction_id=str(self.transaction_id), timeout=int(retry_obj.get_timeout_epoch()), parameters=[{u"index": index}, {u"msg": base64.b64encode(msg)}], ) time.sleep(0.05) index += 1 f.close() time.sleep(1.0) sota_logger.info("SOTA Transmission Server: %s: Finishing package: %s", retry_obj, package) self.transaction_id += 1 self.service_edge.message( calling_service=self.service_id, service_name=destination + "/finish", transaction_id=str(self.transaction_id), timeout=int(retry_obj.get_timeout_epoch()), parameters=[{u"dummy": 0}], )
def initiate_download(package, destination, retry): rvi_logger.info( 'SOTA Callback Server: Initiate download request: retry %s - %s to %s.', retry, package, destination) package_queue.put([package, destination, retry]) return {u'status': 0}
def run(self): while True: [package, destination, retry] = package_queue.get() rvi_logger.info( 'SOTA Transmission Server: Sending package %s to %s', package, destination) # accessing database objects retry_obj = None update_obj = None package_obj = None try: retry_obj = sota.models.Retry.objects.filter(pk=retry)[0] update_obj = retry_obj.ret_update package_obj = update_obj.upd_package except Exception as e: rvi_logger.error( 'SOTA Transmission Server: Cannot access database object: %s, Error: %s', retry, e) continue try: f = open(package_obj.pac_file.path) except Exception as e: sota_logger.error( 'SOTA Transmission Server: %s: Cannot open file: %s', retry_obj, package_obj.pac_file.path) retry_obj.set_status(sota.models.Status.FAILED) update_obj.set_status(sota.models.Status.FAILED) continue retry_obj.set_status(sota.models.Status.RUNNING) update_obj.set_status(sota.models.Status.RUNNING) f_stat = os.stat(package_obj.pac_file.path) self.transaction_id += 1 self.service_edge.message(calling_service=self.service_id, service_name=destination + "/start", transaction_id=str(self.transaction_id), timeout=int( retry_obj.get_timeout_epoch()), parameters=[{ u'package': package, u'chunk_size': self.chunk_size, u'total_size': f_stat.st_size }]) index = 0 while True: msg = f.read(self.chunk_size) if msg == "": break sota_logger.debug( 'SOTA Transmission Server: %s: Sending package: %s, chunk: %d, message size: %s', retry_obj, package, index, len(msg)) self.transaction_id += 1 self.service_edge.message( calling_service=self.service_id, service_name=destination + "/chunk", transaction_id=str(self.transaction_id), timeout=int(retry_obj.get_timeout_epoch()), parameters=[{ u'index': index }, { u'msg': base64.b64encode(msg) }]) time.sleep(0.05) index += 1 f.close() time.sleep(1.0) sota_logger.info( 'SOTA Transmission Server: %s: Finishing package: %s', retry_obj, package) self.transaction_id += 1 self.service_edge.message(calling_service=self.service_id, service_name=destination + "/finish", transaction_id=str(self.transaction_id), timeout=int( retry_obj.get_timeout_epoch()), parameters=[{ u'dummy': 0 }])
def run(self): # Execution starts here rvi_logger.info('RVI Server: Starting...') conf = get_settings() rvi_logger.info('RVI Server: General Configuration: ' + 'RVI_SERVICE_EDGE_URL: ' + conf['SERVICE_EDGE_URL'] + ', ' + 'MEDIA_ROOT: ' + conf['MEDIA_ROOT'] ) # setup RVI Service Edge rvi_logger.info('RVI Server: Setting up outbound connection to RVI Service Edge at %s', conf['SERVICE_EDGE_URL']) self.rvi_service_edge = jsonrpclib.Server(conf['SERVICE_EDGE_URL']) # SOTA Startup if conf['SOTA_ENABLE'] == True: # log SOTA configuration rvi_logger.info('RVI Server: SOTA Configuration: ' + 'RVI_SOTA_CALLBACK_URL: ' + conf['SOTA_CALLBACK_URL'] + ', ' + 'RVI_SOTA_SERVICE_ID: ' + conf['SOTA_SERVICE_ID'] + ', ' + 'RVI_SOTA_CHUNK_SIZE: ' + str(conf['SOTA_CHUNK_SIZE']) ) # start the SOTA callback server try: rvi_logger.info('RVI Server: Starting SOTA Callback Server on %s with service id %s.', conf['SOTA_CALLBACK_URL'], conf['SOTA_SERVICE_ID']) self.sota_cb_server = SOTACallbackServer(self.rvi_service_edge, conf['SOTA_CALLBACK_URL'], conf['SOTA_SERVICE_ID']) self.sota_cb_server.start() rvi_logger.info('RVI Server: SOTA Callback Server started.') except Exception as e: rvi_logger.error('RVI Server: Cannot start SOTA Callback Server: %s', e) sys.exit(1) # wait for SOTA callback server to come up time.sleep(0.5) # start SOTA Transmission Server try: rvi_logger.info('RVI Server: Starting SOTA Transmission Server.') self.sota_tx_server = SOTATransmissionServer(self.rvi_service_edge, conf['SOTA_SERVICE_ID'], conf['SOTA_CHUNK_SIZE']) self.sota_tx_server.start() rvi_logger.info('RVI Server: SOTA Transmission Server started.') except Exception as e: rvi_logger.error('RVI Server: Cannot start SOTA Transmission Server: %s', e) sys.exit(1) # wait for SOTA transmission server to come up time.sleep(0.5) # Tracking Startup if conf['TRACKING_ENABLE'] == True: # log Tracking configuration rvi_logger.info('RVI Server: Tracking Configuration: ' + 'RVI_TRACKING_CALLBACK_URL: ' + conf['TRACKING_CALLBACK_URL'] + ', ' + 'RVI_TRACKING_SERVICE_ID: ' + conf['TRACKING_SERVICE_ID'] ) # start the Tracking callback server try: rvi_logger.info('RVI Server: Starting Tracking Callback Server on %s with service id %s.', conf['TRACKING_CALLBACK_URL'], conf['TRACKING_SERVICE_ID']) self.tracking_cb_server = TrackingCallbackServer(self.rvi_service_edge, conf['TRACKING_CALLBACK_URL'], conf['TRACKING_SERVICE_ID']) self.tracking_cb_server.start() rvi_logger.info('RVI Server: Tracking Callback Server started.') except Exception as e: rvi_logger.error('RVI Server: Cannot start Tracking Callback Server: %s', e) sys.exit(1) # wait for SOTA callback server to come up time.sleep(0.5) else: rvi_logger.info('RVI Server: Tracking not enabled') # Publish to Kafka Message Queue if conf['TRACKING_MQ_PUBLISH'] == True: #log kafka configuration rvi_logger.info('RVI Server: Publishing to Kafka Message Queue: ' + conf['TRACKING_MQ_URL'] + ' , with topic: ' + conf['TRACKING_MQ_TOPIC']) #Start the Kafka message queue forwarding server try: rvi_logger.info('%s: Publishing to message queue enabled.', self.__class__.__name__) self.mq_sink_server = MQSinkServer(self.rvi_service_edge, conf['TRACKING_CALLBACK_URL'], conf['TRACKING_SERVICE_ID']) self.mq_sink_server.start() rvi_logger.info('RVI Server: Message Queue Server started.') except Exception as e: rvi_logger.error('RVI Server: Cannot start Message Queue Server: %s', e) sys.exit(1) else: rvi_logger.info('RVI Server: MQ Publish not enabled') # Save message Queue contents into HBase if conf['TRACKING_MQ_HBASE'] == True: rvi_logger.info('RVI Server: Saving to HBase: ' + conf['TRACKING_MQ_HBASE_URL']) #Start HBase Server thread try: rvi_logger.info('%s: Saving messages to HBase enabled.', self.__class__.__name__) self.hbase_server = HBaseServer(conf['TRACKING_MQ_URL'],conf['TRACKING_MQ_TOPIC'],conf['TRACKING_MQ_HBASE_URL'], conf['TRACKING_MQ_HBASE_PORT'], conf['TRACKING_MQ_HBASE_TABLE']) self.hbase_server.start() rvi_logger.info('RVI Server: Kafka -> HBase consumer started.') except Exception as e: rvi_logger.error('RVI Server: Cannot start HBase Server: %s', e) sys.exit(1) else: rvi_logger.info('RVI Server: HBase server storage not enabled') # catch signals for proper shutdown for sig in (SIGABRT, SIGTERM, SIGINT): signal(sig, self.cleanup) # main execution loop timeout = conf['DB_CLOSE_TIMEOUT'] while True: try: time.sleep(conf['DB_PING_INTERVAL']) # If we are idle too long the database server may # close the connection on us, ping the server to check if # the connection is still up. if (connection.connection is not None): if (connection.is_usable() == True): rvi_logger.debug('RVI Server: Database connection is up.') # Close connection if open longer than the timeout timeout -= conf['DB_PING_INTERVAL'] if (timeout <= 0): connection.close() timeout = conf['DB_CLOSE_TIMEOUT'] rvi_logger.info('RVI Server: Idle Timeout: closed database connection.') else: rvi_logger.error('RVI Server: Database connection is down.') connection.close() elif (conf['TRACKING_MQ_PUBLISH'] == True and conf['TRACKING_ENABLE'] == False): pass else: rvi_logger.error('RVI Server: Database connection is closed.') # As long as the connection is closed reset the timeout timeout = conf['DB_CLOSE_TIMEOUT'] except KeyboardInterrupt: print ('\n') break
def initiate_download(package, destination, retry): rvi_logger.info( "SOTA Callback Server: Initiate download request: retry %s - %s to %s.", retry, package, destination ) package_queue.put([package, destination, retry]) return {u"status": 0}
def run(self): # Execution starts here rvi_logger.info('RVI Server: Starting...') conf = get_settings() rvi_logger.info('RVI Server: General Configuration: ' + 'RVI_SERVICE_EDGE_URL: ' + conf['SERVICE_EDGE_URL'] + ', ' + 'MEDIA_ROOT: ' + conf['MEDIA_ROOT']) # setup RVI Service Edge rvi_logger.info( 'RVI Server: Setting up outbound connection to RVI Service Edge at %s', conf['SERVICE_EDGE_URL']) self.rvi_service_edge = jsonrpclib.Server(conf['SERVICE_EDGE_URL']) # SOTA Startup if conf['SOTA_ENABLE'] == True: # log SOTA configuration rvi_logger.info('RVI Server: SOTA Configuration: ' + 'RVI_SOTA_CALLBACK_URL: ' + conf['SOTA_CALLBACK_URL'] + ', ' + 'RVI_SOTA_SERVICE_ID: ' + conf['SOTA_SERVICE_ID'] + ', ' + 'RVI_SOTA_CHUNK_SIZE: ' + str(conf['SOTA_CHUNK_SIZE'])) # start the SOTA callback server try: rvi_logger.info( 'RVI Server: Starting SOTA Callback Server on %s with service id %s.', conf['SOTA_CALLBACK_URL'], conf['SOTA_SERVICE_ID']) self.sota_cb_server = SOTACallbackServer( self.rvi_service_edge, conf['SOTA_CALLBACK_URL'], conf['SOTA_SERVICE_ID']) self.sota_cb_server.start() rvi_logger.info('RVI Server: SOTA Callback Server started.') except Exception as e: rvi_logger.error( 'RVI Server: Cannot start SOTA Callback Server: %s', e) sys.exit(1) # wait for SOTA callback server to come up time.sleep(0.5) # start SOTA Transmission Server try: rvi_logger.info( 'RVI Server: Starting SOTA Transmission Server.') self.sota_tx_server = SOTATransmissionServer( self.rvi_service_edge, conf['SOTA_SERVICE_ID'], conf['SOTA_CHUNK_SIZE']) self.sota_tx_server.start() rvi_logger.info( 'RVI Server: SOTA Transmission Server started.') except Exception as e: rvi_logger.error( 'RVI Server: Cannot start SOTA Transmission Server: %s', e) sys.exit(1) # wait for SOTA transmission server to come up time.sleep(0.5) # Tracking Startup if conf['TRACKING_ENABLE'] == True: # log Tracking configuration rvi_logger.info('RVI Server: Tracking Configuration: ' + 'RVI_TRACKING_CALLBACK_URL: ' + conf['TRACKING_CALLBACK_URL'] + ', ' + 'RVI_TRACKING_SERVICE_ID: ' + conf['TRACKING_SERVICE_ID']) # start the Tracking callback server try: rvi_logger.info( 'RVI Server: Starting Tracking Callback Server on %s with service id %s.', conf['TRACKING_CALLBACK_URL'], conf['TRACKING_SERVICE_ID']) self.tracking_cb_server = TrackingCallbackServer( self.rvi_service_edge, conf['TRACKING_CALLBACK_URL'], conf['TRACKING_SERVICE_ID']) self.tracking_cb_server.start() rvi_logger.info( 'RVI Server: Tracking Callback Server started.') except Exception as e: rvi_logger.error( 'RVI Server: Cannot start Tracking Callback Server: %s', e) sys.exit(1) # wait for SOTA callback server to come up time.sleep(0.5) else: rvi_logger.info('RVI Server: Tracking not enabled') # Publish to Kafka Message Queue if conf['TRACKING_MQ_PUBLISH'] == True: #log kafka configuration rvi_logger.info('RVI Server: Publishing to Kafka Message Queue: ' + conf['TRACKING_MQ_URL'] + ' , with topic: ' + conf['TRACKING_MQ_TOPIC']) #Start the Kafka message queue forwarding server try: rvi_logger.info('%s: Publishing to message queue enabled.', self.__class__.__name__) self.mq_sink_server = MQSinkServer( self.rvi_service_edge, conf['TRACKING_CALLBACK_URL'], conf['TRACKING_SERVICE_ID']) self.mq_sink_server.start() rvi_logger.info('RVI Server: Message Queue Server started.') except Exception as e: rvi_logger.error( 'RVI Server: Cannot start Message Queue Server: %s', e) sys.exit(1) else: rvi_logger.info('RVI Server: MQ Publish not enabled') # Save message Queue contents into HBase if conf['TRACKING_MQ_HBASE'] == True: rvi_logger.info('RVI Server: Saving to HBase: ' + conf['TRACKING_MQ_HBASE_URL']) #Start HBase Server thread try: rvi_logger.info('%s: Saving messages to HBase enabled.', self.__class__.__name__) self.hbase_server = HBaseServer( conf['TRACKING_MQ_URL'], conf['TRACKING_MQ_TOPIC'], conf['TRACKING_MQ_HBASE_URL'], conf['TRACKING_MQ_HBASE_PORT'], conf['TRACKING_MQ_HBASE_TABLE']) self.hbase_server.start() rvi_logger.info('RVI Server: Kafka -> HBase consumer started.') except Exception as e: rvi_logger.error('RVI Server: Cannot start HBase Server: %s', e) sys.exit(1) else: rvi_logger.info('RVI Server: HBase server storage not enabled') # catch signals for proper shutdown for sig in (SIGABRT, SIGTERM, SIGINT): signal(sig, self.cleanup) # main execution loop timeout = conf['DB_CLOSE_TIMEOUT'] while True: try: time.sleep(conf['DB_PING_INTERVAL']) # If we are idle too long the database server may # close the connection on us, ping the server to check if # the connection is still up. if (connection.connection is not None): if (connection.is_usable() == True): rvi_logger.debug( 'RVI Server: Database connection is up.') # Close connection if open longer than the timeout timeout -= conf['DB_PING_INTERVAL'] if (timeout <= 0): connection.close() timeout = conf['DB_CLOSE_TIMEOUT'] rvi_logger.info( 'RVI Server: Idle Timeout: closed database connection.' ) else: rvi_logger.error( 'RVI Server: Database connection is down.') connection.close() elif (conf['TRACKING_MQ_PUBLISH'] == True and conf['TRACKING_ENABLE'] == False): pass else: rvi_logger.error( 'RVI Server: Database connection is closed.') # As long as the connection is closed reset the timeout timeout = conf['DB_CLOSE_TIMEOUT'] except KeyboardInterrupt: print('\n') break