예제 #1
0
    async def run(self, uuid):
        redis.incr("exec_sum")
        redis.incr(uuid + "&&exec_sum")

        workflow_info = Workflow.select(
            'uuid',
            'name',
            'remarks',
            'start_app',
            'end_app',
            'input_app',
            'webhook_app',
            'timer_app',
            'flow_json',
            'flow_data',
            'controller_data',
            'local_var_data',
            'status'
        ).where(
            "uuid", uuid
        ).first()

        if workflow_info:
            if str(workflow_info.status) == "1":
                return False

            self.workflow_name = workflow_info.name
            self.workflow_remarks = workflow_info.remarks
            self.start_app = workflow_info.start_app
            self.end_app = workflow_info.end_app
            self.input_app = workflow_info.input_app
            self.webhook_app = workflow_info.webhook_app
            self.timer_app = workflow_info.timer_app
            self.flow_json = json.loads(workflow_info.flow_json)
            self.flow_data = json.loads(workflow_info.flow_data)

            await self.make_var(workflow_info.local_var_data, workflow_info.controller_data)

            target_app = await self.find_start_app(edges=self.flow_json["edges"], start_app=self.start_app)

            await self.add_execute_logs(uuid=uuid, app_uuid=self.start_app, app_name="开始", result="剧本开始执行", status=0,
                                        html="<span>剧本开始执行</span>")

            is_while = True

            while is_while:
                try:
                    edge_name, source_app, next_app, is_switch, edge_action, edge_if_else = await self.find_next_app(
                        edges=self.flow_json["edges"],
                        next_app=target_app
                    )

                except Exception as e:
                    await self.add_execute_logs(uuid=uuid, app_uuid="", app_name="", result="当前剧本不具有可执行条件", status=1,
                                                html="<span>当前剧本不具有可执行条件</span>")
                    is_while = False
                    break

                key = target_app + "&&" + self.only_id + "&&sum"
                if redis.exists(key) == 1:
                    sum = redis.get(key)
                    redis.set(key, int(sum) + 1, ex=60 * 1)
                else:
                    redis.set(key, 1, ex=60 * 1)

                if self.input_app == source_app or self.webhook_app == source_app or self.timer_app == source_app:
                    is_status, if_else_result = await self.get_app_data(uuid=uuid, app_uuid=source_app)
                else:
                    source_info = self.flow_data[source_app]
                    is_status, if_else_result = await self.get_app_data(uuid=uuid, app_uuid=source_app,
                                                                        app_info=source_info)

                if is_status == 0:
                    if is_switch:
                        if str(edge_action) == "1":
                            is_arr = re.findall(r'\[\w*.+\]', edge_if_else)
                            if len(is_arr) > 0:
                                edge_if_else_arr = str(is_arr[0]).replace("[", "").replace("]", "").split(",")
                                if if_else_result in edge_if_else_arr:
                                    target_app = next_app
                                else:
                                    pass
                            else:
                                if edge_if_else == if_else_result:
                                    target_app = next_app
                                else:
                                    pass
                        elif str(edge_action) == "2":
                            is_arr = re.findall(r'\[\w*.+\]', edge_if_else)
                            if len(is_arr) > 0:
                                edge_if_else_arr = str(is_arr[0]).replace("[", "").replace("]", "").split(",")
                                if if_else_result not in edge_if_else_arr:
                                    target_app = next_app
                                else:
                                    pass
                            else:
                                if edge_if_else != if_else_result:
                                    target_app = next_app
                                else:
                                    pass
                        elif str(edge_action) == "3":
                            is_arr = re.findall(r'{0}'.format(edge_if_else), if_else_result)
                            if len(is_arr) > 0:
                                target_app = next_app
                            else:
                                pass
                    else:
                        target_app = next_app
                else:
                    is_while = False

                if next_app == self.end_app:
                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束", result="剧本执行结束",
                                                status=0, html="<span>剧本执行结束</span>")
                    await self.add_report()
                    is_while = False

                    if int(redis.decr("exec_sum")) < 0:
                        redis.set("exec_sum", "0")

                    if int(redis.decr(uuid + "&&exec_sum")) < 0:
                        redis.set(uuid + "&&exec_sum", "0")

                    break
예제 #2
0
    async def run(self, uuid):
        redis.incr("exec_sum")
        redis.incr(uuid + "&&exec_sum")

        workflow_info = Workflow.select(
            'uuid',
            'name',
            'remarks',
            'start_app',
            'end_app',
            'input_app',
            'webhook_app',
            'timer_app',
            'flow_json',
            'flow_data',
            'controller_data',
            'local_var_data',
            'status'
        ).where(
            "uuid", uuid
        ).first()

        if workflow_info:
            if str(workflow_info.status) == "1":
                return False

            self.workflow_name = workflow_info.name
            self.workflow_remarks = workflow_info.remarks
            self.start_app = workflow_info.start_app
            self.end_app = workflow_info.end_app
            self.input_app = workflow_info.input_app
            self.webhook_app = workflow_info.webhook_app
            self.timer_app = workflow_info.timer_app
            self.flow_json = json.loads(workflow_info.flow_json)
            self.flow_data = json.loads(workflow_info.flow_data)

            await self.make_var(workflow_info.local_var_data, workflow_info.controller_data)

            target_app = await self.find_start_app(edges=self.flow_json["edges"], start_app=self.start_app)

            await self.add_execute_logs(uuid=uuid, app_uuid=self.start_app, app_name="开始", result="剧本开始执行", status=0,
                                        html="<span>剧本开始执行</span>")
            is_while = True

            while is_while:
                try:
                    edge_name, source_app, next_app, is_switch, edge_action, edge_if_else = await self.find_next_app(
                        edges=self.flow_json["edges"],
                        next_app=target_app
                    )
                except Exception as e:
                    await self.add_execute_logs(uuid=uuid, app_uuid="", app_name="", result="当前剧本不具有可执行条件", status=1,
                                                html="<span>当前剧本不具有可执行条件</span>")

                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束",
                                                result="剧本执行结束",
                                                status=0, html="<span>剧本执行结束</span>")

                    await self.decr_sum(uuid=uuid)
                    is_while = False
                    break

                key = target_app + "&&" + self.only_id + "&&sum"
                if redis.exists(key) == 1:
                    sum = redis.get(key)
                    redis.set(key, int(sum) + 1, ex=lose_time)
                else:
                    redis.set(key, 1, ex=lose_time)

                if self.input_app == source_app or self.webhook_app == source_app or self.timer_app == source_app:
                    is_status, if_else_result = await self.get_app_data(uuid=uuid, app_uuid=source_app)
                else:
                    source_info = self.flow_data[source_app]
                    is_status, if_else_result = await self.get_app_data(uuid=uuid, app_uuid=source_app,
                                                                        app_info=source_info)

                if str(is_status) == "0":
                    if is_switch:
                        if str(edge_action) == "1":
                            is_arr = re.findall(r'\[\w*.+\]', edge_if_else)
                            if len(is_arr) > 0:
                                edge_if_else_arr = str(is_arr[0]).replace("[", "").replace("]", "").split(",")
                                if if_else_result in edge_if_else_arr:
                                    target_app = next_app
                                else:
                                    pass
                            else:
                                if str(edge_if_else) == str(if_else_result):
                                    target_app = next_app
                                else:
                                    pass
                        elif str(edge_action) == "2":
                            is_arr = re.findall(r'\[\w*.+\]', edge_if_else)
                            if len(is_arr) > 0:
                                edge_if_else_arr = str(is_arr[0]).replace("[", "").replace("]", "").split(",")
                                if if_else_result not in edge_if_else_arr:
                                    target_app = next_app
                                else:
                                    pass
                            else:
                                if str(edge_if_else) != str(if_else_result):
                                    target_app = next_app
                                else:
                                    pass
                        elif str(edge_action) == "3":
                            is_arr = re.findall(r'{0}'.format(edge_if_else), if_else_result)
                            if len(is_arr) > 0:
                                target_app = next_app
                            else:
                                pass
                        elif str(edge_action) == "4":
                            is_json_arr = re.findall(r'\{\w.*\}!=\w.*', edge_if_else)
                            is_json_arr_equal = re.findall(r'\{\w.*\}=\w.*', edge_if_else)

                            if len(is_json_arr) == 1:
                                is_equal = False
                                is_json_arr = is_json_arr[0].split("!=")
                                json_key = str(is_json_arr[0])
                                json_val = str(is_json_arr[1])
                            else:
                                is_equal = True
                                is_json_arr = is_json_arr_equal[0].split("=")
                                json_key = str(is_json_arr[0])
                                json_val = str(is_json_arr[1])

                            if len(is_json_arr) > 0:
                                edge_if_else_key = json_key.replace("{", "").replace("}", "")
                                edge_if_else_arr = edge_if_else_key.split(".")

                                is_json = self.is_json(json_text=if_else_result)

                                if is_json is False:
                                    await self.add_execute_logs(
                                        uuid=uuid, app_uuid="", app_name="",
                                        result="非 JSON 格式变量",
                                        status=1,
                                        html="<span>非 JSON 格式变量</span>")

                                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束",
                                                                result="剧本执行结束",
                                                                status=0, html="<span>剧本执行结束</span>")

                                    await self.decr_sum(uuid=uuid)
                                    is_while = False
                                    break

                                try:
                                    if_else_result = json.loads(json.loads(if_else_result))
                                except:
                                    if_else_result = json.loads(if_else_result)

                                key_all = "if_else_result"
                                key_front = "['"
                                key_after = "']"
                                key_number_front = "["
                                key_number_after = "]"

                                for k in edge_if_else_arr:
                                    try:
                                        int(k)
                                        key_all += key_number_front + k + key_number_after
                                    except ValueError:
                                        k = str(k).replace("!!!", "")
                                        key_all += key_front + k + key_after
                                try:
                                    if_else_result = eval(key_all)
                                    if is_equal:
                                        if str(if_else_result) == json_val:
                                            target_app = next_app
                                        else:
                                            pass
                                    else:
                                        if str(if_else_result) != json_val:
                                            target_app = next_app
                                        else:
                                            pass
                                except IndexError:
                                    await self.add_execute_logs(
                                        uuid=uuid, app_uuid="", app_name="",
                                        result="未找到 JSON Index : {key}".format(key=edge_if_else_key),
                                        status=1,
                                        html="<span>未找到 JSON Index : {key}</span>".format(key=edge_if_else_key))

                                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束",
                                                                result="剧本执行结束",
                                                                status=0, html="<span>剧本执行结束</span>")

                                    await self.decr_sum(uuid=uuid)
                                    is_while = False
                                    break
                                except KeyError:
                                    await self.add_execute_logs(
                                        uuid=uuid, app_uuid="", app_name="",
                                        result="未找到 JSON KEY : {key}".format(key=edge_if_else_key),
                                        status=1,
                                        html="<span>未找到 JSON KEY : {key}</span>".format(key=edge_if_else_key))

                                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束",
                                                                result="剧本执行结束",
                                                                status=0, html="<span>剧本执行结束</span>")

                                    await self.decr_sum(uuid=uuid)
                                    is_while = False
                                    break
                                except TypeError:
                                    await self.add_execute_logs(
                                        uuid=uuid, app_uuid="", app_name="",
                                        result="JSON 格式不存在 : {key}".format(key=edge_if_else_key),
                                        status=1,
                                        html="<span>JSON 格式不存在 : {key}</span>".format(key=edge_if_else_key))

                                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束",
                                                                result="剧本执行结束",
                                                                status=0, html="<span>剧本执行结束</span>")

                                    await self.decr_sum(uuid=uuid)
                                    is_while = False
                                    break
                    else:
                        target_app = next_app
                else:
                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束", result="剧本执行结束",
                                                status=0, html="<span>剧本执行结束</span>")
                    await self.decr_sum(uuid=uuid)
                    is_while = False
                    break

                if next_app == self.end_app:
                    await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束", result="剧本执行结束",
                                                status=0, html="<span>剧本执行结束</span>")
                    await self.add_report()
                    await self.decr_sum(uuid=uuid)
                    # redis.delete(*redis.keys(pattern='*{key}*'.format(key=self.only_id)))
                    is_while = False
                    break