-
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
base: nafees/go-sdk-memo
Are you sure you want to change the base?
Changes from 2 commits
e3db009
ead042b
1a0aac2
1f38041
0591206
ae08540
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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, | ||
|
|
@@ -504,3 +515,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 json.loads(resp.data) # type: ignore[no-any-return] | ||
|
||
|
|
||
| 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) # type: ignore[no-any-return] | ||
|
|
||
| result = await fn() | ||
|
|
||
| data = 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.
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