|
| 1 | +""" |
| 2 | +This is a dynamic workflow that can be created and executed at run time. |
| 3 | +dynamic_workflow will run worker tasks get_user_email and send_email in the same order. |
| 4 | +For use cases in which the workflow cannot be defined statically, dynamic workflows is a useful approach. |
| 5 | +For detailed explanation, https://github.com/conductor-sdk/conductor-python/blob/main/workflows.md |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | + |
| 10 | +from conductor.asyncio_client.automator.task_handler import TaskHandler |
| 11 | +from conductor.asyncio_client.configuration.configuration import Configuration |
| 12 | +from conductor.asyncio_client.adapters import ApiClient |
| 13 | +from conductor.asyncio_client.orkes.orkes_clients import OrkesClients |
| 14 | +from conductor.asyncio_client.worker.worker_task import worker_task |
| 15 | +from conductor.asyncio_client.workflow.conductor_workflow import AsyncConductorWorkflow |
| 16 | + |
| 17 | + |
| 18 | +@worker_task(task_definition_name="get_user_email") |
| 19 | +def get_user_email(userid: str) -> str: |
| 20 | + return f"{userid}@example.com" |
| 21 | + |
| 22 | + |
| 23 | +@worker_task(task_definition_name="send_email") |
| 24 | +def send_email(email: str, subject: str, body: str): |
| 25 | + print(f"sending email to {email} with subject {subject} and body {body}") |
| 26 | + |
| 27 | + |
| 28 | +async def main(): |
| 29 | + # defaults to reading the configuration using following env variables |
| 30 | + # CONDUCTOR_SERVER_URL : conductor server e.g. https://play.orkes.io/api |
| 31 | + # CONDUCTOR_AUTH_KEY : API Authentication Key |
| 32 | + # CONDUCTOR_AUTH_SECRET: API Auth Secret |
| 33 | + api_config = Configuration() |
| 34 | + task_handler = TaskHandler(configuration=api_config) |
| 35 | + task_handler.start_processes() |
| 36 | + |
| 37 | + async with ApiClient(api_config) as api_client: |
| 38 | + clients = OrkesClients(api_client=api_client, configuration=api_config) |
| 39 | + workflow_executor = clients.get_workflow_executor() |
| 40 | + workflow = AsyncConductorWorkflow( |
| 41 | + name="dynamic_workflow", version=1, executor=workflow_executor |
| 42 | + ) |
| 43 | + get_email = get_user_email( |
| 44 | + task_ref_name="get_user_email_ref", userid=workflow.input("userid") |
| 45 | + ) |
| 46 | + sendmail = send_email( |
| 47 | + task_ref_name="send_email_ref", |
| 48 | + email=get_email.output("result"), |
| 49 | + subject="Hello from Orkes", |
| 50 | + body="Test Email", |
| 51 | + ) |
| 52 | + |
| 53 | + workflow >> get_email >> sendmail |
| 54 | + |
| 55 | + # Configure the output of the workflow |
| 56 | + workflow.output_parameters( |
| 57 | + output_parameters={"email": get_email.output("result")} |
| 58 | + ) |
| 59 | + |
| 60 | + workflow_run = await workflow.execute(workflow_input={"userid": "user_a"}) |
| 61 | + print(f"\nworkflow output: {workflow_run.output}\n") |
| 62 | + print( |
| 63 | + f"check the workflow execution here: {api_config.ui_host}/execution/{workflow_run.workflow_id}" |
| 64 | + ) |
| 65 | + |
| 66 | + task_handler.stop_processes() |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + asyncio.run(main()) |
0 commit comments