]> git.ipfire.org Git - pbs.git/commitdiff
jobs: Let Apache deliver the log files
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 7 Feb 2025 15:07:03 +0000 (15:07 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 7 Feb 2025 15:07:03 +0000 (15:07 +0000)
This will be even more efficient than sending them in the main process.

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

index b3785df005dfcfbd0304cfff74d1dd8d17a6a99a..f38489a5e209322830d98650de31353542b753b8 100644 (file)
@@ -947,7 +947,12 @@ class Job(database.Base, database.BackendMixin, database.SoftDeleteMixin):
 
        @property
        def log_url(self):
-               return self.backend.path_to_url(self.log_path)
+               """
+                       Creates the URL to the log file without the compression extension.
+               """
+               log_path, extension = os.path.splitext(self.log_path)
+
+               return self.backend.path_to_url(log_path)
 
        # Log Path
 
@@ -963,7 +968,7 @@ class Job(database.Base, database.BackendMixin, database.SoftDeleteMixin):
 
        # Open Log
 
-       async def open_log(self, decode=True):
+       async def open_log(self):
                """
                        Opens the log file, and returns an open file handle
                """
@@ -971,13 +976,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, decode=decode)
+               return await asyncio.to_thread(self._open_log, self.log_path)
 
-       def _open_log(self, path, decode=True):
-               if decode:
-                       # Open gzip-compressed files
-                       if path.endswith(".gz"):
-                               return gzip.open(path)
+       def _open_log(self, path):
+               # Open gzip-compressed files
+               if path.endswith(".gz"):
+                       return gzip.open(path)
 
                # Open uncompressed files
                return open(path, "rb")
index 098359771ac4a644c180d3616ea107dc95cf92bd..cf6235895154e5a419eea779f9dad5a87b807750 100644 (file)
@@ -163,8 +163,8 @@ class LogHandler(base.BaseHandler):
                tail = self.get_argument_int("tail", None)
 
                # Should we tail the log, or stream the entire file?
-               try:
-                       if tail:
+               if tail:
+                       try:
                                log = await job.tail_log(tail)
 
                                # Since we are processing this line-based,
@@ -175,28 +175,12 @@ class LogHandler(base.BaseHandler):
                                # Done!
                                return
 
-                       # Automatically decompress the log
-                       decode = True
-
-                       if self.client_accepts_encoding("gzip"):
-                               decode = False
-
-                               # Tell the client we are sending this compressed
-                               self.set_header("Content-Encoding", "gzip")
-
-                       # If we are sending the entire file we won't split it by line before
-                       log = await job.open_log(decode=decode)
-
-                       while True:
-                               chunk = log.read(65536)
-                               if not chunk:
-                                       break
-
-                               self.write(chunk)
+                       # Send 404 if there is no log file
+                       except FileNotFoundError as e:
+                               raise tornado.web.HTTPError(404, "Could not find log for %s" % job) from e
 
-               # Send 404 if there is no log file
-               except FileNotFoundError as e:
-                       raise tornado.web.HTTPError(404, "Could not find log for %s" % job) from e
+               # Redirect the client
+               self.redirect(job.log_url, status=307)
 
 
 class AbortHandler(base.BaseHandler):