From: Michael Tremer Date: Fri, 15 Jul 2022 10:26:13 +0000 (+0000) Subject: uploads: Make deleting uploads async X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2d165cebb870a9ed4576c819e6c5733f3af2a7a1;p=pbs.git uploads: Make deleting uploads async Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/__init__.py b/src/buildservice/__init__.py index 4362fd9a..496f1b0b 100644 --- a/src/buildservice/__init__.py +++ b/src/buildservice/__init__.py @@ -235,6 +235,17 @@ class Backend(object): # Copy data without any metadata await asyncio.to_thread(shutil.copyfile, src, dst) + async def unlink(self, path): + """ + Unlinks path + """ + log.debug("Unlinking %s" % path) + + try: + await asycio.to_thread(os.unlink, path) + except OSError as e: + pass + def _write_tempfile(self, content): """ Writes the content to a temporary file and returns its path diff --git a/src/buildservice/uploads.py b/src/buildservice/uploads.py index 3d6cbd49..64c1eef7 100644 --- a/src/buildservice/uploads.py +++ b/src/buildservice/uploads.py @@ -78,7 +78,7 @@ class Uploads(base.Object): # Return the newly created upload object return upload - def cleanup(self): + async def cleanup(self): # Find all expired uploads uploads = self._get_uploads(""" SELECT @@ -93,7 +93,8 @@ class Uploads(base.Object): # Delete them all for upload in uploads: - upload.delete() + with self.db.transaction(): + await upload.delete() class Upload(base.DataObject): @@ -140,14 +141,11 @@ class Upload(base.DataObject): user = lazy_property(get_user, set_user) - def delete(self): + async def delete(self): log.info("Deleting upload %s (%s)" % (self, self.path)) # Remove the uploaded data - try: - os.unlink(self.path) - except OSError: - pass + await self.backend.unlink(self.path) # Delete the upload from the database self.db.execute("DELETE FROM uploads WHERE id = %s", self.id) diff --git a/src/hub/builds.py b/src/hub/builds.py index 4d87ba8f..8340c837 100644 --- a/src/hub/builds.py +++ b/src/hub/builds.py @@ -59,7 +59,7 @@ class CreateHandler(BaseHandler): build = self.backend.builds.create(repo, package) # Delete the upload - upload.delete() + await upload.delete() # Send some data about the build self.finish({