mostly works!
This commit is contained in:
parent
43f29e865d
commit
fe4576315a
3 changed files with 33 additions and 39 deletions
|
@ -7,7 +7,7 @@ import logging
|
|||
import sys
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from sqlalchemy import (Column, DateTime, Integer, String, create_engine,
|
||||
from sqlalchemy import (Column, DateTime, Boolean, Integer, String, create_engine,
|
||||
inspect)
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
@ -28,6 +28,7 @@ class Rec(Base):
|
|||
starttime = Column(DateTime, nullable=True)
|
||||
endtime = Column(DateTime, nullable=True)
|
||||
filename = Column(String, nullable=True)
|
||||
ready = Column(Boolean, default=False)
|
||||
|
||||
def __init__(self, name="", starttime=None, endtime=None, filename=None):
|
||||
self.name = name
|
||||
|
|
|
@ -6,7 +6,7 @@ import os
|
|||
import unicodedata
|
||||
from datetime import datetime
|
||||
|
||||
from fastapi import FastAPI, HTTPException, Request, Response
|
||||
from fastapi import FastAPI, HTTPException, Request, Response, BackgroundTasks
|
||||
from fastapi.responses import FileResponse, RedirectResponse, JSONResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from pydantic import BaseModel, Field
|
||||
|
@ -140,16 +140,21 @@ class GenerateResponse(BaseModel):
|
|||
message: str
|
||||
|
||||
@app.post("/api/generate")
|
||||
async def generate(req: GenerateInfo, response: Response):
|
||||
async def generate(req: GenerateInfo, response: Response, background_tasks: BackgroundTasks):
|
||||
# prendiamo la rec in causa
|
||||
recid = req.id
|
||||
rec = db._search(_id=recid)[0]
|
||||
if rec.filename is not None and os.path.exists(rec.filename):
|
||||
rec = db._search(_id=req.id)[0]
|
||||
if rec.ready:
|
||||
return {
|
||||
"status": "ready",
|
||||
"message": "The file has already been generated at %s" % rec.filename,
|
||||
"rec": rec,
|
||||
}
|
||||
if rec.filename is not None:
|
||||
return {
|
||||
"status": "ready",
|
||||
"message": "Generating...",
|
||||
"rec": rec,
|
||||
}
|
||||
if (
|
||||
get_config()["FORGE_MAX_DURATION"] > 0
|
||||
and (rec.endtime - rec.starttime).total_seconds()
|
||||
|
@ -178,8 +183,9 @@ async def generate(req: GenerateInfo, response: Response):
|
|||
),
|
||||
}
|
||||
db.get_session(rec).commit()
|
||||
job_id = pq.submit(
|
||||
create_mp3,
|
||||
background_tasks.add_task(
|
||||
generate_mp3,
|
||||
db_id=req.id,
|
||||
start=rec.starttime,
|
||||
end=rec.endtime,
|
||||
outfile=os.path.join(get_config()["AUDIO_OUTPUT"], rec.filename),
|
||||
|
@ -189,47 +195,34 @@ async def generate(req: GenerateInfo, response: Response):
|
|||
"extra_tags": get_config()["TAG_EXTRA"],
|
||||
},
|
||||
)
|
||||
logger.debug("SUBMITTED: %d" % job_id)
|
||||
logger.debug("SUBMITTED: %d" % req.id)
|
||||
return rec_msg(
|
||||
"Aggiornamento completato!",
|
||||
job_id=job_id,
|
||||
job_id=rec.id,
|
||||
result="/output/" + rec.filename,
|
||||
rec=rec_sanitize(rec),
|
||||
)
|
||||
|
||||
def generate_mp3(db_id: int, **kwargs):
|
||||
'''creates and mark it as ready in the db'''
|
||||
result = create_mp3(**kwargs)
|
||||
logger.debug('Create mp3 for %d -> %s', db_id, result)
|
||||
rec = db._search(_id=db_id)[0]
|
||||
rec.ready = True
|
||||
db.get_session(rec).commit()
|
||||
|
||||
@app.get("/api/jobs/{job_id}")
|
||||
def check_job(job_id: int):
|
||||
try:
|
||||
job = check_job(job_id)
|
||||
except ValueError:
|
||||
abort(400, "job_id not valid")
|
||||
|
||||
|
||||
@app.get("/api/ready/{recid}")
|
||||
def check_job(recid: int):
|
||||
rec = db._search(_id=recid)[0]
|
||||
|
||||
def ret(status):
|
||||
return {"job_status": status, "job_id": job_id}
|
||||
return {"job_status": status, "job_id": recid}
|
||||
|
||||
if job is True:
|
||||
if rec.ready:
|
||||
return ret("DONE")
|
||||
if job is False:
|
||||
abort(404, "No such job has ever been spawned")
|
||||
else:
|
||||
if job.ready():
|
||||
try:
|
||||
res = job.get()
|
||||
return res
|
||||
except Exception as exc:
|
||||
r = ret("FAILED")
|
||||
r["exception"] = str(exc)
|
||||
import traceback
|
||||
|
||||
tb = traceback.format_exc()
|
||||
logger.warning(tb)
|
||||
if get_config()["DEBUG"]:
|
||||
r["exception"] = "%s: %s" % (str(exc), tb)
|
||||
r["traceback"] = tb
|
||||
|
||||
return r
|
||||
return ret("WIP")
|
||||
return ret("WIP")
|
||||
|
||||
|
||||
@app.get("/api/jobs")
|
||||
|
|
|
@ -72,7 +72,7 @@ var RecAPI = {
|
|||
}
|
||||
|
||||
function poll_job (job_id, callback) {
|
||||
$.getJSON('/api/jobs/' + job_id)
|
||||
$.getJSON('/api/ready/' + job_id)
|
||||
.done(function (data) {
|
||||
if (data.job_status !== 'WIP') {
|
||||
console.log('polling completed for job[' + job_id + ']', data)
|
||||
|
|
Loading…
Reference in a new issue