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