From 688e6846ada01d1ad4c4c7e8f0fb93d76467756b Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 1 Nov 2022 12:49:36 +0000 Subject: [PATCH] daemon: Write result into a temporary directory and upload the packages Signed-off-by: Michael Tremer --- src/pakfire/daemon.py | 55 +++++++++++++++++++++++++------------------ src/pakfire/hub.py | 36 ++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 25 deletions(-) diff --git a/src/pakfire/daemon.py b/src/pakfire/daemon.py index 73cfa2311..bae4301b0 100644 --- a/src/pakfire/daemon.py +++ b/src/pakfire/daemon.py @@ -2,6 +2,7 @@ import asyncio import functools +import glob import json import logging import logging.handlers @@ -239,30 +240,38 @@ class Worker(multiprocessing.Process): # Setup build logger logger = BuildLogger(self.log, self.job) - # Run the build - try: - build = self._build(pkg, arch=arch, - logger=logger._log, build_id=job_id) - - # Wait until the build process is done and stream the log in the meantime - while not build.done(): - await logger.stream(timeout=1) - - # Catch any other Exception - except Exception as e: - raise e + # Create a temporary directory in which the built packages will be copied + with tempfile.TemporaryDirectory(prefix="pakfire-packages-") as target: + packages = None - # The build has finished successfully - else: - success = True - - # Notify the hub that the job has finished - finally: - await self.hub.finish_job( - job_id, - success=success, - log=logger.logfile.name, - ) + # Run the build + try: + build = self._build(pkg, arch=arch, target=target, + logger=logger._log, build_id=job_id) + + # Wait until the build process is done and stream the log in the meantime + while not build.done(): + await logger.stream(timeout=1) + + # Catch any other Exception + except Exception as e: + raise e + + # The build has finished successfully + else: + success = True + + # Find any packages + packages = glob.glob("%s/*.pfm" % target) + + # Notify the hub that the job has finished + finally: + await self.hub.finish_job( + job_id, + success=success, + log=logger.logfile.name, + packages=packages, + ) def _build(self, pkg, arch=None, logger=None, **kwargs): """ diff --git a/src/pakfire/hub.py b/src/pakfire/hub.py index d14ac089e..2c5df4fff 100644 --- a/src/pakfire/hub.py +++ b/src/pakfire/hub.py @@ -276,6 +276,34 @@ class Hub(object): async def delete_upload(self, upload_id): await self._request("DELETE", "/uploads", id=upload_id) + async def upload_multi(self, *paths, show_progress=True): + """ + Upload multiple files + + If one file could not be uploaded, any other uploads will be deleted + """ + uploads = [] + + # Upload everything + try: + for path in paths: + upload = await self.upload(path, show_progress=show_progress) + + # Store the upload ID + uploads.append(upload) + + except Exception as e: + # Remove any previous uploads + await asyncio.gather( + *(self.delete_upload(upload) for upload in uploads), + ) + + # Raise the exception + raise e + + # Return the IDs of the uploads + return uploads + @staticmethod def _stream_file(path, size, p, write): # Start the progressbar @@ -438,7 +466,7 @@ class Hub(object): # Return a Job proxy return Job(self, id, conn) - async def finish_job(self, job_id, success, log): + async def finish_job(self, job_id, success, packages=None, log=None): """ Will tell the hub that a job has finished """ @@ -446,9 +474,13 @@ class Hub(object): if log: log = await self.upload(log, filename="%s.log" % job_id) + # Upload the packages + if packages: + packages = await self.upload_multi(*packages) + # Send the request response = await self._request("POST", "/jobs/%s/finished" % job_id, - success=success, log=log) + success=success, log=log, packages=packages) # Handle the response # XXX TODO -- 2.39.5