FastAPI lifespan에서 watcher_loop 스폰. /health. tzdata(zoneinfo Asia/Seoul). .env: REDIS_URL, STOCK_BASE_URL, TRADING_START/END. Plan-B-Infra Phase 2. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
37 lines
874 B
Python
37 lines
874 B
Python
"""task-watcher FastAPI entry — health + lifespan (watcher loop spawn)."""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
|
|
import watcher
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(name)s %(levelname)s %(message)s")
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
watcher_task = asyncio.create_task(watcher.watcher_loop())
|
|
logger.info("task-watcher lifespan 시작")
|
|
try:
|
|
yield
|
|
finally:
|
|
watcher_task.cancel()
|
|
try:
|
|
await watcher_task
|
|
except asyncio.CancelledError:
|
|
pass
|
|
logger.info("task-watcher lifespan 종료")
|
|
|
|
|
|
app = FastAPI(lifespan=lifespan)
|
|
|
|
|
|
@app.get("/health")
|
|
def health():
|
|
return {"ok": True, "service": "task-watcher"}
|