From f0c5e9c597d76216766b4a2b3307c67a210c7cfe Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 15 Jul 2018 02:18:30 +0200 Subject: [PATCH] [enh] add a page per job --- run.py | 36 ++++++++++++++++++++++++++++++++++++ templates/index.html | 4 ++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/run.py b/run.py index 6633791..fd9c409 100644 --- a/run.py +++ b/run.py @@ -16,6 +16,7 @@ import aiofiles from websockets.exceptions import ConnectionClosed from sanic import Sanic, response +from sanic.exceptions import NotFound from playhouse.shortcuts import model_to_dict, dict_to_model @@ -182,6 +183,28 @@ async def index_ws(request, websocket): await websocket.send(f"echo {data}") +@app.websocket('/job--ws') +async def job_ws(request, websocket, job_id): + job = Job.select().where(Job.id == job_id) + + if job.count == 0: + raise NotFound() + + job = job[0] + + subscribe(websocket, f"job-{job.id}") + + await websocket.send(ujson.dumps({ + "action": "init_job", + "data": model_to_dict(job), + })) + + while True: + data = await websocket.recv() + print(f"websocket: {data}") + await websocket.send(f"echo {data}") + + @app.route("/api/jobs") async def api_jobs(request): return response.json(map(model_to_dict, Job.select())) @@ -212,6 +235,19 @@ async def api_new_job(request): return response.text("ok") +@app.route('/job/') +async def job(request, job_id): + job = Job.select().where(Job.id == job_id) + + if job.count == 0: + raise NotFound() + + job = job[0] + + async with aiofiles.open("./templates/job.html", mode="r") as index_template: + return response.html(await index_template.read() % job.id) + + @app.route('/') async def index(request): async with aiofiles.open("./templates/index.html", mode="r") as index_template: diff --git a/templates/index.html b/templates/index.html index cfb5915..4196de1 100644 --- a/templates/index.html +++ b/templates/index.html @@ -20,8 +20,8 @@ Started time End time - - {{job.name}} + + {{job.name}} {{job.state}} {{job.target_revision}} {{job.yunohost_version}}