Repository navigation
feat(sdk): add experimental on_pause hook to steps - #1008
Merged
Merged
Conversation
A step can pause the execution on a failed attempt, and the invocation then ends. So no code can act on the pause. StepConfig.on_pause runs after the PauseDurableExecution call succeeded and before the invocation ends. It gets a StepPauseInfo with the execution ARN, the step id and name, the failed attempt and its error. WaitForCallbackConfig.on_pause passes the hook to the submitter step. An error from the hook is logged and the execution stays paused. The hook is not checkpointed and does not run on replay. Add an example that sends the execution ARN to an SQS queue.
The README example used the default max_attempts of 3 with pause_every=5, so it never paused. Add a test for the warning of an unnamed step whose on_pause raises.
Add a test that a pause without on_pause logs no warning. Say in the example that the queue and PAUSED_QUEUE_URL are set up by the user.
on_pause runs while the status is PAUSING, and a resume in that status cancels the pause. Say so in the API docs, the README and the example, and list the SQS permissions of the sample consumer.
The status can still be RUNNING when on_pause runs, so a resume can also fail with ResourceConflictException. The sample consumer now handles one message per batch.
Wait for the paused invocation to end and check that attempt 2 does not run before the resume.
This comment has been minimized.
This comment has been minimized.
A fixed 0.3 s sleep could resume while the first invocation was still running. The test now waits for InvocationCompleted before it checks that the execution stays paused.
Contributor
Codex AI reviewOne previously reported approach finding remains at Suppressed repeats
Reviewed commit |
This branch was successfully deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Issue #, if available
None.
Description of changes
A step can pause the execution on a failed attempt (
RetryDecision.pause,pause_every). After the pause, the invocation ends. So no later step and no code after the step can run, and a developer cannot act on the pause from code. EventBridgePAUSEDevents carry no step name or reason, so they cannot target one step.This PR adds an experimental
on_pauseoption:StepConfig.on_pause: a sync callable(StepPauseInfo) -> None. The SDK calls it after it paused the execution.StepPauseInfo:durable_execution_arn,step_id,step_name(None for an unnamed step),attempt(the failed attempt) anderror(the error of that attempt).WaitForCallbackConfig.on_pause: passed to the submitter step, because the submitter step can pause throughretry_strategy.StepPauseInfoare marked "Experimental; may change or be removed in a future release.", like pause.Behavior:
on_pauseonly after thePauseDurableExecutioncall succeeded, and before the invocation ends. The SDK waits for it.should_retryis False.on_pauseraises, the SDK logs a warning through the context logger, and the invocation ends with the execution paused. The error does not fail the step.on_pauseis not checkpointed and does not run on replay. So it runs at most once per pause. Its work can be lost if the invocation crashes or times out during the call.Docs: the
StepConfigdocstring and the README say the hook runs at most once, point to the EventBridgeDurable Execution Status Changeevent with statusPAUSEDas a backup signal, and say the execution role needs permission for what the hook calls (for examplesqs:SendMessage).Example:
step_with_on_pausesends the execution ARN to an SQS queue fromon_pause. Itsresume_handleris a sample queue consumer that callsResumeDurableExecution. The catalog entry hasintegration: false, likestep_with_pause_every, because the example needs a queue that the user creates.Tests:
tests/operation/step_on_pause_test.py: the hook gets the right info after a pause, and runs after the pause call. It does not run when the pause fails, when the client cannot pause, without a pause, whenshould_retryis False, or on replay (pending retry and succeeded step). A raising hook is logged and the execution stays paused (named and unnamed step). A pause without a hook logs no warning.wait_for_callbackpasses the hook to the submitter step.test/step/test_step_with_on_pause.py: the local test runner pauses the execution and calls the hook once with the execution ARN. Attempt 2 does not run until the test resumes the execution. The testing package needed no change, because its client already supports pause.Review rounds (independent reviewer, given only the spec and the diff):
max_attempts=3withpause_every=5), and the warning of an unnamed step had no test. Both fixed.on_pause is not Nonecheck had no test that fails without it. Fixed with a test. The example now says the user creates the queue and setsPAUSED_QUEUE_URL.PAUSINGduring the hook. The Lambda docs say it can still beRUNNING. The text now says a resume while the invocation runs can cancel the pause or fail withResourceConflictException. The sample consumer now handles one message per batch.Design
See Rules for every change.
onPausehook (owner: the maintainer who asked for this change). Its problem, behavior rules and tests are the "Description of changes" above.PAUSEDevent: the event carries no step name or reason, so it cannot target one step.operation/step.py, +1 line inoperation/callback.py, +54 lines inconfig.py(mostly docstrings). Public API: +1 class (StepPauseInfo), +2 fields (StepConfig.on_pause,WaitForCallbackConfig.on_pause).Assumptions
ResumeDurableExecutioncall while the paused invocation still runs can cancel the pause or returnResourceConflictException. Evidence: Pausing and resuming durable executions.Decisions not covered by the issue or the design note
StepPauseInfolives inconfig.pynext toStepConfig, and the root package does not export it, likeStepConfig.on_pauseis keyword-only on both configs, so the positional order of the existing fields does not change.Exception, notBaseException, like the pause-failure path.Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.