Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion examples/random_greeter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
#
"""example.py"""
from datetime import datetime

# pylint: disable=C0116
# pylint: disable=W0613

Expand All @@ -33,7 +35,21 @@ async def greet(ctx: Context, name: str) -> str:
# As with ctx.random(), this won't write entries in the journal
random_uuid = ctx.uuid()

# To get a timestamp, use ctx.time()
# This will record the timestamp in the Restate journal
now = await ctx.time()

# You can convert it to date/datetime using Python's standard library functions, e.g.
now_datetime = datetime.fromtimestamp(now)

# Or to perform a difference:
# start = await ctx.time()
# # Some code
# end = await ctx.time()
# delta = datetime.timedelta(seconds=(end-start))

return (f"Hello {name} with "
f"random number {random_number}, "
f"random bytes {random_bytes!r} "
f"and uuid {random_uuid}!")
f"random uuid {random_uuid},"
f"now datetime {now_datetime}!")
8 changes: 8 additions & 0 deletions python/restate/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ def uuid(self) -> UUID:
This UUID will be stable across retries and replays.
"""

@abc.abstractmethod
def time(self) -> RestateDurableFuture[float]:
"""
Returns the result of time.time(), durably recorded in the journal.

This timestamp will be stable across retries and replays.
"""

@typing_extensions.deprecated("`run` is deprecated, use `run_typed` instead for better type safety")
@overload
@abc.abstractmethod
Expand Down
4 changes: 4 additions & 0 deletions python/restate/server_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import typing
import traceback
from uuid import UUID
import time

from restate.context import DurablePromise, AttemptFinishedEvent, HandlerType, ObjectContext, Request, RestateDurableCallFuture, RestateDurableFuture, RunAction, SendHandle, RestateDurableSleepFuture, RunOptions, P
from restate.exceptions import TerminalError
Expand Down Expand Up @@ -480,6 +481,9 @@ def random(self) -> Random:
def uuid(self) -> UUID:
return UUID(int=self.random_instance.getrandbits(128), version=4)

def time(self) -> RestateDurableFuture[float]:
return self.run_typed("timestamp", time.time)

# pylint: disable=R0914
async def create_run_coroutine(self,
handle: int,
Expand Down