|
| 1 | +"""Demonstrates handler execution without any durable operations.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from aws_durable_execution_sdk_python import StepContext |
| 7 | +from aws_durable_execution_sdk_python.context import ( |
| 8 | + DurableContext, |
| 9 | + durable_step, |
| 10 | + durable_with_child_context, |
| 11 | +) |
| 12 | +from aws_durable_execution_sdk_python.execution import durable_execution |
| 13 | +from aws_durable_execution_sdk_python.plugin import ( |
| 14 | + DurableExecutionPlugin, |
| 15 | + AttemptStartInfo, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +class MyPlugin(DurableExecutionPlugin): |
| 20 | + logger = logging.getLogger("MyPlugin") |
| 21 | + |
| 22 | + def on_execution_start(self, info): |
| 23 | + self.logger.info(f"Execution started: {info}") |
| 24 | + |
| 25 | + def on_execution_end(self, info): |
| 26 | + self.logger.info(f"Execution ended: {info}") |
| 27 | + |
| 28 | + def on_operation_start(self, info): |
| 29 | + self.logger.info(f"Operation started: {info}") |
| 30 | + |
| 31 | + def on_operation_end(self, info): |
| 32 | + self.logger.info(f"Operation ended: {info}") |
| 33 | + |
| 34 | + def on_invocation_start(self, info): |
| 35 | + self.logger.info(f"Invocation started: {info}") |
| 36 | + |
| 37 | + def on_invocation_end(self, info): |
| 38 | + self.logger.info(f"Invocation ended: {info}") |
| 39 | + |
| 40 | + def on_operation_attempt_start(self, info: AttemptStartInfo) -> None: |
| 41 | + self.logger.info(f"Attempt started: {info}") |
| 42 | + |
| 43 | + def on_operation_attempt_end(self, info) -> None: |
| 44 | + self.logger.info(f"Attempt ended: {info}") |
| 45 | + |
| 46 | + |
| 47 | +@durable_step |
| 48 | +def add_numbers(_step_context: StepContext, a: int, b: int) -> int: |
| 49 | + return a + b |
| 50 | + |
| 51 | + |
| 52 | +@durable_with_child_context |
| 53 | +def add_numbers_in_child(child_context: DurableContext, a: int, b: int): |
| 54 | + result: int = child_context.step( |
| 55 | + add_numbers(a, b), |
| 56 | + name="add-a-and-b", |
| 57 | + ) |
| 58 | + return result |
| 59 | + |
| 60 | + |
| 61 | +@durable_execution(plugins=[MyPlugin()]) |
| 62 | +def handler(_event: Any, context: DurableContext) -> int: |
| 63 | + result: int = context.run_in_child_context( |
| 64 | + add_numbers_in_child(6, 4), |
| 65 | + name="add-6-and-4", |
| 66 | + ) |
| 67 | + return context.step( |
| 68 | + add_numbers(result, 2), |
| 69 | + name="add-result-to-2", |
| 70 | + ) |
0 commit comments