Пример #1
0
    def create_driver(self, platform, driver_type):
        """
        根据平台和类型创建driver

        :param str platform: 平台名
        :param str driver_type: driver类型
        :return: driver对象
        :rtype: BaseDriver
        :raises ImportError: 模块导入异常
        """
        driver_module = StringUtil.camel_to_underline(driver_type) + '_driver'
        concrete_driver_module = platform.lower() + '_' + driver_module
        full_driver_path = 'opal.driver.' + driver_module + \
                           '.' + concrete_driver_module
        # 动态导入 Driver 模块
        log.info('create driver full path:{}'.format(full_driver_path))
        try:
            module_name = __import__(full_driver_path,
                                     fromlist=[concrete_driver_module])
        except ImportError as e:
            log.error('driver: {} of the platform:{} is not '
                      'exist'.format(driver_type, platform))
            raise e
        driver_class_name = StringUtil.underline_to_camel(
            concrete_driver_module.title())
        driver_class = getattr(module_name, driver_class_name)
        driver_obj = driver_class()
        log.info('create driver class:{}'.format(driver_class_name))
        return driver_obj
Пример #2
0
    def on_error(self, header, message):
        """
        响应错误,本处只打印日志,便于追查问题

        :param dict header: 消息头
        :param dict message: 消息体
        :return: 无返回
        :rtype: None
        """
        log.error("receive an error:{}".format(message))
Пример #3
0
 def __run(self):
     """
     http server进程
     发生异常会记录日志
     :raises: Exception: 运行中可能抛出的异常
     """
     try:
         handler = RequestHandler
         SocketServer.TCPServer.allow_reuse_address = True
         _server = SocketServer.TCPServer(('', self._port), handler)
         _server.serve_forever()
     except Exception:
         log.error('Generic Exception' + traceback.format_exc())
Пример #4
0
    def active(self):
        """
        感知器生效函数。该函数创建新线程,定期进行外部事件拉取操作

        :return: 无返回
        :rtype: None
        :raises ThreadError: 创建线程失败
        """
        try:
            self._pull_thread = threading.Thread(target=self.event_dealer)
            self._pull_thread.daemon = True
            self._pull_thread.start()
        except threading.ThreadError as e:
            log.error("create new thread err:{}".format(e))
            raise e
Пример #5
0
    def save_context(self):
        """
        运行数据持久化,当当前Guardian为主(lock属性为True)时,可持久化数据,否则失败

        :return: 无返回
        :rtype: None
        :raises EInvalidOperation: 非法操作
        """
        if not self.lock:
            log.error("current guardian instance no privilege to save context")
            raise exception.EInvalidOperation(
                "current guardian instance no privilege to save context")
        context_path = CONTEXT_PATH.format(self.__guardian_id)
        ZkClient().save_data(context_path, pickle.dumps(self))
        log.info("save context success")
Пример #6
0
    def record_action(self):
        """
        记录状态

        .. Note:: 为避免es服务异常导致主进程退出,此方法捕获所有异常,并打印日志

        :return: 无返回
        :rtype: None
        """
        try:
            ESClient("ark", "operation").put_data(
                self.operation_id,
                json.dumps(self, default=lambda obj: obj.__dict__))
        except Exception as e:
            log.error("record operation err:{}".format(e))
        else:
            log.info("record action success, operation_id:{}".format(
                self.operation_id))
Пример #7
0
    def _do_request(self, uid):
        """
        发送http请求
        发生异常会记录日志

        :raises: exception.EFailedRequest 请求失败
        :param str uid: 
        :return: None 或者 请求数据
        :rtype: None 或者str
        """

        response = None
        try:
            condition = {"uid": uid}
            esc = client.ESClient("ark", "operation")
            response = esc.get_data_with_condition(condition)
        except exception.EFailedRequest as e:
            log.error(str(e.reason))
        finally:
            return response
Пример #8
0
    def http_request(self, host, port, method, url, header=None, data=None,
                     timeout=30,retry_times=2, response_code=None,
                     response_json=True):
        """
        http请求接口

        :param str host: 服务器地址
        :param int port: 服务器端口
        :param str method: http方法
        :param str url: url地址
        :param dict header: http消息头
        :param str data: http body数据
        :param int timeout: 请求超时时间
        :param int retry_times: 请求重试次数
        :param list response_code: 请求正确时的返回码
        :param bool response_json: True 返回json格式的数据,False 返回字符串
        :return: http 请求的数据
        :rtype: str
        :raises EFailedRequest: 请求失败
        """
        log.debug("http request, host:{}, port:{}, method:{}, url:{}, header:"
                  "{}, data:{}, timeout:{}, retry_times:{}, response code:{}, "
                  "response_json:{}".format(host, port, method, url, header,
                                            data, timeout, retry_times,
                                            response_code, response_json))
        header = header or {}
        res_data = None
        for i in range(retry_times + 1):
            stop_tag = True if i == retry_times else False
            sleep_time = (i + 1) * (i + 1)
            try:
                conn = httplib.HTTPConnection(
                    host=host, port=port, timeout=timeout)
                conn.request(method=method, url=url, body=data, headers=header)
                resp = conn.getresponse()
                res_data = resp.read()
                log.info("http request ret: %s" % res_data)
            except Exception as e:
                log.info("http request exe: %s" % e)
                if stop_tag:
                    log.error("http request failed,error:{}".format(e))
                    raise exception.EFailedRequest(
                        "http request failed,error:{}".format(e))
                else:
                    time.sleep(sleep_time)
                    continue
            else:
                log.debug("http request ok")
                if not response_code or not isinstance(response_code, list):
                    if 200 <= resp.status < 300:
                        break
                    elif stop_tag:
                        log.error("request failed,code:{},msg:{}".format(
                                resp.status, resp.msg))
                        raise exception.EFailedRequest(
                            "request failed,code:{},msg:{}".format(
                                resp.status, resp.msg))
                    else:
                        time.sleep((i + 1) * (i + 1))
                        continue
                else:
                    if resp.status in response_code:
                        break
                    elif stop_tag:
                        log.error("request failed,error,code:{},data:{}".format(
                                resp.status, data))
                        raise exception.EFailedRequest(
                            "request failed,error,code:{},data:{}".format(
                                resp.status, data))
                    else:
                        time.sleep(sleep_time)
                        continue
        log.debug("http response data:{}".format(res_data))
        if response_json:
            return json.loads(res_data)
        else:
            return res_data