def __init__(self): WorkflowSpec.__init__(self) # The first step of our workflow is to let the general confirm # the nuclear strike. #ansible_run = AnsibleRun(self, 'Ping','hostname') #self.start.connect(ansible_run) #ansible_execute = AnsibleRun(self, 'Shell', ["ping", "-t", "1", "127.0.0.1"]) #ansible_run.connect(ansible_execute) data = {'post_assign': {'name': 'Test', 'value': 'TestValues'}} # MultiInstance对当前任务进行拆分,1:要创建的任务数 multi_inst = MultiInstance(self, 'ansible_exec', 1) self.start.connect(multi_inst) #AnsibleRun为任务规范,引用工作流规范,给定任务规范名称 ansible_run = AnsibleRun(self, 'Ping', 'yes') ansible_execute = AnsibleRun(self, 'Shell', "no") # TaskSpec,将给定对任务作为输出任务添加 multi_inst.connect(ansible_run) multi_inst.connect(ansible_execute) # 同步之前分割对任务,使用MultiInstance多实例模式时,join可以跨所有实例工作,在使用ThreadSplit时join将忽略来自另一个线程的实例。 synch_1 = Join(self, 'synch_1') #self.start.connect(synch_1) ansible_run.connect(synch_1) ansible_execute.connect(synch_1) #gate_test = Gate(self,'gate1','synch_1') #synch_1.connect(gate_test) # 实现具有一个或多个输入,和任意数量输出的任务。 # 如果连接了多个输入,则任务执行隐式多重合并。 # 如果连接了多个输出,则任务执行隐式并行分割。 end = Simple(self, 'End') end2 = Simple(self, 'End2') # 表示一个if条件,其中多个条件可能同时匹配,从而创建多个传出分支。此任务有一个或多个输入,以及一个或多个传入分支。 multichoice = MultiChoice(self, 'multi_choice_1') synch_1.connect(multichoice) cond = Equal(Attrib('Result'), 'yes') multichoice.connect_if(cond, end) cond = Equal(Attrib('Result'), 'no') multichoice.connect_if(cond, end2)
def create_instance(self): if 'testtask' in self.wf_spec.task_specs: del self.wf_spec.task_specs['testtask'] return Join(self.wf_spec, 'testtask', description='foo')
def buildNodelist(self): task=None for node in self.nodeList: nodeId=str(node['id']) nodeName=node['name'] nodeType=node['type'] try: task=self.get_task_spec_from_name(nodeName) except: if nodeType=='StartTask': self.start.description=nodeId task = self.start elif nodeType=='MultiInstance': task = MultiInstance(self, nodeName, 1,description=nodeId) elif nodeType=='playbook': file_path = node['file_path'] task=taskSpec(self,nodeName,description=nodeId,node_type=nodeType,file_path=file_path) elif nodeType=='join': task=Join(self,nodeName,description=nodeId) elif nodeType == 'end': task = Simple(self, nodeName,description=nodeId) else: task = Simple(self, nodeName,description=nodeId) finally: print(nodeId,nodeName) self.task_list[nodeId]=task return task
def __init__(self, parent, name, split_task, **kwargs): """ Constructor. :type parent: L{SpiffWorkflow.specs.WorkflowSpec} :param parent: A reference to the parent (usually a workflow). :type name: string :param name: A name for the task. :type split_task: str :param split_task: The name of the task spec that was previously used to split the branch. :type kwargs: dict :param kwargs: See L{SpiffWorkflow.specs.Join}. """ assert split_task is not None Join.__init__(self, parent, name, split_task, **kwargs)
def testValidate(self): """ Tests that we can detect when two wait taks are waiting on each other. """ task1 = Join(self.wf_spec, 'First') self.wf_spec.start.connect(task1) task2 = Join(self.wf_spec, 'Second') task1.connect(task2) task2.follow(task1) task1.follow(task2) results = self.wf_spec.validate() self.assertIn("Found loop with 'Second': Second->First then 'Second' " "again", results) self.assertIn("Found loop with 'First': First->Second then 'First' " "again", results)
def testAncestors(self): T1 = Simple(self.wf_spec, 'T1') T2A = Simple(self.wf_spec, 'T2A') T2B = Simple(self.wf_spec, 'T2B') M = Join(self.wf_spec, 'M') T3 = Simple(self.wf_spec, 'T3') T1.follow(self.wf_spec.start) T2A.follow(T1) T2B.follow(T1) T2A.connect(M) T2B.connect(M) T3.follow(M) self.assertEquals(T1.ancestors(), [self.wf_spec.start]) self.assertEquals(T2A.ancestors(), [T1, self.wf_spec.start]) self.assertEquals(T2B.ancestors(), [T1, self.wf_spec.start]) self.assertEquals(M.ancestors(), [T2A, T1, self.wf_spec.start, T2B]) self.assertEqual(len(T3.ancestors()), 5)
def test_ancestors_cyclic(self): T1 = Join(self.wf_spec, 'T1') T2 = Simple(self.wf_spec, 'T2') T1.follow(self.wf_spec.start) T2.follow(T1) T1.connect(T2) self.assertEquals(T1.ancestors(), [self.wf_spec.start]) self.assertEquals(T2.ancestors(), [T1, self.wf_spec.start])
def setup_workflow(self, structured=True, threshold=None, cancel=False): wf_spec = WorkflowSpec() split = Simple(wf_spec, 'split') wf_spec.start.connect(split) if structured: join = Join(wf_spec, 'join', threshold=threshold, split_task=split.name, cancel=cancel) else: join = Join(wf_spec, 'join', threshold=threshold, cancel=cancel) single = Simple(wf_spec, 'first', manual=True) default = Simple(wf_spec, 'default') choice = ExclusiveChoice(wf_spec, 'choice', manual=True) end = Simple(wf_spec, 'end') single.connect(join) join_condition = Equal(Attrib('should_join'), True) choice.connect_if(join_condition, join) choice.connect(default) split.connect(single) split.connect(choice) join.connect(end) workflow = Workflow(wf_spec) return workflow
def testAncestors(self): T1 = Simple(self.wf_spec, "T1") T2A = Simple(self.wf_spec, "T2A") T2B = Simple(self.wf_spec, "T2B") M = Join(self.wf_spec, "M") T3 = Simple(self.wf_spec, "T3") T1.follow(self.wf_spec.start) T2A.follow(T1) T2B.follow(T1) T2A.connect(M) T2B.connect(M) T3.follow(M) self.assertEquals(T1.ancestors(), [self.wf_spec.start]) self.assertEquals(T2A.ancestors(), [T1, self.wf_spec.start]) self.assertEquals(T2B.ancestors(), [T1, self.wf_spec.start]) M_ancestors = M.ancestors() self.assertIn(T1, M_ancestors) self.assertIn(T2A, M_ancestors) self.assertIn(T2B, M_ancestors) self.assertIn(self.wf_spec.start, M_ancestors) self.assertEqual(len(T3.ancestors()), 5)
def test_cyclic_wait(self): """ Tests that we can detect when two wait taks are witing on each other. """ task1 = Join(self.wf_spec, 'First') self.wf_spec.start.connect(task1) task2 = Join(self.wf_spec, 'Second') task1.connect(task2) task2.follow(task1) task1.follow(task2) results = self.wf_spec.validate() self.assertIn("Found loop with 'Second': Second->First then 'Second' " "again", results) self.assertIn("Found loop with 'First': First->Second then 'First' " "again", results)
def buildFLows(self, beforeId, afterId): task = None # 判断之后节点在nodeIdList列表中的index beforenode_index = self.nodeIdList.index(beforeId) afternode_index = self.nodeIdList.index(afterId) # 获取nodeList中对应节点的数据信息 beforeNodeInfo = self.nodeList[beforenode_index] afterNodeInfo = self.nodeList[afternode_index] beforeNodeName = beforeNodeInfo['name'] afterNodeName = afterNodeInfo['name'] try: task = self.get_task_spec_from_name(afterNodeName) except: if afterNodeInfo['type'] == 'MultiInstance': task = MultiInstance(self, afterNodeName, 1) elif afterNodeInfo['type'] == 'task': task = taskSpec(self, afterNodeName) elif afterNodeInfo['type'] == 'join': task = Join(self, afterNodeName) elif afterNodeInfo['type'] == 'end': task = Simple(self, 'End') print(afterNodeName) return task, afterNodeName, beforeNodeName
def create_instance(self): return Join(self.wf_spec, 'testtask', description='foo')
def __init__(self): WorkflowSpec.__init__(self) # The first step of our workflow is to let the general confirm # the nuclear strike. #workflow_run = taskSpec(self, 'Ping','hostname') #self.start.connect(workflow_run) #workflow_execute = taskSpec(self, 'Shell', ["ping", "-t", "1", "127.0.0.1"]) #workflow_run.connect(workflow_execute) # data = {'post_assign':{'name':'Test','value':'TestValues'}} # MultiInstance对当前任务进行拆分,1:要创建的任务数 multi_inst = MultiInstance(self,'workflow_task',1) self.start.connect(multi_inst) #taskSpec为任务规范,引用工作流规范,给定任务规范名称 workflow_1 = taskSpec(self, 'SQL') workflow_2 = taskSpec(self, '脚本') workflow_3 = taskSpec(self, 'SQL3') # TaskSpec,将给定对任务作为输出任务添加 multi_inst.connect(workflow_1) multi_inst.connect(workflow_2) multi_inst.connect(workflow_3) # 同步之前分割对任务,使用MultiInstance多实例模式时,join可以跨所有实例工作,在使用ThreadSplit时join将忽略来自另一个线程的实例。 synch_1 = Join(self, 'synch_1') #self.start.connect(synch_1) workflow_1.connect(synch_1) workflow_2.connect(synch_1) workflow_3.connect(synch_1) #gate_test = Gate(self,'gate1','synch_1') #synch_1.connect(gate_test) # 实现具有一个或多个输入,和任意数量输出的任务。 # 如果连接了多个输入,则任务执行隐式多重合并。 # 如果连接了多个输出,则任务执行隐式并行分割。 end = Simple(self, 'End') # 表示一个if条件,其中多个条件可能同时匹配,从而创建多个传出分支。此任务有一个或多个输入,以及一个或多个传入分支。 # multichoice = MultiChoice(self, 'multi_choice_1') # synch_1.connect(end) #gate_test.connect(end) #synch_1.connect(end) #synch_1.connect(multi_inst) #end = Simple(self, 'End') #workflow_execute.connect(end) # As soon as all tasks are either "completed" or "aborted", the # workflow implicitely ends. # ids = [] # for i in ids2: # afterNodeIds = [x for x in lineList if x['from'] == i['to']] # for each in afterNodeIds: # beforeId = each['from'] # afterId = each['to'] # task, afterNodeName, beforenode_index = self.buildFLows(beforeId, afterId, nodeIdList, nodeList) # task_list[task.name] = task # beforeNodeName = nodeList[beforenode_index]['name'] # beforeNode = task_list[beforeNodeName] # # if task_list[afterNodeName] not in beforeNode.outputs: # beforeNode.connect(task_list[afterNodeName]) # ids.append(each) # # ids2 = [] # task = None # for i in ids: # afterNodeIds = [x for x in lineList if x['from'] == i['to']] # toNode = [x['to'] for x in afterNodeIds] # # num=len(set(toNode)) # for each in afterNodeIds: # beforeId = each['from'] # afterId = each['to'] # task, afterNodeName, beforenode_index = self.buildFLows(beforeId, afterId, nodeIdList, nodeList) # # num-=1 # task_list[task.name] = task # beforeNodeName = nodeList[beforenode_index]['name'] # beforeNode = task_list[beforeNodeName] # # if task_list[afterNodeName] not in beforeNode.outputs: # beforeNode.connect(task_list[afterNodeName]) # ids2.append(each) # # ids = [] # for i in ids2: # afterNodeIds = [x for x in lineList if x['from'] == i['to']] # for each in afterNodeIds: # beforeId = each['from'] # afterId = each['to'] # task, afterNodeName, beforenode_index = self.buildFLows(beforeId, afterId, nodeIdList, nodeList) # task_list[task.name] = task # beforeNodeName = nodeList[beforenode_index]['name'] # beforeNode = task_list[beforeNodeName] # # if task_list[afterNodeName] not in beforeNode.outputs: # beforeNode.connect(task_list[afterNodeName]) # ids.append(each) # # ids2 = [] # task = None # for i in ids: # afterNodeIds = [x for x in lineList if x['from'] == i['to']] # toNode = [x['to'] for x in afterNodeIds] # # num=len(set(toNode)) # for each in afterNodeIds: # beforeId = each['from'] # afterId = each['to'] # task, afterNodeName, beforenode_index = self.buildFLows(beforeId, afterId, nodeIdList, nodeList) # # num-=1 # task_list[task.name] = task # beforeNodeName = nodeList[beforenode_index]['name'] # beforeNode = task_list[beforeNodeName] # # if task_list[afterNodeName] not in beforeNode.outputs: # beforeNode.connect(task_list[afterNodeName]) # ids2.append(each) # # ids = [] # for i in ids2: # afterNodeIds = [x for x in lineList if x['from'] == i['to']] # for each in afterNodeIds: # beforeId = each['from'] # afterId = each['to'] # task, afterNodeName, beforenode_index = self.buildFLows(beforeId, afterId, nodeIdList, nodeList) # task_list[task.name] = task # beforeNodeName = nodeList[beforenode_index]['name'] # beforeNode = task_list[beforeNodeName] # # if task_list[afterNodeName] not in beforeNode.outputs: # beforeNode.connect(task_list[afterNodeName]) # ids.append(each) # # ids2 = [] # task = None # for i in ids: # afterNodeIds = [x for x in lineList if x['from'] == i['to']] # toNode = [x['to'] for x in afterNodeIds] # # num=len(set(toNode)) # for each in afterNodeIds: # beforeId = each['from'] # afterId = each['to'] # task, afterNodeName, beforenode_index = self.buildFLows(beforeId, afterId, nodeIdList, nodeList) # # num-=1 # task_list[task.name] = task # beforeNodeName = nodeList[beforenode_index]['name'] # beforeNode = task_list[beforeNodeName] # # if task_list[afterNodeName] not in beforeNode.outputs: # beforeNode.connect(task_list[afterNodeName]) # ids2.append(each) # # ids = [] # for i in ids2: # afterNodeIds = [x for x in lineList if x['from'] == i['to']] # for each in afterNodeIds: # beforeId = each['from'] # afterId = each['to'] # task, afterNodeName, beforenode_index = self.buildFLows(beforeId, afterId, nodeIdList, nodeList) # task_list[task.name] = task # beforeNodeName = nodeList[beforenode_index]['name'] # beforeNode = task_list[beforeNodeName] # # if task_list[afterNodeName] not in beforeNode.outputs: # beforeNode.connect(task_list[afterNodeName]) # ids.append(each) # # ids2 = [] # task = None # for i in ids: # afterNodeIds = [x for x in lineList if x['from'] == i['to']] # toNode = [x['to'] for x in afterNodeIds] # # num=len(set(toNode)) # for each in afterNodeIds: # beforeId = each['from'] # afterId = each['to'] # task, afterNodeName, beforenode_index = self.buildFLows(beforeId, afterId, nodeIdList, nodeList) # # num-=1 # task_list[task.name] = task # beforeNodeName = nodeList[beforenode_index]['name'] # beforeNode = task_list[beforeNodeName] # # if task_list[afterNodeName] not in beforeNode.outputs: # beforeNode.connect(task_list[afterNodeName]) # ids2.append(each) # # ids = [] # for i in ids2: # afterNodeIds = [x for x in lineList if x['from'] == i['to']] # for each in afterNodeIds: # beforeId = each['from'] # afterId = each['to'] # task, afterNodeName, beforenode_index = self.buildFLows(beforeId, afterId, nodeIdList, nodeList) # task_list[task.name] = task # beforeNodeName = nodeList[beforenode_index]['name'] # beforeNode = task_list[beforeNodeName] # # if task_list[afterNodeName] not in beforeNode.outputs: # beforeNode.connect(task_list[afterNodeName]) # ids.append(each)