|
1 |
| -"""End-to-end sample that demonstrates how to configure an orchestrator |
2 |
| -that calls an activity function in a sequence and prints the outputs.""" |
3 |
| -from durabletask import client, task, worker |
4 |
| - |
5 |
| - |
6 |
| -def hello(ctx: task.ActivityContext, name: str) -> str: |
7 |
| - """Activity function that returns a greeting""" |
8 |
| - return f'Hello {name}!' |
9 |
| - |
10 |
| - |
11 |
| -def sequence(ctx: task.OrchestrationContext, _): |
12 |
| - """Orchestrator function that calls the 'hello' activity function in a sequence""" |
13 |
| - # call "hello" activity function in a sequence |
14 |
| - result1 = yield ctx.call_activity(hello, input='Tokyo') |
15 |
| - result2 = yield ctx.call_activity(hello, input='Seattle') |
16 |
| - result3 = yield ctx.call_activity(hello, input='London') |
17 |
| - |
18 |
| - # return an array of results |
19 |
| - return [result1, result2, result3] |
20 |
| - |
21 |
| - |
22 |
| -# configure and start the worker |
23 |
| -with worker.TaskHubGrpcWorker() as w: |
24 |
| - w.add_orchestrator(sequence) |
25 |
| - w.add_activity(hello) |
26 |
| - w.start() |
27 |
| - |
28 |
| - # create a client, start an orchestration, and wait for it to finish |
29 |
| - c = client.TaskHubGrpcClient() |
30 |
| - instance_id = c.schedule_new_orchestration(sequence) |
31 |
| - state = c.wait_for_orchestration_completion(instance_id, timeout=10) |
32 |
| - if state and state.runtime_status == client.OrchestrationStatus.COMPLETED: |
33 |
| - print(f'Orchestration completed! Result: {state.serialized_output}') |
34 |
| - elif state: |
35 |
| - print(f'Orchestration failed: {state.failure_details}') |
| 1 | +"""End-to-end sample that demonstrates how to configure an orchestrator |
| 2 | +that calls an activity function in a sequence and prints the outputs.""" |
| 3 | +import os |
| 4 | + |
| 5 | +from azure.identity import DefaultAzureCredential |
| 6 | + |
| 7 | +from durabletask import client, task |
| 8 | +from durabletask.azuremanaged.client import DurableTaskSchedulerClient |
| 9 | +from durabletask.azuremanaged.worker import DurableTaskSchedulerWorker |
| 10 | + |
| 11 | + |
| 12 | +def hello(ctx: task.ActivityContext, name: str) -> str: |
| 13 | + """Activity function that returns a greeting""" |
| 14 | + return f'Hello {name}!' |
| 15 | + |
| 16 | + |
| 17 | +def sequence(ctx: task.OrchestrationContext, _): |
| 18 | + """Orchestrator function that calls the 'hello' activity function in a sequence""" |
| 19 | + # call "hello" activity function in a sequence |
| 20 | + result1 = yield ctx.call_activity(hello, input='Tokyo') |
| 21 | + result2 = yield ctx.call_activity(hello, input='Seattle') |
| 22 | + result3 = yield ctx.call_activity(hello, input='London') |
| 23 | + |
| 24 | + # return an array of results |
| 25 | + return [result1, result2, result3] |
| 26 | + |
| 27 | + |
| 28 | +# Use environment variables if provided, otherwise use default emulator values |
| 29 | +taskhub_name = os.getenv("TASKHUB", "default") |
| 30 | +endpoint = os.getenv("ENDPOINT", "http://localhost:8080") |
| 31 | + |
| 32 | +print(f"Using taskhub: {taskhub_name}") |
| 33 | +print(f"Using endpoint: {endpoint}") |
| 34 | + |
| 35 | +# Set credential to None for emulator, or DefaultAzureCredential for Azure |
| 36 | +credential = None if endpoint == "http://localhost:8080" else DefaultAzureCredential() |
| 37 | + |
| 38 | +# configure and start the worker - use secure_channel=False for emulator |
| 39 | +secure_channel = endpoint != "http://localhost:8080" |
| 40 | +with DurableTaskSchedulerWorker(host_address=endpoint, secure_channel=secure_channel, |
| 41 | + taskhub=taskhub_name, token_credential=credential) as w: |
| 42 | + w.add_orchestrator(sequence) |
| 43 | + w.add_activity(hello) |
| 44 | + w.start() |
| 45 | + |
| 46 | + # Construct the client and run the orchestrations |
| 47 | + c = DurableTaskSchedulerClient(host_address=endpoint, secure_channel=secure_channel, |
| 48 | + taskhub=taskhub_name, token_credential=credential) |
| 49 | + instance_id = c.schedule_new_orchestration(sequence) |
| 50 | + state = c.wait_for_orchestration_completion(instance_id, timeout=60) |
| 51 | + if state and state.runtime_status == client.OrchestrationStatus.COMPLETED: |
| 52 | + print(f'Orchestration completed! Result: {state.serialized_output}') |
| 53 | + elif state: |
| 54 | + print(f'Orchestration failed: {state.failure_details}') |
0 commit comments