-
Notifications
You must be signed in to change notification settings - Fork 304
Attempt to add memo functions to durable context for TS and Py SDKs #2921
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
Draft
mnafees
wants to merge
6
commits into
nafees/go-sdk-memo
Choose a base branch
from
nafees/ts-py-sdks-memo
base: nafees/go-sdk-memo
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e3db009
attempt to add memo to durable context for TS and Py SDKs
mnafees ead042b
address PR comments
mnafees 1a0aac2
Merge branch 'nafees/go-sdk-memo' into nafees/ts-py-sdks-memo
mnafees 1f38041
Merge branch 'nafees/go-sdk-memo' into nafees/ts-py-sdks-memo
mnafees 0591206
fix to_thread
mnafees ae08540
Merge branch 'nafees/go-sdk-memo' into nafees/ts-py-sdks-memo
mnafees 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
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,7 +1,9 @@ | ||
| import asyncio | ||
| import hashlib | ||
| import json | ||
| from collections.abc import Awaitable, Callable | ||
| from datetime import timedelta | ||
| from typing import TYPE_CHECKING, Any, cast | ||
| from typing import TYPE_CHECKING, Any, TypeVar, cast | ||
| from warnings import warn | ||
|
|
||
| from hatchet_sdk.clients.admin import AdminClient | ||
|
|
@@ -28,11 +30,20 @@ | |
| from hatchet_sdk.utils.typing import JSONSerializableMapping, LogLevel | ||
| from hatchet_sdk.worker.runner.utils.capture_logs import AsyncLogSender, LogRecord | ||
|
|
||
| TMemo = TypeVar("TMemo") | ||
|
|
||
| if TYPE_CHECKING: | ||
| from hatchet_sdk.runnables.task import Task | ||
| from hatchet_sdk.runnables.types import R, TWorkflowInput | ||
|
|
||
|
|
||
| def _compute_memo_key(step_name: str, deps: list[Any]) -> str: | ||
| h = hashlib.sha256() | ||
| h.update(step_name.encode()) | ||
| h.update(json.dumps(deps, default=str).encode()) | ||
| return h.hexdigest() | ||
|
|
||
|
|
||
| class Context: | ||
| def __init__( | ||
| self, | ||
|
|
@@ -520,3 +531,59 @@ async def aio_sleep_for(self, duration: Duration) -> dict[str, Any]: | |
| f"sleep:{timedelta_to_expr(duration)}-{wait_index}", | ||
| SleepCondition(duration=duration), | ||
| ) | ||
|
|
||
| def memo(self, fn: Callable[[], TMemo], deps: list[Any]) -> TMemo: | ||
| if self.durable_event_listener is None: | ||
| raise ValueError("Durable event listener is not available") | ||
|
|
||
| key = _compute_memo_key(self.action.action_id, deps) | ||
|
|
||
| resp = self.durable_event_listener.get_durable_event_log( | ||
| external_id=self.workflow_run_id, | ||
| key=key, | ||
| ) | ||
|
|
||
| if resp.found: | ||
| return cast(TMemo, json.loads(resp.data)) | ||
|
|
||
| result = fn() | ||
|
|
||
| data = json.dumps(result).encode() | ||
|
|
||
| self.durable_event_listener.create_durable_event_log( | ||
| external_id=self.workflow_run_id, | ||
| key=key, | ||
| data=data, | ||
| ) | ||
|
Comment on lines
+551
to
+557
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and |
||
|
|
||
| return result | ||
|
|
||
| async def aio_memo( | ||
| self, fn: Callable[[], Awaitable[TMemo]], deps: list[Any] | ||
| ) -> TMemo: | ||
| if self.durable_event_listener is None: | ||
| raise ValueError("Durable event listener is not available") | ||
|
|
||
| key = _compute_memo_key(self.action.action_id, deps) | ||
|
|
||
| resp = await asyncio.to_thread( | ||
| self.durable_event_listener.get_durable_event_log, | ||
| external_id=self.workflow_run_id, | ||
| key=key, | ||
| ) | ||
|
|
||
| if resp.found: | ||
| return await asyncio.to_thread(json.loads, resp.data) | ||
|
|
||
| result = await fn() | ||
|
|
||
| data = (await asyncio.to_thread(json.dumps, result)).encode() | ||
|
|
||
| await asyncio.to_thread( | ||
| self.durable_event_listener.create_durable_event_log, | ||
| external_id=self.workflow_run_id, | ||
| key=key, | ||
| data=data, | ||
| ) | ||
|
|
||
| return result | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this needs to be wrapped in
asyncio.to_threadso it doesn't block