Merged
Conversation
cretz
reviewed
Mar 31, 2025
Member
cretz
left a comment
There was a problem hiding this comment.
May also want a simple test on this one
Member
There was a problem hiding this comment.
Like our other samples w/ no special install instructions, may want to link to top-level README for "prerequisites"
updatable_timer/workflow.py
Outdated
Comment on lines
9
to
27
| @workflow.defn | ||
| class Workflow: | ||
| def __init__(self): | ||
| self.timer: Optional[UpdatableTimer] = None | ||
|
|
||
| @workflow.run | ||
| async def run(self, wake_up_time: float): | ||
| self.timer = UpdatableTimer( | ||
| datetime.fromtimestamp(wake_up_time, tz=timezone.utc) | ||
| ) | ||
| await self.timer.sleep() | ||
|
|
||
| @workflow.signal | ||
| async def update_wake_up_time(self, wake_up_time: float): | ||
| # Deals with situation when the signal method is called before the run method. | ||
| # This happens when a workflow task is executed after a signal is received | ||
| # or when a workflow is started using the signal-with-start. | ||
| await workflow.wait_condition(lambda: self.timer is not None) | ||
| assert self.timer is not None # for mypy |
Member
There was a problem hiding this comment.
We have a neat, fairly new feature for @workflow.init that lets you operate on workflow input on the constructor, so can change this to:
Suggested change
| @workflow.defn | |
| class Workflow: | |
| def __init__(self): | |
| self.timer: Optional[UpdatableTimer] = None | |
| @workflow.run | |
| async def run(self, wake_up_time: float): | |
| self.timer = UpdatableTimer( | |
| datetime.fromtimestamp(wake_up_time, tz=timezone.utc) | |
| ) | |
| await self.timer.sleep() | |
| @workflow.signal | |
| async def update_wake_up_time(self, wake_up_time: float): | |
| # Deals with situation when the signal method is called before the run method. | |
| # This happens when a workflow task is executed after a signal is received | |
| # or when a workflow is started using the signal-with-start. | |
| await workflow.wait_condition(lambda: self.timer is not None) | |
| assert self.timer is not None # for mypy | |
| @workflow.defn | |
| class Workflow: | |
| @workflow.init | |
| def __init__(self, wake_up_time: float) -> None: | |
| self.timer = UpdatableTimer( | |
| datetime.fromtimestamp(wake_up_time, tz=timezone.utc) | |
| ) | |
| @workflow.run | |
| async def run(self, wake_up_time: float) -> None: | |
| await self.timer.sleep() | |
| @workflow.signal | |
| async def update_wake_up_time(self, wake_up_time: float): |
(also can remove the assert self.timer is not None # for mypy in the query)
updatable_timer/workflow.py
Outdated
|
|
||
| @workflow.defn | ||
| class Workflow: | ||
| def __init__(self): |
Member
There was a problem hiding this comment.
Recommend return type annotations everywhere
Member
Author
There was a problem hiding this comment.
My Java brain protested this for a constructor :).
Refactored workflow to use workflow.init. Added unit test.
cretz
approved these changes
Apr 1, 2025
This was referenced Apr 1, 2025
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.
What was changed
Added updatable_timer sample based on the Java one.
Why?
It demonstrates how to use
workflow.wait_conditionwith a timeout. How to update the sleep interval is a FAQ.