|
| 1 | +import json |
| 2 | + |
| 3 | +from conductor.client.configuration.configuration import Configuration |
| 4 | +from conductor.client.http.models import StartWorkflowRequest, RerunWorkflowRequest, TaskResult, WorkflowRun, \ |
| 5 | + WorkflowDef |
| 6 | +from conductor.client.http.models.task_result_status import TaskResultStatus |
| 7 | +from conductor.client.http.models.workflow_def import to_workflow_def |
| 8 | +from conductor.client.http.models.workflow_state_update import WorkflowStateUpdate |
| 9 | +from conductor.client.orkes_clients import OrkesClients |
| 10 | +from conductor.client.workflow.conductor_workflow import ConductorWorkflow |
| 11 | +from conductor.client.workflow.task.fork_task import ForkTask |
| 12 | +from conductor.client.workflow.task.http_task import HttpTask |
| 13 | +from conductor.client.workflow.task.join_task import JoinTask |
| 14 | +from conductor.client.workflow_client import WorkflowClient |
| 15 | + |
| 16 | + |
| 17 | +def main(): |
| 18 | + api_config = Configuration() |
| 19 | + clients = OrkesClients(configuration=api_config) |
| 20 | + workflow_client = clients.get_workflow_client() |
| 21 | + executor = clients.get_workflow_executor() |
| 22 | + |
| 23 | + workflow = ConductorWorkflow(name='fork_join_example', version=1, executor=executor) |
| 24 | + fork_size = 10 |
| 25 | + tasks = [] |
| 26 | + join_on = [] |
| 27 | + for i in range(fork_size): |
| 28 | + http = HttpTask(task_ref_name=f"http_{i}", |
| 29 | + http_input={"uri": "https://orkes-api-tester.orkesconductor.com/api2"}) |
| 30 | + http.optional = True |
| 31 | + tasks.append([http]) |
| 32 | + join_on.append(f"http_{i}") |
| 33 | + |
| 34 | + # HTTP tasks are marked as optional and the URL gives 404 error |
| 35 | + # the script below checks if the tasks are completed or completed with errors and completes the join task |
| 36 | + script = """ |
| 37 | + (function(){ |
| 38 | + let results = {}; |
| 39 | + let pendingJoinsFound = false; |
| 40 | + if($.joinOn){ |
| 41 | + $.joinOn.forEach((element)=>{ |
| 42 | + if($[element] && $[element].status !== 'COMPLETED' && $[element] && $[element].status !== 'COMPLETED_WITH_ERRORS'){ |
| 43 | + results[element] = $[element].status; |
| 44 | + pendingJoinsFound = true; |
| 45 | + } |
| 46 | + }); |
| 47 | + if(pendingJoinsFound){ |
| 48 | + return { |
| 49 | + "status":"IN_PROGRESS", |
| 50 | + "reasonForIncompletion":"Pending", |
| 51 | + "outputData":{ |
| 52 | + "scriptResults": results |
| 53 | + } |
| 54 | + }; |
| 55 | + } |
| 56 | + // To complete the Join - return true OR an object with status = 'COMPLETED' like above. |
| 57 | + return true; |
| 58 | + } |
| 59 | + })(); |
| 60 | + """ |
| 61 | + join = JoinTask(task_ref_name='join', join_on_script=script, join_on=join_on) |
| 62 | + fork = ForkTask(task_ref_name="fork", forked_tasks=tasks) |
| 63 | + workflow >> fork >> join |
| 64 | + workflow_id = workflow.start_workflow_with_input() |
| 65 | + print(f'started workflow with id {workflow_id}') |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == '__main__': |
| 69 | + main() |
0 commit comments