Example #1
0
def serve_loan():
    """

    :return:
    """
    #params = request.form
    fm_client = FraudMetrixClient(app.config['FRAUD_METRIX_PARTNER_CODE'],
                                  app.config['FRAUD_METRIX_SECRET_KEY'], app.config['FRAUD_METRIX_PARTNER_KEY'])
    work_dict_1 = {
            'tongdun_interface':'loan',
            'user_id':123,
            'order_id': 'sdfg-fgh-from-flask',
            'content': 'demo'
        }
    mc = MnsClient(MNS_CFG)
    send_id = mc.send_msg(json.dumps(work_dict_1))
    g.mns_id = send_id
    return fm_client.invoke_loan_sync(1,2,json.dumps(work_dict_1))
Example #2
0
 def init_from_cfg(self):
     parser = ConfigParser.ConfigParser()
     parser.read(self.srv_cfg_file)
     for sec,op in required_ops:
         if not parser.has_option(sec, op):
             logger.error("ERROR: need (%s, %s) in %s.\n" % (sec,op,self.srv_cfg_file))
             sys.exit(1)
     self.pause_file = parser.get(BASE, PAUSE_FILE)
     try:
         self.mns_consume_client = MnsClient(self.mns_consume_cfg)
     except Exception as e:
         logger.error("load mns client failed due to %s" %e)
         sys.exit(1)
Example #3
0
class BasicService():
    """Basic Service
    """
    __metaclass__ = ABCMeta

    def __init__(self, srv_cfg_file, mns_consume_cfg):
        self.srv_cfg_file = srv_cfg_file
        self.mns_consume_cfg = mns_consume_cfg
        self.mns_consume_client = None
        self.consumer_result = False
        self.init_from_cfg()


    def init_from_cfg(self):
        parser = ConfigParser.ConfigParser()
        parser.read(self.srv_cfg_file)
        for sec,op in required_ops:
            if not parser.has_option(sec, op):
                logger.error("ERROR: need (%s, %s) in %s.\n" % (sec,op,self.srv_cfg_file))
                sys.exit(1)
        self.pause_file = parser.get(BASE, PAUSE_FILE)
        try:
            self.mns_consume_client = MnsClient(self.mns_consume_cfg)
        except Exception as e:
            logger.error("load mns client failed due to %s" %e)
            sys.exit(1)

    @abstractmethod
    def consume(self, work):
        """
        @param work means the msg body you recv from mns
        @return: True if you consume msg successfully, other wise, return False
        never never use sys.exit in the consume method !!!
        """
        pass

    def if_pause(self):
        """
        judge if there is pause file
        @return:
        """
        return os.path.exists(self.pause_file)


    def pre_consume(self):
        """
        @return: True or False
        """
        return self.mns_consume_client.recv_msg()

    def post_consume(self):
        """
        check consume result and delete msg from mns or not, and some other staff
        @return:
        """
        if self.consumer_result is True:
            #logger.debug("consume return True")
            self.mns_consume_client.del_msg()

    @contextmanager
    def consume_wrapper(self):
        """

        @return:
        """
        msg_body = self.pre_consume()
        yield  msg_body
        self.post_consume()

    def run(self):
        while True:
            if self.if_pause():
                time.sleep(self.wait_seconds)
                continue
            with self.consume_wrapper() as msg_body:
                if  msg_body is None:
                    continue
                self.consumer_result = self.consume(msg_body)