]> git.ipfire.org Git - pakfire.git/commitdiff
daemon: Build scaffolding to abort builds
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 26 Apr 2023 17:14:14 +0000 (17:14 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 26 Apr 2023 17:14:14 +0000 (17:14 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/daemon.py
src/pakfire/hub.py

index 3278d37b495f37511004ee9e168e1a42a9a6832e..561301f86c1d733ccf96c10dd8922cf80c75477f 100644 (file)
@@ -227,7 +227,7 @@ class Worker(multiprocessing.Process):
                        raise ValueError("Did not received a package URL")
 
                # Connect to the hub
-               self.job = await self.hub.job(job_id)
+               self.job = await self.hub.job(job_id, abort_callback=self.abort)
 
                # Setup build logger
                logger = BuildLogger(self.log, self.job)
@@ -293,6 +293,14 @@ class Worker(multiprocessing.Process):
 
                # XXX figure out what to do, when a build is running
 
+       def abort(self, *args, **kwargs):
+               """
+                       Called to abort a running build immediately
+               """
+               log.warning("Build job has been aborted")
+
+               # XXX TODO
+
        # Signal handling.
 
        def register_signal_handlers(self):
index c95a68287007a18c6cdebad6e52c73b9e56be003..e7459b1ac784eb0cf003e9482b988a0c43000a4c 100644 (file)
@@ -405,11 +405,11 @@ class Hub(object):
                """
                return await self._proxy(ControlConnection, *args, **kwargs)
 
-       async def job(self, job_id):
+       async def job(self, *args, **kwargs):
                """
                        Creates a control connection for a certain job
                """
-               return await self._proxy(JobControlConnection, job_id)
+               return await self._proxy(JobControlConnection, *args, **kwargs)
 
 
 class HubObject(object):
@@ -617,13 +617,43 @@ class JobControlConnection(HubObject):
        """
                Proxy for Build Jobs
        """
-       def init(self, id):
+       def init(self, id, abort_callback=None):
                self.id = id
 
+               self.callbacks = {
+                       "abort" : abort_callback,
+               }
+
        @property
        def url(self):
                return "/api/v1/jobs/%s" % self.id
 
+       def on_message_callback(self, message):
+               message = self.hub._decode_json_message(message)
+
+               # Ignore empty messages
+               if message is None:
+                       return
+
+               # Log the received message
+               log.debug("Received message:\n%s" % json.dumps(message, indent=4))
+
+               # Fetch the message type & data
+               type = message.get("type")
+               data = message.get("data")
+
+               # Find a suitable callback
+               try:
+                       callback = self.callbacks[type]
+
+               # Log an error for unknown messages and ignore them
+               except KeyError:
+                       log.error("Received message of unknown type '%s'" % type)
+                       return
+
+               # Call the callback
+               callback(data)
+
        async def finished(self, success, packages=None, logfile=None):
                """
                        Will tell the hub that a job has finished