This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
69 lines (52 loc) · 1.71 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
import threading
from argparse import ArgumentParser, FileType
import uvicorn
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from asr_worker.config import api_config
from asr_worker.mq_consumer import MQConsumer
from asr_worker.asr import ASR
parser = ArgumentParser()
parser.add_argument('--log-config', type=FileType('r'), default='logging/logging.ini',
help="Path to log config file.")
parser.add_argument('--port', type=int, default='8000',
help="Port used for healthcheck probes.")
args = parser.parse_args()
app = FastAPI()
mq_thread = threading.Thread()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
allow_credentials=True,
)
@app.on_event("startup")
async def startup():
global mq_thread
asr = ASR(api_config=api_config)
consumer = MQConsumer(asr=asr)
mq_thread = threading.Thread(target=consumer.start)
mq_thread.connected = False
mq_thread.consume = True
mq_thread.start()
@app.on_event("shutdown")
async def shutdown():
global mq_thread
mq_thread.consume = False
@app.get('/health/readiness')
@app.get('/health/startup')
async def health_check():
# Returns 200 if models are loaded and connection to RabbitMQ is up
global mq_thread
if not mq_thread.is_alive() or not getattr(mq_thread, "connected"):
raise HTTPException(500)
return "OK"
@app.get('/health/liveness')
async def liveness():
global mq_thread
if not mq_thread.is_alive():
raise HTTPException(500)
return "OK"
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=args.port, log_config=args.log_config.name)