Esempio n. 1
0
        def _request_log(response, *args, **kws):
            now = time.time()
            request_start_time = getattr(request, "request_start_time", None)
            real_ip = request.headers.get("X-Real-Ip", request.remote_addr)

            _response = response.get_json(silent=True)
            code = _response.get(
                "code",
                CONST.BIZ_CODE_FAIL) if _response else CONST.BIZ_CODE_FAIL

            # 预留心跳检测
            if request.endpoint != "HEARTBEAT_ENDPOINT":
                data = dict(
                    request_time=str(
                        time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())),
                    remote_addr=real_ip,
                    status_code=response.status_code,
                    code=code,
                    method=request.method,
                    url=request.url,
                    endpoint=request.endpoint,
                    args=request.args.to_dict(),
                    form=request.form.to_dict(),
                    json=request.json,
                    response=_response,
                    elapsed=now -
                    request_start_time if request_start_time else None,
                )

                logger = logging.getLogger("request")
                logger.info(data)

            return response
    def get(self):
        logger.info("enter into index api.request method is {}".format(
            request.method))
        mysql_wrapper = GLOBAL.get_mysql_wrapper()
        with mysql_wrapper.session_scope() as session:
            session.add(UserInfo('10002', 'usa', '0002'))

            # session.add_all([UserInfo('10001','us','0001')])
        return biz_success()
Esempio n. 3
0
def upset_data(table_obj, filter_data, update_data, mysql_wrapper=None):
    if not mysql_wrapper:
        mysql_wrapper = GLOBAL.get_mysql_wrapper()
    with mysql_wrapper.session_scope() as session:

        query_result = session.query(table_obj).filter(*filter_data).first()
        if query_result:
            session.query(table_obj).filter(*filter_data).update(update_data)
            logger.info("success update one.update={}".format(update_data))
        else:
            session.add(table_obj(**update_data))
            logger.info("success add one.update_data={}".format(update_data))
Esempio n. 4
0
    def connect_etcd_client(self):
        while True:
            try:
                self.etcd_client = etcd.Client(host=self.etcd_host,
                                               port=self.etcd_port,
                                               allow_reconnect=True,
                                               read_timeout=10)

                logger.info('Connected to ETCD cluster: {}:{}'.format(
                    self.etcd_host, self.etcd_port))
                return self.etcd_client
            except etcd.EtcdException:
                time.sleep(CONST.ETCD_RECONNECT_WAIT_TIME)
                logger.info('reconnect...')
                continue
Esempio n. 5
0
    def get(self):
        logger.info("enter into index api.request method is {}".format(
            request.method))
        SETTING = GLOBAL.get_settings()
        logger.info("listen port is {}".format(SETTING.LISTEN_PORT))
        logger.info('enter into index view function')

        test_is_change = SETTING.TEST_IS_CHANGE
        logger.info("test_is_change={}".format(test_is_change))

        data = {"demo": 'demo1'}
        return biz_success(data)
Esempio n. 6
0
    def connect_etcd_cluster(self, connect_leader=False):
        while True:
            try:
                self.etcd_client = etcd.Client(host=self.etcd_host,
                                               port=self.etcd_port,
                                               allow_reconnect=True,
                                               read_timeout=30)

                logger.info('Connected to ETCD cluster: {}:{}'.format(
                    self.etcd_host, self.etcd_port))

                if connect_leader:
                    leader = self.etcd_client.leader['clientURLs']
                    logger.debug('leader: {}'.format(leader))
                    if leader is None:
                        return

                    leader = leader[0].split(':')[1][2:]
                    logger.info('reconnect to leader: {}'.format(leader))
                    self.etcd_client = etcd.Client(host=leader,
                                                   port=self.etcd_port,
                                                   allow_reconnect=True,
                                                   read_timeout=10)

            except etcd.EtcdException:
                time.sleep(CONST.ETCD_RECONNECT_WAIT_TIME)
                logger.info('reconnect...')
                continue

            break
Esempio n. 7
0
    def select_config(self):
        settings_server = SettingsFileWrapper()
        if not self.__config_dict:
            raise Exception("load config failed! not config data")

        config_method = os.environ.get('debug_method')
        if config_method == 1:
            logger.info('use LOCAL config')
            settings_server.generate_settings_file(self.__config_dict)
            GLOBAL.set_settings(type("SETTING", (), self.__config_dict))
        else:
            config_method = self.__yaml_dict.get(CONST.SYSTEM_NAME).get(
                CONST.SUB_SYSTEM).get(CONST.DEBUG_METHOD)
            if config_method == 1:
                logger.info('use LOCAL config')
                settings_server.generate_settings_file(self.__config_dict)
                GLOBAL.set_settings(type("SETTING", (), self.__config_dict))
            else:
                logger.info('load ETCD config')
                etcd_wrapper_server = EtcdWrapper(
                    host=os.environ.get('ETCD_SERVICE_SERVICE_HOST',
                                        '127.0.0.1'),
                    port=int(
                        os.environ.get('ETCD_SERVICE_SERVICE_PORT', '2379')))
                # etcd_wrapper.connect_etcd_cluster()
                etcd_wrapper_server.connect_etcd_client()

                self.reset_etcd_config(etcd_wrapper_server)

                config_dict = etcd_wrapper_server.read_etcd_config_data()
                settings_server.generate_settings_file(config_dict)
                GLOBAL.set_settings(type("SETTING", (), config_dict))

                t = threading.Thread(target=self.load_config_by_etcd,
                                     args=(settings_server, ),
                                     name="read_etcd_loop")
                t.start()
Esempio n. 8
0
 def aft_request(response):
     logger.info("enter into after request")
     _request_log(response)
     return response
Esempio n. 9
0
 def bf_request():
     logger.info("enter into before request")
Esempio n. 10
0
 def bf_first_request():
     logger.info("into before first request")
Esempio n. 11
0
 def create_tables(self, Base):
     try:
         logger.info('creating sql tables.')
         Base.metadata.create_all(self.engine)
     except Exception as e:
         logger.error('create tables failed {}'.format(str(e)))