[mod] rename, uses 'job' instead of 'build task'

This commit is contained in:
Laurent Peuch 2018-07-13 06:25:35 +02:00
parent a6ee91c944
commit b4f4293784
3 changed files with 50 additions and 50 deletions

View file

@ -13,7 +13,7 @@ class Repo(peewee.Model):
database = db database = db
class BuildTask(peewee.Model): class Job(peewee.Model):
repo = peewee.ForeignKeyField(Repo) repo = peewee.ForeignKeyField(Repo)
target_revision = peewee.CharField() target_revision = peewee.CharField()
yunohost_version = peewee.CharField() yunohost_version = peewee.CharField()
@ -41,7 +41,7 @@ class Worker(peewee.Model):
# peewee is a bit stupid and will crash if the table already exists # peewee is a bit stupid and will crash if the table already exists
for i in [Repo, BuildTask, Worker]: for i in [Repo, Job, Worker]:
try: try:
i.create_table() i.create_table()
except: except:

66
run.py
View file

@ -19,7 +19,7 @@ from sanic import Sanic, response
from playhouse.shortcuts import model_to_dict, dict_to_model from playhouse.shortcuts import model_to_dict, dict_to_model
from models import Repo, BuildTask, db, Worker from models import Repo, Job, db, Worker
app = Sanic() app = Sanic()
@ -58,7 +58,7 @@ async def initialize_app_list():
) )
print(f"Schedule a new build for {app_id}") print(f"Schedule a new build for {app_id}")
BuildTask.create( Job.create(
repo=repo, repo=repo,
target_revision=app_data["git"]["revision"], target_revision=app_data["git"]["revision"],
yunohost_version="stretch-stable", yunohost_version="stretch-stable",
@ -66,7 +66,7 @@ async def initialize_app_list():
) )
async def tasks_dispatcher(): async def jobs_dispatcher():
if Worker.select().count() == 0: if Worker.select().count() == 0:
for i in range(5): for i in range(5):
Worker.create(state="available") Worker.create(state="available")
@ -80,37 +80,37 @@ async def tasks_dispatcher():
continue continue
with db.atomic('IMMEDIATE'): with db.atomic('IMMEDIATE'):
build_tasks = BuildTask.select().where(BuildTask.state == "scheduled") jobs = Job.select().where(Job.state == "scheduled")
# no task to process, wait # no jobs to process, wait
if build_tasks.count() == 0: if jobs.count() == 0:
await asyncio.sleep(3) await asyncio.sleep(3)
continue continue
for i in range(min(workers.count(), build_tasks.count())): for i in range(min(workers.count(), jobs.count())):
build_task = build_tasks[i] job = jobs[i]
worker = workers[i] worker = workers[i]
build_task.state = "running" job.state = "running"
build_task.started_time = datetime.now() job.started_time = datetime.now()
print(build_task) print(job)
build_task.save() job.save()
worker.state = "busy" worker.state = "busy"
worker.save() worker.save()
asyncio.ensure_future(run_task(worker, build_task)) asyncio.ensure_future(run_job(worker, job))
async def run_task(worker, build_task): async def run_job(worker, job):
await broadcast({ await broadcast({
"target": "build_task", "target": "job",
"id": build_task.id, "id": job.id,
"data": model_to_dict(build_task), "data": model_to_dict(job),
}, "build_tasks") }, "jobs")
# fake stupid command, whould run CI instead # fake stupid command, whould run CI instead
print(f"Starting job for {build_task.repo.name}...") print(f"Starting job for {job.repo.name}...")
command = await asyncio.create_subprocess_shell("/usr/bin/tail /var/log/auth.log", command = await asyncio.create_subprocess_shell("/usr/bin/tail /var/log/auth.log",
stdout=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE) stderr=asyncio.subprocess.PIPE)
@ -123,21 +123,21 @@ async def run_task(worker, build_task):
# XXX stupid crap to stimulate long jobs # XXX stupid crap to stimulate long jobs
await asyncio.sleep(random.randint(1, 15)) await asyncio.sleep(random.randint(1, 15))
# await asyncio.sleep(5) # await asyncio.sleep(5)
print(f"Finished task for {build_task.repo.name}") print(f"Finished job for {job.repo.name}")
await command.wait() await command.wait()
build_task.end_time = datetime.now() job.end_time = datetime.now()
build_task.state = "done" job.state = "done"
build_task.save() job.save()
worker.state = "available" worker.state = "available"
worker.save() worker.save()
await broadcast({ await broadcast({
"target": "build_task", "target": "job",
"id": build_task.id, "id": job.id,
"data": model_to_dict(build_task), "data": model_to_dict(job),
}, "build_tasks") }, "jobs")
async def broadcast(message, channel): async def broadcast(message, channel):
@ -160,7 +160,7 @@ def subscribe(ws, channel):
@app.websocket('/index-ws') @app.websocket('/index-ws')
async def index_ws(request, websocket): async def index_ws(request, websocket):
subscribe(websocket, "build_tasks") subscribe(websocket, "jobs")
while True: while True:
data = await websocket.recv() data = await websocket.recv()
@ -168,20 +168,20 @@ async def index_ws(request, websocket):
await websocket.send(f"echo {data}") await websocket.send(f"echo {data}")
@app.route("/api/tasks") @app.route("/api/jobs")
async def api_tasks(request): async def api_jobs(request):
return response.json(map(model_to_dict, BuildTask.select())) return response.json(map(model_to_dict, Job.select()))
@app.route('/') @app.route('/')
async def index(request): async def index(request):
return response.html(open("./templates/index.html", "r").read()) return response.html(open("./templates/index.html", "r").read())
# return await render_template("index.html", build_tasks=BuildTask.select().order_by("id")) # return await render_template("index.html", jobs=Job.select().order_by("id"))
if __name__ == "__main__": if __name__ == "__main__":
subscriptions = defaultdict(list) subscriptions = defaultdict(list)
app.add_task(initialize_app_list()) app.add_task(initialize_app_list())
app.add_task(tasks_dispatcher()) app.add_task(jobs_dispatcher())
app.run('localhost', port=5000, debug=True) app.run('localhost', port=5000, debug=True)

View file

@ -19,14 +19,14 @@
<th>Started time</th> <th>Started time</th>
<th>End time</th> <th>End time</th>
</tr> </tr>
<tr v-for="build_task in build_tasks" :id="build_task.id"> <tr v-for="job in jobs" :id="job.id">
<td>{{build_task.repo.name}}</td> <td>{{job.repo.name}}</td>
<td>{{build_task.state}}</td> <td>{{job.state}}</td>
<td>{{build_task.target_revision}}</td> <td>{{job.target_revision}}</td>
<td>{{build_task.yunohost_version}}</td> <td>{{job.yunohost_version}}</td>
<td>{{build_task.created_time}}</td> <td>{{job.created_time}}</td>
<td>{{build_task.started_time}}</td> <td>{{job.started_time}}</td>
<td>{{build_task.end_time}}</td> <td>{{job.end_time}}</td>
</tr> </tr>
</table> </table>
</div> </div>
@ -35,12 +35,12 @@
var app = new Vue({ var app = new Vue({
el: '#jobs', el: '#jobs',
data: { data: {
build_tasks: [] jobs: []
}, },
}) })
$.getJSON("/api/tasks").done(function(data) { $.getJSON("/api/jobs").done(function(data) {
app.build_tasks = data; app.jobs = data;
}) })
ws = new WebSocket('ws://' + document.domain + ':' + location.port + '/index-ws'); ws = new WebSocket('ws://' + document.domain + ':' + location.port + '/index-ws');
@ -50,10 +50,10 @@
var data = message.data; var data = message.data;
var target = message.target; var target = message.target;
if (target == "build_task") { if (target == "job") {
for (var i = 0; i < app.build_tasks.length; ++i) { for (var i = 0; i < app.jobs.length; ++i) {
if (app.build_tasks[i].id == data.id) { if (app.jobs[i].id == data.id) {
Vue.set(app.build_tasks, i, data); Vue.set(app.jobs, i, data);
return; return;
} }
} }