Skip to content

Commit

Permalink
Remove Continue-As-New from the sample
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Oct 4, 2024
1 parent c68fc78 commit 1ea0905
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
11 changes: 6 additions & 5 deletions message_passing/waiting_for_handlers_and_compensation/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Waiting for message handlers, and performing compensation and cleanup in message handlers

This sample demonstrates the following recommended practices:
This sample demonstrates how to do the following:

1. Ensure that all update/signal handlers are finished before a successful
workflow return, and on workflow cancellation and failure.
2. Perform compensation/cleanup in an update handler when the workflow is
cancelled or fails.

1. Ensuring that all signal and update handlers are finished before a successful
workflow return, and on workflow failure, cancellation, and continue-as-new.
2. Performing necessary compensation/cleanup in an update handler when the
workflow is cancelled, fails, or continues-as-new.


To run, open two terminals and `cd` to this directory in them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class WorkflowExitType(IntEnum):
SUCCESS = 0
FAILURE = 1
CANCELLATION = 2
CONTINUE_AS_NEW = 3


@dataclass
Expand Down
22 changes: 10 additions & 12 deletions message_passing/waiting_for_handlers_and_compensation/starter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ async def _check_run(
wait_for_stage=client.WorkflowUpdateStage.ACCEPTED,
)
except Exception as e:
print(f" 🔴 caught exception while starting update: {e}: {e.__cause__ or ''}")
print(
f" 🔴 caught exception while starting update: {e}: {e.__cause__ or ''}"
)

if exit_type == WorkflowExitType.CANCELLATION:
await wf_handle.cancel()
Expand All @@ -48,24 +50,20 @@ async def _check_run(
f" 🔴 caught exception while waiting for update result: {e}: {e.__cause__ or ''}"
)

if exit_type == WorkflowExitType.CONTINUE_AS_NEW:
await _check_run(wf_handle, WorkflowExitType.SUCCESS)
else:
try:
await wf_handle.result()
print(" 🟢 caller received workflow result")
except Exception as e:
print(
f" 🔴 caught exception while waiting for workflow result: {e}: {e.__cause__ or ''}"
)
try:
await wf_handle.result()
print(" 🟢 caller received workflow result")
except Exception as e:
print(
f" 🔴 caught exception while waiting for workflow result: {e}: {e.__cause__ or ''}"
)


async def main():
for exit_type in [
WorkflowExitType.SUCCESS,
WorkflowExitType.FAILURE,
WorkflowExitType.CANCELLATION,
WorkflowExitType.CONTINUE_AS_NEW,
]:
print(f"\n\nworkflow exit type: {exit_type.name}")
await starter(exit_type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class WaitingForHandlersAndCompensationWorkflow:
perform compensation/cleanup:
1. It ensures that all signal and update handlers have finished before a
successful return, and on failure, cancellation, and continue-as-new.
successful return, and on failure and cancellation.
2. The update handler performs any necessary compensation/cleanup when the
workflow is cancelled, fails, or continues-as-new.
workflow is cancelled or fails.
"""

def __init__(self) -> None:
Expand Down Expand Up @@ -151,8 +151,6 @@ async def _run(self, input: WorkflowInput) -> str:
await workflow.wait_condition(lambda: self._update_started)
if input.exit_type == WorkflowExitType.SUCCESS:
return "workflow-result"
elif input.exit_type == WorkflowExitType.CONTINUE_AS_NEW:
workflow.continue_as_new(WorkflowInput(exit_type=WorkflowExitType.SUCCESS))
elif input.exit_type == WorkflowExitType.FAILURE:
raise exceptions.ApplicationError("deliberately failing workflow")
elif input.exit_type == WorkflowExitType.CANCELLATION:
Expand All @@ -164,7 +162,4 @@ async def _run(self, input: WorkflowInput) -> str:
def is_workflow_exit_exception(e: BaseException) -> bool:
# 👉 If you have set additional failure_exception_types you should also
# check for these here.
return isinstance(
e,
(asyncio.CancelledError, workflow.ContinueAsNewError, exceptions.FailureError),
)
return isinstance(e, (asyncio.CancelledError, exceptions.FailureError))

0 comments on commit 1ea0905

Please sign in to comment.