-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathapp.py
57 lines (39 loc) · 1.76 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import restate
from pydantic import BaseModel
from restate.exceptions import TerminalError
class LocationUpdate(BaseModel):
location: str
timestamp: str
class PackageInfo(BaseModel):
final_destination: str
locations: list[LocationUpdate] = []
# Package tracking system:
# Digital twin representing a package in delivery with real-time location updates.
# Handlers get called over HTTP or Kafka.
package_tracker = restate.VirtualObject("package-tracker")
# Called first by the seller over HTTP
@package_tracker.handler("registerPackage")
async def register_package(ctx: restate.ObjectContext, package_info: PackageInfo):
# store in state the user's information as coming from the registration event
ctx.set("package-info", package_info)
# Connected to a Kafka topic for real-time location updates
@package_tracker.handler("updateLocation")
async def update_location(ctx: restate.ObjectContext, location_update: LocationUpdate):
# get the package info from the state
package_info = await ctx.get("package-info", type_hint=PackageInfo)
if package_info is None:
raise TerminalError(f"Package {ctx.key()} not found")
# Update the package details in the state
package_info.locations.append(location_update)
ctx.set("package-info", package_info)
# Called by the delivery dashboard to get the package details
@package_tracker.handler("getPackageInfo", kind="shared")
async def get_package_info(ctx: restate.ObjectSharedContext) -> PackageInfo:
return await ctx.get("package-info", type_hint=PackageInfo)
app = restate.app(services=[package_tracker])
if __name__ == "__main__":
import hypercorn
import asyncio
conf = hypercorn.Config()
conf.bind = ["0.0.0.0:9080"]
asyncio.run(hypercorn.asyncio.serve(app, conf))