-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathapp.py
More file actions
32 lines (23 loc) · 777 Bytes
/
app.py
File metadata and controls
32 lines (23 loc) · 777 Bytes
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
import restate
import logging
logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] [%(process)d] [%(levelname)s] - %(message)s",
)
logger = logging.getLogger(__name__)
product_service = restate.VirtualObject("product")
@product_service.handler()
async def reserve(ctx: restate.ObjectContext) -> bool:
if await ctx.get("reserved", type_hint=bool):
logging.info(f"Product already reserved {ctx.key()}")
return False
logging.info(f"Reserving product {ctx.key()}")
ctx.set("reserved", True)
return True
app = restate.app([product_service])
if __name__ == "__main__":
import hypercorn
import asyncio
conf = hypercorn.Config()
conf.bind = ["0.0.0.0:9080"]
asyncio.run(hypercorn.asyncio.serve(app, conf))