From 5790b9134ea8cb63302b022ba5b6ca351159f863 Mon Sep 17 00:00:00 2001 From: Julian Mutter Date: Fri, 6 Oct 2023 16:02:16 +0200 Subject: [PATCH] Add timeout error handling --- transcoder/transcode_dvd.py | 62 ++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/transcoder/transcode_dvd.py b/transcoder/transcode_dvd.py index d92aa0a..0ebb35e 100755 --- a/transcoder/transcode_dvd.py +++ b/transcoder/transcode_dvd.py @@ -279,13 +279,19 @@ def transcode_episode(dvd: Dvd, season, episode, title_number): # Returns success def run_and_log_handbrake(dvd: Dvd, command) -> bool: with open(dvd.log_file, "a") as log_file: - proc = subprocess.run( - command, - stderr=subprocess.STDOUT, - stdout=log_file, - timeout=TRANSCODE_TIMEOUT_SECONDS, - ) - if proc.returncode != 0: + try: + proc = subprocess.run( + command, + stderr=subprocess.STDOUT, + stdout=log_file, + timeout=TRANSCODE_TIMEOUT_SECONDS, + ) + if proc.returncode != 0: + return False + except subprocess.TimeoutExpired: + log_file.write( + f"Transcoding timeout of {TRANSCODE_TIMEOUT_SECONDS}s reached!" + ) return False if check_handbrake_log_for_error(dvd): @@ -309,24 +315,30 @@ def find_series_titles(dvd: Dvd): return read_titles_from_titles_file(dvd) with open(dvd.log_file, "w") as log_file: - proc = subprocess.run( - [ - "nice", - "-n", - str(HANDBRAKE_PROCESS_NICE_LEVEL), - "HandBrakeCLI", - "--input", - dvd.dvd_path, - "-t", - "0", - "--min-duration", - str(MIN_EPISODES_DURATION_SECONDS), - "--json", - ], - stdout=subprocess.PIPE, - stderr=log_file, - timeout=TRANSCODE_TIMEOUT_SECONDS, - ) + try: + proc = subprocess.run( + [ + "nice", + "-n", + str(HANDBRAKE_PROCESS_NICE_LEVEL), + "HandBrakeCLI", + "--input", + dvd.dvd_path, + "-t", + "0", + "--min-duration", + str(MIN_EPISODES_DURATION_SECONDS), + "--json", + ], + stdout=subprocess.PIPE, + stderr=log_file, + timeout=TRANSCODE_TIMEOUT_SECONDS, + ) + except subprocess.TimeoutExpired: + log_file.write( + f"Transcoding timeout of {TRANSCODE_TIMEOUT_SECONDS}s reached!" + ) + return None if proc.returncode != 0: return None