generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Implement send callback request for local runner #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,62 @@ | ||
| from typing import Any | ||
| """Simple durable Lambda handler example. | ||
|
|
||
| from aws_durable_execution_sdk_python.context import DurableContext | ||
| This example demonstrates: | ||
| - Step execution with logging | ||
| - Wait operations (pausing without consuming resources) | ||
| - Replay-aware logging | ||
| - Returning a response | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from aws_durable_execution_sdk_python.config import Duration | ||
| from aws_durable_execution_sdk_python.context import DurableContext, durable_step | ||
| from aws_durable_execution_sdk_python.execution import durable_execution | ||
|
|
||
| if TYPE_CHECKING: | ||
| from aws_durable_execution_sdk_python.types import StepContext | ||
|
|
||
|
|
||
| @durable_step | ||
| def step_1(step_context: StepContext) -> None: | ||
| """First step that logs a message.""" | ||
| step_context.logger.info("Hello from step1") | ||
|
|
||
|
|
||
| @durable_step | ||
| def step_2(step_context: StepContext, status_code: int) -> str: | ||
| """Second step that returns a message.""" | ||
| step_context.logger.info("Returning message with status code: %d", status_code) | ||
| return f"Hello from Durable Lambda! (status: {status_code})" | ||
|
|
||
|
|
||
| @durable_execution | ||
| def handler(_event: Any, _context: DurableContext) -> str: | ||
| """Simple hello world durable function.""" | ||
| return "Hello World!" | ||
| def handler(event: Any, context: DurableContext) -> dict[str, Any]: | ||
| """Durable Lambda handler with steps, waits, and logging. | ||
|
|
||
| Args: | ||
| event: Lambda event input | ||
| context: Durable execution context | ||
|
|
||
| Returns: | ||
| Response dictionary with statusCode and body | ||
| """ | ||
| # Execute Step #1 - logs a message | ||
| context.step(step_1()) | ||
|
|
||
| # Pause for 10 seconds without consuming CPU cycles or incurring usage charges | ||
| # The execution will suspend here and resume after 10 seconds | ||
| context.wait(Duration.from_seconds(10)) | ||
|
|
||
| context.logger.info("Waited for 10 seconds") | ||
|
|
||
| # Execute Step #2 - returns a message with status code | ||
| message = context.step(step_2(status_code=200)) | ||
|
|
||
| # Return response | ||
| return { | ||
| "statusCode": 200, | ||
| "body": message, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
examples/test/wait_for_callback/test_wait_for_callback_failure.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import pytest | ||
| from aws_durable_execution_sdk_python.execution import InvocationStatus | ||
| from aws_durable_execution_sdk_python.lambda_service import ErrorObject | ||
|
|
||
| from src.wait_for_callback import wait_for_callback | ||
|
|
||
|
|
||
| @pytest.mark.example | ||
| @pytest.mark.durable_execution( | ||
| handler=wait_for_callback.handler, | ||
| lambda_function_name="Wait For Callback Failure", | ||
| ) | ||
| def test_wait_for_callback_failure(durable_runner): | ||
| with durable_runner: | ||
| execution_arn = durable_runner.run_async(input="test", timeout=30) | ||
| callback_id = durable_runner.wait_for_callback(execution_arn=execution_arn) | ||
| durable_runner.send_callback_failure( | ||
| callback_id=callback_id, error=ErrorObject.from_message("my callback error") | ||
| ) | ||
| result = durable_runner.wait_for_result(execution_arn=execution_arn) | ||
|
|
||
| assert result.status is InvocationStatus.FAILED | ||
| assert isinstance(result.error, ErrorObject) | ||
| assert result.error.to_dict() == { | ||
| "ErrorMessage": "my callback error", | ||
| "ErrorType": "CallableRuntimeError", | ||
| } |
25 changes: 25 additions & 0 deletions
25
examples/test/wait_for_callback/test_wait_for_callback_success.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import pytest | ||
| from aws_durable_execution_sdk_python.execution import InvocationStatus | ||
|
|
||
| from src.wait_for_callback import wait_for_callback | ||
| from test.conftest import deserialize_operation_payload | ||
|
|
||
|
|
||
| @pytest.mark.example | ||
| @pytest.mark.durable_execution( | ||
| handler=wait_for_callback.handler, | ||
| lambda_function_name="Wait For Callback Success", | ||
| ) | ||
| def test_wait_for_callback_success(durable_runner): | ||
| with durable_runner: | ||
| execution_arn = durable_runner.run_async(input="test", timeout=30) | ||
| callback_id = durable_runner.wait_for_callback(execution_arn=execution_arn) | ||
| durable_runner.send_callback_success( | ||
| callback_id=callback_id, result="callback success".encode() | ||
| ) | ||
| result = durable_runner.wait_for_result(execution_arn=execution_arn) | ||
| assert result.status is InvocationStatus.SUCCEEDED | ||
| assert ( | ||
| deserialize_operation_payload(result.result) | ||
| == "External system result: callback success" | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.