Captures the stdout and stderr of the pre/post scripts into the log

This commit is contained in:
Trenton Holmes
2022-11-09 20:11:36 -08:00
committed by Trenton H
parent 50a211f367
commit 9a47963fd5
2 changed files with 95 additions and 19 deletions

View File

@@ -2,7 +2,8 @@ import datetime
import hashlib
import os
import uuid
from subprocess import Popen
from subprocess import CompletedProcess
from subprocess import run
from typing import Optional
from typing import Type
@@ -148,14 +149,21 @@ class Consumer(LoggingMixin):
script_env["DOCUMENT_SOURCE_PATH"] = filepath_arg
try:
Popen(
(
completed_proc = run(
args=[
settings.PRE_CONSUME_SCRIPT,
filepath_arg,
),
],
env=script_env,
).wait()
except Exception as e:
capture_output=True,
)
self._log_script_outputs(completed_proc)
# Raises exception on non-zero output
completed_proc.check_returncode()
except Exception as e: # pragma: nocover
self._fail(
MESSAGE_PRE_CONSUME_SCRIPT_ERROR,
f"Error while executing pre-consume script: {e}",
@@ -208,8 +216,8 @@ class Consumer(LoggingMixin):
script_env["DOCUMENT_ORIGINAL_FILENAME"] = str(document.original_filename)
try:
Popen(
(
completed_proc = run(
args=[
settings.POST_CONSUME_SCRIPT,
str(document.pk),
document.get_public_filename(),
@@ -219,10 +227,17 @@ class Consumer(LoggingMixin):
reverse("document-thumb", kwargs={"pk": document.pk}),
str(document.correspondent),
str(",".join(document.tags.all().values_list("name", flat=True))),
),
],
env=script_env,
).wait()
except Exception as e:
capture_output=True,
)
self._log_script_outputs(completed_proc)
# Raises exception on non-zero output
completed_proc.check_returncode()
except Exception as e: # pragma: nocover
self._fail(
MESSAGE_POST_CONSUME_SCRIPT_ERROR,
f"Error while executing post-consume script: {e}",
@@ -510,3 +525,31 @@ class Consumer(LoggingMixin):
with open(source, "rb") as read_file:
with open(target, "wb") as write_file:
write_file.write(read_file.read())
def _log_script_outputs(self, completed_process: CompletedProcess):
"""
Decodes a process stdout and stderr streams and logs them to the main log
"""
# Log what the script exited as
self.log(
"info",
f"{completed_process.args[0]} exited {completed_process.returncode}",
)
# Decode the output (if any)
stdout_str = completed_process.stdout.decode("utf8", errors="ignore").split(
"\n",
)
stderr_str = completed_process.stderr.decode("utf8", errors="ignore").split(
"\n",
)
if len(stdout_str):
self.log("info", "Script stdout:")
for line in stdout_str:
self.log("info", line)
if len(stderr_str):
self.log("warning", "Script stderr:")
for line in stderr_str:
self.log("warning", line)