refactor: wrap start function execution in try-except block

Improve error handling by catching Failed exceptions during the run and ensuring the running flag is properly released on failure. This prevents the application from remaining in a locked state after critical errors.
This commit is contained in:
bobokun 2025-09-15 17:10:51 -04:00
parent 3a468c591b
commit a1b181511d
No known key found for this signature in database
GPG key ID: B73932169607D927
2 changed files with 151 additions and 150 deletions

View file

@ -1 +1 @@
4.6.3-develop3 4.6.3-develop4

View file

@ -455,6 +455,8 @@ def start():
# Acquire lock only briefly to set the flag, then release immediately # Acquire lock only briefly to set the flag, then release immediately
with is_running_lock: with is_running_lock:
is_running.value = True # Set flag to indicate a run is in progress is_running.value = True # Set flag to indicate a run is in progress
try:
start_time = datetime.now() start_time = datetime.now()
args["time"] = start_time.strftime("%H:%M") args["time"] = start_time.strftime("%H:%M")
args["time_obj"] = start_time args["time_obj"] = start_time
@ -569,18 +571,8 @@ def start():
body = logger.separator(msg)[0] body = logger.separator(msg)[0]
return body return body
try:
cfg = Config(default_dir, args) cfg = Config(default_dir, args)
qbit_manager = cfg.qbt qbit_manager = cfg.qbt
except Exception as ex:
logger.stacktrace()
logger.print_line(ex, "CRITICAL")
logger.print_line("Exiting scheduled Run.", "CRITICAL")
finished_run(next_scheduled_run_info_shared)
# Release flag when config validation fails
with is_running_lock:
is_running.value = False
return None
if qbit_manager: if qbit_manager:
# Execute qBittorrent commands using shared function with error handling # Execute qBittorrent commands using shared function with error handling
@ -615,6 +607,15 @@ def start():
with is_running_lock: with is_running_lock:
is_running.value = False is_running.value = False
logger.info("Released lock for web API requests despite webhook error") logger.info("Released lock for web API requests despite webhook error")
except Failed as ex:
logger.stacktrace()
logger.print_line(ex, "CRITICAL")
logger.print_line("Exiting scheduled Run.", "CRITICAL")
finished_run(next_scheduled_run_info_shared)
# Release flag when any Failed exception occurs during the run
with is_running_lock:
is_running.value = False
return None
def end(): def end():