-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
74 lines (61 loc) · 2.11 KB
/
main.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from fastapi import Request
from bootstrap.application import create_app
from config.config import settings
import uvicorn
import requests
import sched
import time
from fastapi.responses import JSONResponse
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from slurm import slurm_auth
from bootstrap.scheduler import create_scheduler
app = create_app()
scheduler = create_scheduler()
# start up a timer task to sync users
@app.on_event("startup")
async def run_scheduler():
scheduler.start()
@app.get("/")
async def root():
return "OpenCSG starcloud API list"
async def _reverse_proxy(request: Request):
session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount("http://", adapter)
session.mount("https://", adapter)
await slurm_auth.verify(request)
# note this is required to call slurm API
slurm_auth.add_slurm_header(request)
try:
if request.method == "POST" or request.method == "PATCH":
content = await request.json()
r = session.request(
request.method,
json=content,
headers=request.headers,
url=settings.SLURM_API + request.url.path + "?" + request.url.query,
)
else:
r = session.request(
request.method,
headers=request.headers,
url=settings.SLURM_API + request.url.path + "?" + request.url.query,
)
except requests.exceptions.ConnectionError:
return JSONResponse(None, status_code=500)
return JSONResponse(r.json(), status_code=r.status_code)
# forward all slurm api to slurm server
app.add_route(
"/slurm/{full_path:path}", _reverse_proxy, [
"GET", "OPTIONS", "POST", "PATCH", "DELETE"]
)
# forward all slurm api to slurm server
app.add_route(
"/slurmdb/{full_path:path}", _reverse_proxy, [
"GET", "OPTIONS", "POST", "PATCH", "DELETE"]
)
if __name__ == "__main__":
uvicorn.run(app="main:app", host=settings.SERVER_HOST,
port=settings.SERVER_PORT)