|
| 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, entities |
| 8 | +from durabletask.azuremanaged.client import DurableTaskSchedulerClient |
| 9 | +from durabletask.azuremanaged.worker import DurableTaskSchedulerWorker |
| 10 | + |
| 11 | + |
| 12 | +class Counter(entities.DurableEntity): |
| 13 | + def set(self, input: int): |
| 14 | + self.set_state(input) |
| 15 | + |
| 16 | + def add(self, input: int): |
| 17 | + current_state = self.get_state(int, 0) |
| 18 | + new_state = current_state + (input or 1) |
| 19 | + self.set_state(new_state) |
| 20 | + return new_state |
| 21 | + |
| 22 | + def update_parent(self): |
| 23 | + parent_entity_id = entities.EntityInstanceId("Counter", "parentCounter") |
| 24 | + if self.entity_context.entity_id == parent_entity_id: |
| 25 | + return # Prevent self-update |
| 26 | + self.signal_entity(parent_entity_id, "set", self.get_state(int, 0)) |
| 27 | + |
| 28 | + def start_hello(self): |
| 29 | + self.schedule_new_orchestration("hello_orchestrator") |
| 30 | + |
| 31 | + def get(self): |
| 32 | + return self.get_state(int, 0) |
| 33 | + |
| 34 | + |
| 35 | +def counter_orchestrator(ctx: task.OrchestrationContext, _): |
| 36 | + """Orchestrator function that demonstrates the behavior of the counter entity""" |
| 37 | + |
| 38 | + entity_id = task.EntityInstanceId("Counter", "myCounter") |
| 39 | + parent_entity_id = task.EntityInstanceId("Counter", "parentCounter") |
| 40 | + |
| 41 | + # Use Counter to demonstrate starting an orchestration from an entity |
| 42 | + ctx.signal_entity(entity_id, "start_hello") |
| 43 | + |
| 44 | + # User Counter to demonstrate signaling an entity from another entity |
| 45 | + # Initialize myCounter with state 0, increment it by 1, and set the state of parentCounter using |
| 46 | + # update_parent on myCounter. Retrieve and return the state of parentCounter (should be 1). |
| 47 | + ctx.signal_entity(entity_id, "set", 0) |
| 48 | + yield ctx.call_entity(entity_id, "add", 1) |
| 49 | + yield ctx.call_entity(entity_id, "update_parent") |
| 50 | + |
| 51 | + return (yield ctx.call_entity(parent_entity_id, "get")) |
| 52 | + |
| 53 | + |
| 54 | +def hello_orchestrator(ctx: task.OrchestrationContext, _): |
| 55 | + return f"Hello world!" |
| 56 | + |
| 57 | + |
| 58 | +# Use environment variables if provided, otherwise use default emulator values |
| 59 | +taskhub_name = os.getenv("TASKHUB", "default") |
| 60 | +endpoint = os.getenv("ENDPOINT", "http://localhost:8080") |
| 61 | + |
| 62 | +print(f"Using taskhub: {taskhub_name}") |
| 63 | +print(f"Using endpoint: {endpoint}") |
| 64 | + |
| 65 | +# Set credential to None for emulator, or DefaultAzureCredential for Azure |
| 66 | +credential = None if endpoint == "http://localhost:8080" else DefaultAzureCredential() |
| 67 | + |
| 68 | +# configure and start the worker - use secure_channel=False for emulator |
| 69 | +secure_channel = endpoint != "http://localhost:8080" |
| 70 | +with DurableTaskSchedulerWorker(host_address=endpoint, secure_channel=secure_channel, |
| 71 | + taskhub=taskhub_name, token_credential=credential) as w: |
| 72 | + w.add_orchestrator(counter_orchestrator) |
| 73 | + w.add_orchestrator(hello_orchestrator) |
| 74 | + w.add_entity(Counter) |
| 75 | + w.start() |
| 76 | + |
| 77 | + # Construct the client and run the orchestrations |
| 78 | + c = DurableTaskSchedulerClient(host_address=endpoint, secure_channel=secure_channel, |
| 79 | + taskhub=taskhub_name, token_credential=credential) |
| 80 | + instance_id = c.schedule_new_orchestration(counter_orchestrator) |
| 81 | + state = c.wait_for_orchestration_completion(instance_id, timeout=60) |
| 82 | + if state and state.runtime_status == client.OrchestrationStatus.COMPLETED: |
| 83 | + print(f'Orchestration completed! Result: {state.serialized_output}') |
| 84 | + elif state: |
| 85 | + print(f'Orchestration failed: {state.failure_details}') |
0 commit comments