Пример #1
0
    def __init__(self, job_adapter, stage_builder=None, process_count=1):
        """
        初始化方法

        :param JobAdapter job_adapter: 任务处理器,分级操作解析器与任务处理器绑定,完成分级任务的具体执行操作
        :param StageBuilder stage_builder: 分级操作构建器
        :param int process_count: 最大进程数
        """
        if not isinstance(job_adapter, stage.JobAdapter):
            raise exception.ETypeMismatch(
                "job_adapter is not instance of "
                "JobAdapter, type:{}", format(type(job_adapter)))
        if not isinstance(stage_builder, stage.StageBuilder):
            raise exception.ETypeMismatch("stage_builder is not instance of "
                                          "StageBuilder,type:{}".format(
                                              type(stage_builder)))
        super(StageStateMachineExecutor, self).__init__(None, process_count)
        self._stage_builder = stage_builder or stage.StageBuilder()
        self._bind_job_type(job_adapter)
Пример #2
0
    def validate_listeners(self):
        """
        验证消息处理器是否符合要求。通常,我们认为一个Guardian包含的消息处理器数量应该为:
        至少一个感知器、一个决策器、一个执行器。

        :return: 无返回
        :rtype: None
        :raises ETypeMismatch: 消息处理器验证失败
        """
        sensor_count, decision_count, executor_count = 0, 0, 0
        for listener in self._listener_list:
            if isinstance(listener, BaseSensor):
                sensor_count += 1
            elif isinstance(listener, BaseDecisionMaker):
                decision_count += 1
            elif isinstance(listener, BaseExecutor):
                executor_count += 1
            else:
                raise exception.ETypeMismatch("listener type is not match")
        if sensor_count < 1 or decision_count != 1 or executor_count != 1:
            raise exception.ETypeMismatch("listener must be: one decision, "
                                          "one executor, at least one sensor")
Пример #3
0
    def add_node(self, node):
        """
        增加节点

        :param Node  node: 节点
        :return: None
        :raises ETypeMismatch: 节点类型错误
        :raises EInvalidOperation: 操作不合法
        """
        if not isinstance(node, Node):
            raise exception.ETypeMismatch("param node type must be Node")
        elif node in self._nodes:
            raise exception.EInvalidOperation(
                "node {} already added".format(node.name))
        else:
            self._nodes.append(node)
            self._nodes_process[node.name] = False
Пример #4
0
    def __init__(self, process_count=1):
        """
        初始化方法

        :param int process_count: 进程数
        :return: 无返回
        :rtype: None
        :raises ETypeMismatch: 参数类型不匹配
        """
        if not isinstance(process_count, int) or process_count < 1 \
                or process_count > 1000:
            raise exception.ETypeMismatch(
                "param process_count must be 1-1000 integer")
        manager = multiprocessing.Manager()
        self._result_queue = manager.Queue()
        self._concerned_message_list = ["IDLE_MESSAGE", "DECIDED_MESSAGE"]
        self._todo_message_queue = []
        self._process_pool = multiprocessing.Pool(processes=process_count)
Пример #5
0
    def decision_logic(self, message):
        """
        具体决策逻辑

        :param Message message: 消息对象
        :return: 决策完成的消息
        :rtype: Message
        :raises ETypeMismatch: 事件参数不匹配异常
        """
        log.info("begin decision logic, message:{}".format(message.name))
        operation_id = message.operation_id
        params = message.params
        if self._from_key in params \
                and params[self._from_key] in self._mapping:
            params_cp = copy.deepcopy(params)
            params_cp[self._to_key] = self._mapping[params_cp[self._from_key]]
            decided_message = framework.OperationMessage(
                "DECIDED_MESSAGE", operation_id, params_cp)
            return decided_message
        else:
            raise exception.ETypeMismatch(
                "{} not in params or params[{}] not in "
                "mapping".format(self._from_key, self._from_key))
Пример #6
0
    def on_message(self, message):
        """
        触发执行并返回结果,``on_execute_message`` 可能为同步或异步操作,
        若为异步操作,则返回结果并非本次执行的结果,
        需要根据ret中EXECUTOR_OPERATION_ID字段确定是哪个操作的结果

        :param Message message: 消息对象
        :return: 无返回
        :rtype: None
        :raises ETypeMismatch: 返回值类型不匹配
        :raises EMissingParam: 返回值缺字段
        """
        ret = self.on_execute_message(message)
        log.info("execute message return:{}".format(ret))
        if not ret:
            return
        elif not isinstance(ret, dict):
            raise exception.ETypeMismatch()
        elif EXECUTOR_OPERATION_ID not in ret:
            raise exception.EMissingParam()
        else:
            message = OperationMessage("COMPLETE_MESSAGE",
                                       ret[EXECUTOR_OPERATION_ID], ret)
            self.send(message)