|
| 1 | +"""End-to-end sample that demonstrates how to configure an orchestrator |
| 2 | +that a dynamic number activity functions in parallel, waits for them all |
| 3 | +to complete, and prints an aggregate summary of the outputs.""" |
| 4 | +import random |
| 5 | +import time |
| 6 | +import os |
| 7 | +from durabletask import client, task |
| 8 | +from durabletask import client, task |
| 9 | +from externalpackages.durabletaskscheduler.durabletask_scheduler_worker import DurableTaskSchedulerWorker |
| 10 | +from externalpackages.durabletaskscheduler.durabletask_scheduler_client import DurableTaskSchedulerClient |
| 11 | +from externalpackages.durabletaskscheduler.access_token_manager import AccessTokenManager |
| 12 | + |
| 13 | + |
| 14 | +def get_work_items(ctx: task.ActivityContext, _) -> list[str]: |
| 15 | + """Activity function that returns a list of work items""" |
| 16 | + # return a random number of work items |
| 17 | + count = random.randint(2, 10) |
| 18 | + print(f'generating {count} work items...') |
| 19 | + return [f'work item {i}' for i in range(count)] |
| 20 | + |
| 21 | + |
| 22 | +def process_work_item(ctx: task.ActivityContext, item: str) -> int: |
| 23 | + """Activity function that returns a result for a given work item""" |
| 24 | + print(f'processing work item: {item}') |
| 25 | + |
| 26 | + # simulate some work that takes a variable amount of time |
| 27 | + time.sleep(random.random() * 5) |
| 28 | + |
| 29 | + # return a result for the given work item, which is also a random number in this case |
| 30 | + return random.randint(0, 10) |
| 31 | + |
| 32 | + |
| 33 | +def orchestrator(ctx: task.OrchestrationContext, _): |
| 34 | + """Orchestrator function that calls the 'get_work_items' and 'process_work_item' |
| 35 | + activity functions in parallel, waits for them all to complete, and prints |
| 36 | + an aggregate summary of the outputs""" |
| 37 | + |
| 38 | + work_items: list[str] = yield ctx.call_activity(get_work_items) |
| 39 | + |
| 40 | + # execute the work-items in parallel and wait for them all to return |
| 41 | + tasks = [ctx.call_activity(process_work_item, input=item) for item in work_items] |
| 42 | + results: list[int] = yield task.when_all(tasks) |
| 43 | + |
| 44 | + # return an aggregate summary of the results |
| 45 | + return { |
| 46 | + 'work_items': work_items, |
| 47 | + 'results': results, |
| 48 | + 'total': sum(results), |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | +# Read the environment variable |
| 53 | +taskhub_name = os.getenv("TASKHUB") |
| 54 | + |
| 55 | +# Check if the variable exists |
| 56 | +if taskhub_name: |
| 57 | + print(f"The value of TASKHUB is: {taskhub_name}") |
| 58 | +else: |
| 59 | + print("TASKHUB is not set. Please set the TASKHUB environment variable to the name of the taskhub you wish to use") |
| 60 | + print("If you are using windows powershell, run the following: $env:TASKHUB=\"<taskhubname>\"") |
| 61 | + print("If you are using bash, run the following: export TASKHUB=\"<taskhubname>\"") |
| 62 | + exit() |
| 63 | + |
| 64 | +# Read the environment variable |
| 65 | +endpoint = os.getenv("ENDPOINT") |
| 66 | + |
| 67 | +# Check if the variable exists |
| 68 | +if endpoint: |
| 69 | + print(f"The value of ENDPOINT is: {endpoint}") |
| 70 | +else: |
| 71 | + print("ENDPOINT is not set. Please set the ENDPOINT environment variable to the endpoint of the scheduler") |
| 72 | + print("If you are using windows powershell, run the following: $env:ENDPOINT=\"<schedulerEndpoint>\"") |
| 73 | + print("If you are using bash, run the following: export ENDPOINT=\"<schedulerEndpoint>\"") |
| 74 | + exit() |
| 75 | + |
| 76 | +# Define the scope for Azure Resource Manager (ARM) |
| 77 | +arm_scope = "https://durabletask.io/.default" |
| 78 | +token_manager = AccessTokenManager(scope = arm_scope) |
| 79 | + |
| 80 | +meta_data: list[tuple[str, str]] = [ |
| 81 | + ("taskhub", taskhub_name) |
| 82 | +] |
| 83 | + |
| 84 | + |
| 85 | +# configure and start the worker |
| 86 | +with DurableTaskSchedulerWorker(host_address=endpoint, metadata=meta_data, secure_channel=True, access_token_manager=token_manager) as w: |
| 87 | + w.add_orchestrator(orchestrator) |
| 88 | + w.add_activity(process_work_item) |
| 89 | + w.add_activity(get_work_items) |
| 90 | + w.start() |
| 91 | + |
| 92 | + # create a client, start an orchestration, and wait for it to finish |
| 93 | + c = DurableTaskSchedulerClient(host_address=endpoint, metadata=meta_data, secure_channel=True, access_token_manager=token_manager) |
| 94 | + instance_id = c.schedule_new_orchestration(orchestrator) |
| 95 | + state = c.wait_for_orchestration_completion(instance_id, timeout=30) |
| 96 | + if state and state.runtime_status == client.OrchestrationStatus.COMPLETED: |
| 97 | + print(f'Orchestration completed! Result: {state.serialized_output}') |
| 98 | + elif state: |
| 99 | + print(f'Orchestration failed: {state.failure_details}') |
0 commit comments