
- Implemented APScheduler for automatic uptime checks - Each monitor runs on its own configured interval (default 5 minutes) - Scheduler starts automatically on application startup - Real-time job management when monitors are created/updated/deleted - Added scheduler status and control endpoints - Background logging of all check results - Updated documentation with scheduling features
69 lines
1.6 KiB
Python
69 lines
1.6 KiB
Python
import logging
|
|
from contextlib import asynccontextmanager
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from app.db.session import engine
|
|
from app.db.base import Base
|
|
from app.routers import monitors, checks, scheduler
|
|
from app.services.scheduler import uptime_scheduler
|
|
|
|
# Configure logging
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Create database tables
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# Startup
|
|
logger.info("Starting Uptime Monitoring API...")
|
|
uptime_scheduler.start()
|
|
yield
|
|
# Shutdown
|
|
logger.info("Shutting down Uptime Monitoring API...")
|
|
uptime_scheduler.stop()
|
|
|
|
|
|
app = FastAPI(
|
|
title="Uptime Monitoring API",
|
|
description="API for monitoring website/endpoint uptime and performance",
|
|
version="1.0.0",
|
|
openapi_url="/openapi.json",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Include routers
|
|
app.include_router(monitors.router, prefix="/api/v1")
|
|
app.include_router(checks.router, prefix="/api/v1")
|
|
app.include_router(scheduler.router, prefix="/api/v1")
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {
|
|
"title": "Uptime Monitoring API",
|
|
"documentation": "/docs",
|
|
"health_check": "/health",
|
|
}
|
|
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
return {"status": "healthy", "service": "uptime-monitoring-api"}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|