Skip to content

Commit d842dad

Browse files
committed
Add top level sleep fn
1 parent d5cc5d3 commit d842dad

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

temporalio/worker/_workflow_instance.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,20 @@ def workflow_upsert_search_attributes(
14391439
else [update.value]
14401440
)
14411441

1442+
async def workflow_sleep(
1443+
self, duration: float, *, summary: Optional[str] = None
1444+
) -> None:
1445+
user_metadata = (
1446+
temporalio.api.sdk.v1.UserMetadata(
1447+
summary=self._payload_converter.to_payload(summary)
1448+
)
1449+
if summary
1450+
else None
1451+
)
1452+
self._timer_impl(
1453+
duration, lambda _: None, options=_TimerOptions(user_metadata=user_metadata)
1454+
)
1455+
14421456
async def workflow_wait_condition(
14431457
self,
14441458
fn: Callable[[], bool],

temporalio/workflow.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,11 @@ def workflow_upsert_search_attributes(
750750
],
751751
) -> None: ...
752752

753+
@abstractmethod
754+
async def workflow_sleep(
755+
self, duration: float, *, summary: Optional[str] = None
756+
) -> None: ...
757+
753758
@abstractmethod
754759
async def workflow_wait_condition(
755760
self,
@@ -1071,15 +1076,6 @@ def update(
10711076
]: ...
10721077

10731078

1074-
@overload
1075-
def update(
1076-
*, description: str
1077-
) -> Callable[
1078-
[Callable[MultiParamSpec, ReturnType]],
1079-
UpdateMethodMultiParam[MultiParamSpec, ReturnType],
1080-
]: ...
1081-
1082-
10831079
def update(
10841080
fn: Optional[CallableSyncOrAsyncType] = None,
10851081
*,
@@ -1170,6 +1166,24 @@ def uuid4() -> uuid.UUID:
11701166
return uuid.UUID(bytes=random().getrandbits(16 * 8).to_bytes(16, "big"), version=4)
11711167

11721168

1169+
async def sleep(
1170+
duration: Union[float, timedelta], *, summary: Optional[str] = None
1171+
) -> None:
1172+
"""Sleep for the given duration.
1173+
1174+
Args:
1175+
duration: Duration to sleep in seconds or as a timedelta.
1176+
summary: A single-line fixed summary for this timer that may appear in UI/CLI.
1177+
This can be in single-line Temporal markdown format.
1178+
"""
1179+
await _Runtime.current().workflow_sleep(
1180+
duration=duration.total_seconds()
1181+
if isinstance(duration, timedelta)
1182+
else duration,
1183+
summary=summary,
1184+
)
1185+
1186+
11731187
async def wait_condition(
11741188
fn: Callable[[], bool],
11751189
*,
@@ -1992,7 +2006,7 @@ def start_activity(
19922006
need to. Contact Temporal before setting this value.
19932007
versioning_intent: When using the Worker Versioning feature, specifies whether this Activity
19942008
should run on a worker with a compatible Build Id or not.
1995-
summary: Gets or sets a single-line fixed summary for this activity that may appear in UI/CLI.
2009+
summary: A single-line fixed summary for this activity that may appear in UI/CLI.
19962010
This can be in single-line Temporal markdown format.
19972011
19982012
Returns:

tests/worker/test_workflow.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6195,6 +6195,7 @@ async def run(self) -> None:
61956195
raise RuntimeError("Expected timeout")
61966196
except asyncio.TimeoutError:
61976197
pass
6198+
await workflow.sleep(0.01, summary="timer2")
61986199
self._waiting = True
61996200
workflow.set_current_details("such detail")
62006201
await workflow.wait_condition(lambda: self._done)
@@ -6263,6 +6264,7 @@ async def waiting() -> bool:
62636264
execution=WorkflowExecution(workflow_id=handle.id),
62646265
)
62656266
)
6267+
timer_summs = set()
62666268
for event in resp.history.events:
62676269
if event.event_type == EventType.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED:
62686270
assert "cool workflow bro" in PayloadConverter.default.from_payload(
@@ -6276,6 +6278,7 @@ async def waiting() -> bool:
62766278
event.user_metadata.summary
62776279
)
62786280
elif event.event_type == EventType.EVENT_TYPE_TIMER_STARTED:
6279-
assert "hi!" in PayloadConverter.default.from_payload(
6280-
event.user_metadata.summary
6281+
timer_summs.add(
6282+
PayloadConverter.default.from_payload(event.user_metadata.summary)
62816283
)
6284+
assert timer_summs == {"hi!", "timer2"}

0 commit comments

Comments
 (0)