def run_application(options, instance_name): input_log_file_handler = logging.handlers.TimedRotatingFileHandler( os.path.expanduser(options.mailer_log), when='MIDNIGHT') input_log_file_handler.setFormatter(logging.Formatter(u"%(asctime)s - %(message)s")) app_logger = logging.getLogger(instance_name) app_logger.setLevel(logging.INFO) app_logger.addHandler(input_log_file_handler) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.DEBUG) ch.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')) app_logger.addHandler(ch) app_logger.info('START') def log(command, key, value=None): log_msg = u'%s, %s, %s' %(command, key, value if value else None) app_logger.info(unicode(log_msg)) log('PARAM', 'BEGIN') log('PARAM', 'trade_in', options.trade_in) log('PARAM', 'trade_pub', options.trade_pub) log('PARAM', 'app_log', options.app_log) log('PARAM', 'maxmind_license_key', options.maxmind_license_key) log('PARAM', 'END') context = zmq.Context() socket = context.socket(zmq.SUB) socket.connect(options.trade_pub) socket.setsockopt(zmq.SUBSCRIBE, "^FRAUDER$") trade_in_socket = context.socket(zmq.REQ) trade_in_socket.connect(options.trade_in) application_trade_client = TradeClient( context, trade_in_socket) application_trade_client.connect() login_response = application_trade_client.sendJSON( MessageBuilder.login( 8999999, options.frauder_username, options.frauder_password)) if login_response.get('UserStatus') != 1: raise RuntimeError("Invalid user id") brokers = {} broker_list, broker_list_columns = application_trade_client.getBrokerList(['1']) for b in broker_list: brokers[b[0]] = { "params": b } while True: try: raw_email_message = socket.recv() log('IN', 'TRADE_IN_PUB', raw_email_message) msg = JsonMessage(raw_email_message) if not msg.isAccessLog(): continue try: broker_id = msg.get('BrokerID') except Exception as ex: traceback.print_exc() log('ERROR', 'EXCEPTION', str(ex)) time.sleep(1) except KeyboardInterrupt: app_logger.info('END') break except Exception as ex: time.sleep(1)