-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpackage_tracker.py
55 lines (38 loc) · 1.72 KB
/
package_tracker.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
from typing import List
import restate
from pydantic import BaseModel
from restate import VirtualObject, ObjectContext
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 = VirtualObject("package-tracker")
# Called first by the seller over HTTP
@package_tracker.handler("registerPackage")
async def register_package(ctx: ObjectContext, package_info: PackageInfo):
# store in state the user's information as coming from the registration event
ctx.set("package-info", package_info.model_dump())
# Connected to a Kafka topic for real-time location updates
@package_tracker.handler("updateLocation")
async def update_location(ctx: ObjectContext, location_update: LocationUpdate):
# get the package info from the state
package_info = PackageInfo(**await ctx.get("package-info"))
if package_info is None:
raise TerminalError(f"Package {ctx.key()} not found")
# Update the package details in the state
locations = package_info.locations or []
locations.append(location_update)
package_info.locations = locations
# store the updated package info in state
ctx.set("package-info", package_info.model_dump())
# Called by the delivery dashboard to get the package details
@package_tracker.handler("getPackageInfo")
async def get_package_info(ctx: ObjectContext) -> PackageInfo:
return PackageInfo(**await ctx.get("package-info"))
app = restate.app(services=[package_tracker])