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
|
import sys
|
||||||
from datetime import datetime, timedelta
|
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)
|
inspect)
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
@ -28,6 +28,7 @@ class Rec(Base):
|
||||||
starttime = Column(DateTime, nullable=True)
|
starttime = Column(DateTime, nullable=True)
|
||||||
endtime = Column(DateTime, nullable=True)
|
endtime = Column(DateTime, nullable=True)
|
||||||
filename = Column(String, nullable=True)
|
filename = Column(String, nullable=True)
|
||||||
|
ready = Column(Boolean, default=False)
|
||||||
|
|
||||||
def __init__(self, name="", starttime=None, endtime=None, filename=None):
|
def __init__(self, name="", starttime=None, endtime=None, filename=None):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
|
@ -6,7 +6,7 @@ import os
|
||||||
import unicodedata
|
import unicodedata
|
||||||
from datetime import datetime
|
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.responses import FileResponse, RedirectResponse, JSONResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
@ -140,16 +140,21 @@ class GenerateResponse(BaseModel):
|
||||||
message: str
|
message: str
|
||||||
|
|
||||||
@app.post("/api/generate")
|
@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
|
# prendiamo la rec in causa
|
||||||
recid = req.id
|
rec = db._search(_id=req.id)[0]
|
||||||
rec = db._search(_id=recid)[0]
|
if rec.ready:
|
||||||
if rec.filename is not None and os.path.exists(rec.filename):
|
|
||||||
return {
|
return {
|
||||||
"status": "ready",
|
"status": "ready",
|
||||||
"message": "The file has already been generated at %s" % rec.filename,
|
"message": "The file has already been generated at %s" % rec.filename,
|
||||||
"rec": rec,
|
"rec": rec,
|
||||||
}
|
}
|
||||||
|
if rec.filename is not None:
|
||||||
|
return {
|
||||||
|
"status": "ready",
|
||||||
|
"message": "Generating...",
|
||||||
|
"rec": rec,
|
||||||
|
}
|
||||||
if (
|
if (
|
||||||
get_config()["FORGE_MAX_DURATION"] > 0
|
get_config()["FORGE_MAX_DURATION"] > 0
|
||||||
and (rec.endtime - rec.starttime).total_seconds()
|
and (rec.endtime - rec.starttime).total_seconds()
|
||||||
|
@ -178,8 +183,9 @@ async def generate(req: GenerateInfo, response: Response):
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
db.get_session(rec).commit()
|
db.get_session(rec).commit()
|
||||||
job_id = pq.submit(
|
background_tasks.add_task(
|
||||||
create_mp3,
|
generate_mp3,
|
||||||
|
db_id=req.id,
|
||||||
start=rec.starttime,
|
start=rec.starttime,
|
||||||
end=rec.endtime,
|
end=rec.endtime,
|
||||||
outfile=os.path.join(get_config()["AUDIO_OUTPUT"], rec.filename),
|
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"],
|
"extra_tags": get_config()["TAG_EXTRA"],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
logger.debug("SUBMITTED: %d" % job_id)
|
logger.debug("SUBMITTED: %d" % req.id)
|
||||||
return rec_msg(
|
return rec_msg(
|
||||||
"Aggiornamento completato!",
|
"Aggiornamento completato!",
|
||||||
job_id=job_id,
|
job_id=rec.id,
|
||||||
result="/output/" + rec.filename,
|
result="/output/" + rec.filename,
|
||||||
rec=rec_sanitize(rec),
|
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:
|
@app.get("/api/ready/{recid}")
|
||||||
job = check_job(job_id)
|
def check_job(recid: int):
|
||||||
except ValueError:
|
rec = db._search(_id=recid)[0]
|
||||||
abort(400, "job_id not valid")
|
|
||||||
|
|
||||||
def ret(status):
|
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")
|
return ret("DONE")
|
||||||
if job is False:
|
return ret("WIP")
|
||||||
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")
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/jobs")
|
@app.get("/api/jobs")
|
||||||
|
|
|
@ -72,7 +72,7 @@ var RecAPI = {
|
||||||
}
|
}
|
||||||
|
|
||||||
function poll_job (job_id, callback) {
|
function poll_job (job_id, callback) {
|
||||||
$.getJSON('/api/jobs/' + job_id)
|
$.getJSON('/api/ready/' + job_id)
|
||||||
.done(function (data) {
|
.done(function (data) {
|
||||||
if (data.job_status !== 'WIP') {
|
if (data.job_status !== 'WIP') {
|
||||||
console.log('polling completed for job[' + job_id + ']', data)
|
console.log('polling completed for job[' + job_id + ']', data)
|
||||||
|
|
Loading…
Reference in a new issue