Skip to content

Commit e96f6bf

Browse files
committed
add example
1 parent 58d65cd commit e96f6bf

6 files changed

Lines changed: 133 additions & 2 deletions

File tree

.github/hooks/pre-commit

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
if hatch fmt --check; then
4+
echo "Hatch fmt check passed!"
5+
else
6+
hatch fmt
7+
echo "Error: hatch fmt modified your files. Please re-stage and commit again."
8+
exit 1
9+
fi

examples/examples-catalog.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,17 @@
580580
"ApplicationLogLevel": "DEBUG",
581581
"LogFormat": "JSON"
582582
}
583-
}
583+
},
584+
{
585+
"name": "Plugin",
586+
"description": "Test plugin",
587+
"handler": "execution_with_plugin.handler",
588+
"integration": true,
589+
"durableConfig": {
590+
"RetentionPeriodInDays": 7,
591+
"ExecutionTimeout": 300
592+
},
593+
"path": "./src/plugin/execution_with_plugin.py"
594+
}
584595
]
585596
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
)

examples/template.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,24 @@
941941
"ExecutionTimeout": 300
942942
}
943943
}
944+
},
945+
"ExecutionWithPlugin": {
946+
"Type": "AWS::Serverless::Function",
947+
"Properties": {
948+
"CodeUri": "build/",
949+
"Handler": "execution_with_plugin.handler",
950+
"Description": "Test plugin",
951+
"Role": {
952+
"Fn::GetAtt": [
953+
"DurableFunctionRole",
954+
"Arn"
955+
]
956+
},
957+
"DurableConfig": {
958+
"RetentionPeriodInDays": 7,
959+
"ExecutionTimeout": 300
960+
}
961+
}
944962
}
945963
}
946964
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Tests for step example."""
2+
3+
import pytest
4+
from aws_durable_execution_sdk_python.execution import InvocationStatus
5+
6+
from src.step import step
7+
from test.conftest import deserialize_operation_payload
8+
9+
10+
@pytest.mark.example
11+
@pytest.mark.durable_execution(
12+
handler=step.handler,
13+
lambda_function_name="Plugin",
14+
)
15+
def test_step(durable_runner):
16+
"""Test basic step example."""
17+
with durable_runner:
18+
result = durable_runner.run(input="{}", timeout=10)
19+
20+
assert result.status is InvocationStatus.SUCCEEDED
21+
assert deserialize_operation_payload(result.result) == 8
22+
23+
step_result = result.get_step("add_numbers")
24+
assert deserialize_operation_payload(step_result.result) == 8

src/aws_durable_execution_sdk_python/execution.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import logging
77
from concurrent.futures import ThreadPoolExecutor
88
from dataclasses import dataclass
9-
from enum import Enum
109
from typing import TYPE_CHECKING, Any
1110

1211
from aws_durable_execution_sdk_python.context import DurableContext

0 commit comments

Comments
 (0)