From 4f872b98ff1c130ea251bbd7a76c1650f1d9f936 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 26 Apr 2023 17:14:14 +0000 Subject: [PATCH] daemon: Build scaffolding to abort builds Signed-off-by: Michael Tremer --- src/pakfire/daemon.py | 10 +++++++++- src/pakfire/hub.py | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/pakfire/daemon.py b/src/pakfire/daemon.py index 3278d37b4..561301f86 100644 --- a/src/pakfire/daemon.py +++ b/src/pakfire/daemon.py @@ -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): diff --git a/src/pakfire/hub.py b/src/pakfire/hub.py index c95a68287..e7459b1ac 100644 --- a/src/pakfire/hub.py +++ b/src/pakfire/hub.py @@ -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 -- 2.39.5