From: Michael Tremer Date: Fri, 7 Feb 2025 15:07:03 +0000 (+0000) Subject: jobs: Let Apache deliver the log files X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=63127f4f384c9d9b0230f1e9395c9380e49bb92f;p=pbs.git jobs: Let Apache deliver the log files This will be even more efficient than sending them in the main process. Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/jobs.py b/src/buildservice/jobs.py index b3785df0..f38489a5 100644 --- a/src/buildservice/jobs.py +++ b/src/buildservice/jobs.py @@ -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") diff --git a/src/web/jobs.py b/src/web/jobs.py index 09835977..cf623589 100644 --- a/src/web/jobs.py +++ b/src/web/jobs.py @@ -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):