-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
27 lines (20 loc) · 788 Bytes
/
app.py
File metadata and controls
27 lines (20 loc) · 788 Bytes
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
import celery.states as states
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from worker import celery
app = FastAPI()
@app.get("/add/{param1}/{param2}", response_class=HTMLResponse)
async def add(param1: int, param2: int) -> str:
task = celery.send_task('tasks.add', args=[param1, param2], kwargs={})
response = f"<a href='{app.url_path_for('check_task', task_id=task.id)}'>check status of {task.id} </a>"
return response
@app.get("/check/{task_id}", response_class=HTMLResponse)
async def check_task(task_id: str) -> str:
res = celery.AsyncResult(task_id)
if res.state == states.PENDING:
return res.state
else:
return str(res.result)
@app.get("/health_check")
async def health_check():
return {"Status": "Ok"}