]> git.ipfire.org Git - pbs.git/commitdiff
backend: Use the @run_in_thread decorator
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 10 Feb 2025 15:25:03 +0000 (15:25 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 10 Feb 2025 15:25:03 +0000 (15:25 +0000)
This allows us to write shorter code but achieve the same.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/__init__.py
src/buildservice/jobs.py

index 074f7b082d26191f830072040420cbf7eb7e85c0..69d6f8dd24c2548d2b8efd257a661b7049b3872a 100644 (file)
@@ -44,6 +44,7 @@ log = logging.getLogger("pbs")
 from .__version__ import VERSION as __version__
 
 from .constants import *
+from .decorators import *
 
 class Backend(object):
        version = __version__
@@ -339,17 +340,27 @@ class Backend(object):
                        "kinit", "-k", "-t", keytab, principal, **kwargs,
                )
 
-       async def exists(self, *args, **kwargs):
+       @run_in_thread
+       def exists(self, *args, **kwargs):
                """
                        Checks whether a file exists
                """
-               return await asyncio.to_thread(os.path.exists, *args, **kwargs)
+               return os.path.exists(*args, **kwargs)
 
-       async def makedirs(self, path, **kwargs):
+       @run_in_thread
+       def makedirs(self, path, **kwargs):
                """
                        Creates a new directory
                """
-               return await asyncio.to_thread(os.makedirs, path, **kwargs)
+               return os.makedirs(path, **kwargs)
+
+       async def make_parent_directory(self, path):
+               """
+                       Creates the parent directory of path
+               """
+               path = os.path.dirname(path)
+
+               return await self.makedirs(path, exist_ok=True)
 
        async def copy(self, src, dst, mode=None):
                """
@@ -365,7 +376,7 @@ class Backend(object):
 
                # Set mode
                if mode:
-                       await asyncio.to_thread(os.chmod, dst, mode)
+                       await self.chmod(dst, mode)
 
        async def move(self, src, dst, **kwargs):
                """
@@ -379,15 +390,8 @@ class Backend(object):
                # Move!
                await asyncio.to_thread(shutil.move, src, dst, **kwargs)
 
-       async def make_parent_directory(self, path):
-               """
-                       Creates the parent directory of path
-               """
-               path = os.path.dirname(path)
-
-               return await self.makedirs(path, exist_ok=True)
-
-       async def unlink(self, path):
+       @run_in_thread
+       def unlink(self, path):
                """
                        Unlinks path
                """
@@ -400,9 +404,6 @@ class Backend(object):
 
                log.debug("Unlinking %s" % path)
 
-               await asyncio.to_thread(self._unlink, path)
-
-       def _unlink(self, path):
                # Unlink the file we were asked to unlink
                try:
                        os.unlink(path)
@@ -426,7 +427,8 @@ class Backend(object):
 
                        log.debug("  Cleaned up %s..." % path)
 
-       async def rmtree(self, path):
+       @run_in_thread
+       def rmtree(self, path):
                """
                        Removes a directory recursively
                """
@@ -440,14 +442,15 @@ class Backend(object):
                log.debug("Removing %s..." % path)
 
                try:
-                       await asyncio.to_thread(shutil.rmtree, path)
+                       shutil.rmtree(path)
 
                # Ignore if path didn't exist in the first place
                except FileNotFoundError:
                        pass
 
-       async def chmod(self, *args, **kwargs):
-               return await asyncio.to_thread(os.chmod, *args, **kwargs)
+       @run_in_thread
+       def chmod(self, *args, **kwargs):
+               return os.chmod(*args, **kwargs)
 
        def tempfile(self, mode="w+b", sync=False, **kwargs):
                """
index 3c4b86ac68b0cbfe0197bed584891673470d2e92..6a007ec28d077cc66316707e28f29042a48a48b9 100644 (file)
@@ -971,7 +971,8 @@ class Job(database.Base, database.BackendMixin, database.SoftDeleteMixin):
 
        # Open Log
 
-       async def open_log(self):
+       @run_in_thread
+       def open_log(self):
                """
                        Opens the log file, and returns an open file handle
                """
@@ -979,15 +980,12 @@ class Job(database.Base, database.BackendMixin, database.SoftDeleteMixin):
                if not self.has_log():
                        raise FileNotFoundError
 
-               return await asyncio.to_thread(self._open_log, self.log_path)
-
-       def _open_log(self, path):
                # Open gzip-compressed files
-               if path.endswith(".gz"):
+               if self.log_path.endswith(".gz"):
                        return gzip.open(path)
 
                # Open uncompressed files
-               return open(path, "rb")
+               return open(self.log_path, "rb")
 
        # Tail Log