From 286a9136b071583be90ca1342a43010a4116bb31 Mon Sep 17 00:00:00 2001 From: Jakub Kadlcik Date: Tue, 10 Sep 2024 08:36:23 +0200 Subject: [PATCH] backend: add a timeout for waiting until a Pulp task finishes --- backend/copr_backend/pulp.py | 5 ++++- backend/copr_backend/storage.py | 13 +++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/backend/copr_backend/pulp.py b/backend/copr_backend/pulp.py index 6dc19042b..3a93a1765 100644 --- a/backend/copr_backend/pulp.py +++ b/backend/copr_backend/pulp.py @@ -190,18 +190,21 @@ def delete_distribution(self, distribution): url = self.config["base_url"] + distribution return requests.delete(url, **self.request_params) - def wait_for_finished_task(self, task): + def wait_for_finished_task(self, task, timeout=86400): """ Pulp task (e.g. creating a publication) can be running for an unpredictably long time. We need to wait until it is finished to know what it actually did. """ + start = time.time() while True: response = self.get_task(task) if not response.ok: break if response.json()["state"] not in ["waiting", "running"]: break + if time.time() > start + timeout: + break time.sleep(5) return response diff --git a/backend/copr_backend/storage.py b/backend/copr_backend/storage.py index fdb683815..1d816b6c1 100644 --- a/backend/copr_backend/storage.py +++ b/backend/copr_backend/storage.py @@ -8,6 +8,7 @@ from copr_common.enums import StorageEnum from copr_backend.helpers import call_copr_repo, build_chroot_log_name from copr_backend.pulp import PulpClient +from copr_backend.exceptions import CoprBackendError def storage_for_job(job, opts, log): @@ -231,7 +232,10 @@ def upload_build_results(self, chroot, results_dir, target_dir_name): # creating the `pulp.json` file task = response.json()["task"] response = self.client.wait_for_finished_task(task) - created = response.json()["created_resources"] + created = response.json().get("created_resources") + if not created: + raise CoprBackendError( + "Pulp task {0} didn't create any resources".format(task)) resources.extend(created) self.log.info("Uploaded to Pulp: %s", path) @@ -257,7 +261,12 @@ def publish_repository(self, chroot, **kwargs): task, response.text) return False - publication = response.json()["created_resources"][0] + resources = response.json()["created_resources"] + if not resources: + raise CoprBackendError( + "Pulp task {0} didn't create any resources".format(task)) + + publication = resources[0] distribution_name = self._distribution_name(chroot) distribution = self._get_distribution(chroot)