From: Michael Tremer Date: Sun, 29 May 2022 12:08:41 +0000 (+0000) Subject: hub: Add a new class for jobs X-Git-Tag: 0.9.28~720 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c8d43da3b49da8f4409745e9827639a4406dbfec;p=pakfire.git hub: Add a new class for jobs This is a proxy which is being used during the build to communicate with the hub. Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/hub.py b/src/pakfire/hub.py index 51d8df367..5c4f3cc03 100644 --- a/src/pakfire/hub.py +++ b/src/pakfire/hub.py @@ -26,6 +26,7 @@ import json import logging import os.path import psutil +import tornado.escape import tornado.httpclient import tornado.websocket import urllib.parse @@ -343,3 +344,52 @@ class Hub(object): return return callback(message) + + async def job(self, id): + """ + Connects to the given job + """ + # Connect to the hub + conn = await self._request("GET", "/jobs/%s/builder" % id, + websocket=True, ping=10) + + # Return a Job proxy + return Job(self, id, conn) + + +class Job(object): + """ + Proxy for Build Jobs + """ + def __init__(self, hub, id, conn): + self.hub = hub + self.id = id + self.conn = conn + + async def _write_message(self, message, **kwargs): + """ + Sends a message but encodes it into JSON first + """ + if isinstance(message, dict): + message = tornado.escape.json_encode(message) + + return await self.conn.write_message(message, **kwargs) + + async def status(self, status): + """ + Sends a new status to the hub + """ + await self._write_message({ + "message" : "status", + "status" : status, + }) + + async def log(self, level, message): + """ + Sends a log message to the hub + """ + await self._write_message({ + "message" : "log", + "level" : level, + "log" : message, + })