]> git.ipfire.org Git - pakfire.git/commitdiff
hub: Add a new class for jobs
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 May 2022 12:08:41 +0000 (12:08 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 May 2022 12:08:41 +0000 (12:08 +0000)
This is a proxy which is being used during the build to communicate with
the hub.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/hub.py

index 51d8df36756642a04a8699115d671df847d3e36e..5c4f3cc0335f9c4f82ec255ef1dda5b5dd544016 100644 (file)
@@ -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,
+               })