|
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 | | -from durabletask import task |
5 | | -from durabletask.azuremanaged.worker import DurableTaskSchedulerWorker |
6 | | -from durabletask.azuremanaged.client import DurableTaskSchedulerClient, OrchestrationStatus |
7 | | -from azure.identity import DefaultAzureCredential |
8 | | - |
9 | | -def hello(ctx: task.ActivityContext, name: str) -> str: |
10 | | - """Activity function that returns a greeting""" |
11 | | - return f'Hello {name}!' |
12 | | - |
13 | | - |
14 | | -def sequence(ctx: task.OrchestrationContext, _): |
15 | | - """Orchestrator function that calls the 'hello' activity function in a sequence""" |
16 | | - # call "hello" activity function in a sequence |
17 | | - result1 = yield ctx.call_activity(hello, input='Tokyo') |
18 | | - result2 = yield ctx.call_activity(hello, input='Seattle') |
19 | | - result3 = yield ctx.call_activity(hello, input='London') |
20 | | - |
21 | | - # return an array of results |
22 | | - return [result1, result2, result3] |
23 | | - |
24 | | - |
25 | | -# Read the environment variable |
26 | | -taskhub_name = os.getenv("TASKHUB") |
27 | | - |
28 | | -# Check if the variable exists |
29 | | -if taskhub_name: |
30 | | - print(f"The value of TASKHUB is: {taskhub_name}") |
31 | | -else: |
32 | | - print("TASKHUB is not set. Please set the TASKHUB environment variable to the name of the taskhub you wish to use") |
33 | | - print("If you are using windows powershell, run the following: $env:TASKHUB=\"<taskhubname>\"") |
34 | | - print("If you are using bash, run the following: export TASKHUB=\"<taskhubname>\"") |
35 | | - exit() |
36 | | - |
37 | | -# Read the environment variable |
38 | | -endpoint = os.getenv("ENDPOINT") |
39 | | - |
40 | | -# Check if the variable exists |
41 | | -if endpoint: |
42 | | - print(f"The value of ENDPOINT is: {endpoint}") |
43 | | -else: |
44 | | - print("ENDPOINT is not set. Please set the ENDPOINT environment variable to the endpoint of the scheduler") |
45 | | - print("If you are using windows powershell, run the following: $env:ENDPOINT=\"<schedulerEndpoint>\"") |
46 | | - print("If you are using bash, run the following: export ENDPOINT=\"<schedulerEndpoint>\"") |
47 | | - exit() |
48 | | - |
49 | | -credential = DefaultAzureCredential() |
50 | | - |
51 | | -# configure and start the worker |
52 | | -with DurableTaskSchedulerWorker(host_address=endpoint, secure_channel=True, |
53 | | - taskhub=taskhub_name, token_credential=credential) as w: |
54 | | - w.add_orchestrator(sequence) |
55 | | - w.add_activity(hello) |
56 | | - w.start() |
57 | | - |
58 | | - # Construct the client and run the orchestrations |
59 | | - c = DurableTaskSchedulerClient(host_address=endpoint, secure_channel=True, |
60 | | - taskhub=taskhub_name, token_credential=credential) |
61 | | - instance_id = c.schedule_new_orchestration(sequence) |
62 | | - state = c.wait_for_orchestration_completion(instance_id, timeout=60) |
63 | | - if state and state.runtime_status == OrchestrationStatus.COMPLETED: |
64 | | - print(f'Orchestration completed! Result: {state.serialized_output}') |
65 | | - elif state: |
66 | | - 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 | +from durabletask import task |
| 5 | +from durabletask.azuremanaged.worker import DurableTaskSchedulerWorker |
| 6 | +from durabletask.azuremanaged.client import DurableTaskSchedulerClient, OrchestrationStatus |
| 7 | +from azure.identity import DefaultAzureCredential |
| 8 | + |
| 9 | +def hello(ctx: task.ActivityContext, name: str) -> str: |
| 10 | + """Activity function that returns a greeting""" |
| 11 | + return f'Hello {name}!' |
| 12 | + |
| 13 | + |
| 14 | +def sequence(ctx: task.OrchestrationContext, _): |
| 15 | + """Orchestrator function that calls the 'hello' activity function in a sequence""" |
| 16 | + # call "hello" activity function in a sequence |
| 17 | + result1 = yield ctx.call_activity(hello, input='Tokyo') |
| 18 | + result2 = yield ctx.call_activity(hello, input='Seattle') |
| 19 | + result3 = yield ctx.call_activity(hello, input='London') |
| 20 | + |
| 21 | + # return an array of results |
| 22 | + return [result1, result2, result3] |
| 23 | + |
| 24 | + |
| 25 | +# Read the environment variable |
| 26 | +taskhub_name = os.getenv("TASKHUB") |
| 27 | + |
| 28 | +# Check if the variable exists |
| 29 | +if taskhub_name: |
| 30 | + print(f"The value of TASKHUB is: {taskhub_name}") |
| 31 | +else: |
| 32 | + print("TASKHUB is not set. Please set the TASKHUB environment variable to the name of the taskhub you wish to use") |
| 33 | + print("If you are using windows powershell, run the following: $env:TASKHUB=\"<taskhubname>\"") |
| 34 | + print("If you are using bash, run the following: export TASKHUB=\"<taskhubname>\"") |
| 35 | + exit() |
| 36 | + |
| 37 | +# Read the environment variable |
| 38 | +endpoint = os.getenv("ENDPOINT") |
| 39 | + |
| 40 | +# Check if the variable exists |
| 41 | +if endpoint: |
| 42 | + print(f"The value of ENDPOINT is: {endpoint}") |
| 43 | +else: |
| 44 | + print("ENDPOINT is not set. Please set the ENDPOINT environment variable to the endpoint of the scheduler") |
| 45 | + print("If you are using windows powershell, run the following: $env:ENDPOINT=\"<schedulerEndpoint>\"") |
| 46 | + print("If you are using bash, run the following: export ENDPOINT=\"<schedulerEndpoint>\"") |
| 47 | + exit() |
| 48 | + |
| 49 | +credential = DefaultAzureCredential() |
| 50 | + |
| 51 | +# configure and start the worker |
| 52 | +with DurableTaskSchedulerWorker(host_address=endpoint, secure_channel=True, |
| 53 | + taskhub=taskhub_name, token_credential=credential) as w: |
| 54 | + w.add_orchestrator(sequence) |
| 55 | + w.add_activity(hello) |
| 56 | + w.start() |
| 57 | + |
| 58 | + # Construct the client and run the orchestrations |
| 59 | + c = DurableTaskSchedulerClient(host_address=endpoint, secure_channel=True, |
| 60 | + taskhub=taskhub_name, token_credential=credential) |
| 61 | + instance_id = c.schedule_new_orchestration(sequence) |
| 62 | + state = c.wait_for_orchestration_completion(instance_id, timeout=60) |
| 63 | + if state and state.runtime_status == OrchestrationStatus.COMPLETED: |
| 64 | + print(f'Orchestration completed! Result: {state.serialized_output}') |
| 65 | + elif state: |
| 66 | + print(f'Orchestration failed: {state.failure_details}') |
0 commit comments