self ping lifespan
Browse files
fetch.py
CHANGED
@@ -1,8 +1,12 @@
|
|
|
|
|
|
|
|
1 |
from datetime import datetime
|
2 |
from fastapi import FastAPI, Request
|
3 |
from fastapi.responses import Response, FileResponse
|
4 |
from typing import Any
|
5 |
|
|
|
6 |
import json
|
7 |
import subprocess
|
8 |
|
@@ -14,7 +18,14 @@ class PrettyJSONResponse(Response):
|
|
14 |
return json.dumps(content, indent=2).encode("utf-8")
|
15 |
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
|
20 |
@app.middleware("http")
|
@@ -62,3 +73,22 @@ def get_analytics(n: int = 5):
|
|
62 |
@app.api_route("/qa", response_class=FileResponse, methods=["GET", "HEAD"])
|
63 |
def query_analytics():
|
64 |
return "a.json"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from apscheduler.executors.asyncio import AsyncIOExecutor
|
2 |
+
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
3 |
+
from contextlib import asynccontextmanager
|
4 |
from datetime import datetime
|
5 |
from fastapi import FastAPI, Request
|
6 |
from fastapi.responses import Response, FileResponse
|
7 |
from typing import Any
|
8 |
|
9 |
+
import httpx
|
10 |
import json
|
11 |
import subprocess
|
12 |
|
|
|
18 |
return json.dumps(content, indent=2).encode("utf-8")
|
19 |
|
20 |
|
21 |
+
@asynccontextmanager
|
22 |
+
async def lifespan(app: FastAPI):
|
23 |
+
scheduler.start()
|
24 |
+
yield
|
25 |
+
scheduler.shutdown()
|
26 |
+
|
27 |
+
|
28 |
+
app = FastAPI(lifespan=lifespan)
|
29 |
|
30 |
|
31 |
@app.middleware("http")
|
|
|
73 |
@app.api_route("/qa", response_class=FileResponse, methods=["GET", "HEAD"])
|
74 |
def query_analytics():
|
75 |
return "a.json"
|
76 |
+
|
77 |
+
|
78 |
+
@app.get("/favicon.ico")
|
79 |
+
async def favicon():
|
80 |
+
return {"message": "woof!"}
|
81 |
+
|
82 |
+
|
83 |
+
@app.get("/ping")
|
84 |
+
async def ping():
|
85 |
+
return {"message": "woof!"}
|
86 |
+
|
87 |
+
|
88 |
+
async def self_ping():
|
89 |
+
async with httpx.AsyncClient() as client:
|
90 |
+
_ = await client.get("http://0.0.0.0:7860/ping")
|
91 |
+
|
92 |
+
|
93 |
+
scheduler = AsyncIOScheduler(executors={"default": AsyncIOExecutor()})
|
94 |
+
scheduler.add_job(self_ping, "interval", minutes=60)
|