Stabilize staff concurrency and premium emoji; enable verified Drone deployment
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
30
src/core/health.py
Normal file
30
src/core/health.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""Health reflects a live event loop and a usable database connection."""
|
||||
import asyncio
|
||||
import os
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from sqlalchemy import text
|
||||
|
||||
from .database import async_session_maker
|
||||
|
||||
HEARTBEAT = Path(os.getenv("HEARTBEAT_FILE", "/tmp/lottery-heartbeat"))
|
||||
|
||||
|
||||
async def heartbeat():
|
||||
while True:
|
||||
async with async_session_maker() as session:
|
||||
await session.execute(text("SELECT 1"))
|
||||
HEARTBEAT.write_text(str(time.time()), encoding="ascii")
|
||||
await asyncio.sleep(10)
|
||||
|
||||
|
||||
def healthy():
|
||||
try:
|
||||
return 0 <= time.time() - float(HEARTBEAT.read_text(encoding="ascii")) < 60
|
||||
except (OSError, ValueError):
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(0 if healthy() else 1)
|
||||
Reference in New Issue
Block a user