[enh] boardcasts logs

This commit is contained in:
Laurent Peuch 2018-07-15 03:40:23 +02:00
parent f0c5e9c597
commit 5b51fbfef6
2 changed files with 30 additions and 11 deletions

View file

@ -26,6 +26,8 @@ class Job(peewee.Model):
('failure', 'Failure'), ('failure', 'Failure'),
), default="scheduled") ), default="scheduled")
log = peewee.TextField(default="")
created_time = peewee.DateTimeField(constraints=[peewee.SQL("DEFAULT (datetime('now'))")]) created_time = peewee.DateTimeField(constraints=[peewee.SQL("DEFAULT (datetime('now'))")])
started_time = peewee.DateTimeField(null=True) started_time = peewee.DateTimeField(null=True)
end_time = peewee.DateTimeField(null=True) end_time = peewee.DateTimeField(null=True)

39
run.py
View file

@ -115,9 +115,9 @@ async def jobs_dispatcher():
async def run_job(worker, job): async def run_job(worker, job):
await broadcast({ await broadcast({
"action": "job", "action": "update_job",
"data": model_to_dict(job), "data": model_to_dict(job),
}, "jobs") }, ["jobs", f"job-{job.id}"])
# fake stupid command, whould run CI instead # fake stupid command, whould run CI instead
print(f"Starting job {job.name}...") print(f"Starting job {job.name}...")
@ -128,6 +128,19 @@ async def run_job(worker, job):
while not command.stdout.at_eof(): while not command.stdout.at_eof():
data = await command.stdout.readline() data = await command.stdout.readline()
line = data.decode().rstrip() line = data.decode().rstrip()
job.log += data.decode()
# XXX seems to be okay performance wise but that's probably going to be
# a bottleneck at some point :/
# theoritically jobs are going to have slow output
job.save()
await broadcast({
"action": "update_job",
"id": job.id,
"data": model_to_dict(job),
}, ["jobs", f"job-{job.id}"])
print(f">> {line}") print(f">> {line}")
# XXX stupid crap to stimulate long jobs # XXX stupid crap to stimulate long jobs
@ -147,18 +160,22 @@ async def run_job(worker, job):
"action": "update_job", "action": "update_job",
"id": job.id, "id": job.id,
"data": model_to_dict(job), "data": model_to_dict(job),
}, "jobs") }, ["jobs", f"job-{job.id}"])
async def broadcast(message, channel): async def broadcast(message, channels):
ws_list = subscriptions[channel] if not isinstance(channels, (list, tuple)):
dead_ws = [] channels = [channels]
for ws in ws_list: for channel in channels:
try: ws_list = subscriptions[channel]
await ws.send(ujson.dumps(message)) dead_ws = []
except ConnectionClosed:
dead_ws.append(ws) for ws in ws_list:
try:
await ws.send(ujson.dumps(message))
except ConnectionClosed:
dead_ws.append(ws)
for to_remove in dead_ws: for to_remove in dead_ws:
ws_list.remove(to_remove) ws_list.remove(to_remove)