From 6442bb06b9be885ec6f873d13b84c816ceae71e6 Mon Sep 17 00:00:00 2001 From: Nikolay Korotkiy Date: Wed, 24 Jul 2024 01:30:07 +0400 Subject: [PATCH 1/2] Fix exception handling --- debian/changelog | 6 ++++++ wb-cloud-agent | 5 ++++- wb/cloud_agent/main.py | 40 +++++++++++++++++++++++++++------------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/debian/changelog b/debian/changelog index 96ae439..3ec6a44 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +wb-cloud-agent (1.5.6) stable; urgency=medium + + * Fix exception handling + + -- Nikolay Korotkiy Mon, 22 Jul 2024 15:45:00 +0400 + wb-cloud-agent (1.5.5) stable; urgency=medium * Fix preinst hook diff --git a/wb-cloud-agent b/wb-cloud-agent index d37b603..00319e7 100755 --- a/wb-cloud-agent +++ b/wb-cloud-agent @@ -6,4 +6,7 @@ import sys from wb.cloud_agent import main if __name__ == "__main__": - sys.exit(main.main()) + try: + sys.exit(main.main()) + except KeyboardInterrupt: + sys.exit(0) diff --git a/wb/cloud_agent/main.py b/wb/cloud_agent/main.py index 2ac7c54..dc5f682 100644 --- a/wb/cloud_agent/main.py +++ b/wb/cloud_agent/main.py @@ -5,6 +5,7 @@ import logging import os import subprocess +import sys import threading import time from contextlib import ExitStack @@ -153,7 +154,12 @@ def do_curl(settings: AppSettings, method="get", endpoint="", params=None): url, ] - result = subprocess.run(command, timeout=360, check=True, capture_output=True) + try: + result = subprocess.run(command, timeout=360, check=True, capture_output=True) + except subprocess.CalledProcessError as e: + raise ValueError("Error during request: " + str(e)) + except subprocess.TimeoutExpired as e: + raise ValueError("Timeout expired: " + str(e)) decoded_result = result.stdout.decode("utf-8") split_result = decoded_result.split(data_delimiter) @@ -473,20 +479,28 @@ def main(): mqtt.on_connect = on_connect mqtt.on_message = on_message - if hasattr(options, "func"): - mqtt.start() - return options.func(options, settings, mqtt) + try: + if hasattr(options, "func"): + mqtt.start() + return options.func(options, settings, mqtt) - if not options.daemon: + if not options.daemon: + mqtt.start() + make_start_up_request(settings, mqtt) + return show_activation_link(settings) + + mqtt.will_set(settings.MQTT_PREFIX + "/controls/status", "stopped", retain=True, qos=2) mqtt.start() + publish_ctrl(settings, mqtt, "status", "starting") make_start_up_request(settings, mqtt) - return show_activation_link(settings) + send_agent_version(settings) + update_providers_list(settings, mqtt) - mqtt.will_set(settings.MQTT_PREFIX + "/controls/status", "stopped", retain=True, qos=2) - mqtt.start() - publish_ctrl(settings, mqtt, "status", "starting") - make_start_up_request(settings, mqtt) - send_agent_version(settings) - update_providers_list(settings, mqtt) + run_daemon(mqtt, settings) - run_daemon(mqtt, settings) + except (ConnectionError, ConnectionRefusedError): + logging.error(f"Cannot connect to broker {settings.BROKER_URL}") + sys.exit(1) + except Exception as e: + logging.error(e) + sys.exit(1) From aa0822e4e8b3d5fd673539d85d2bdb74ce6702a4 Mon Sep 17 00:00:00 2001 From: Nikolay Korotkiy Date: Tue, 6 Aug 2024 03:36:53 +0400 Subject: [PATCH 2/2] Fixup --- wb-cloud-agent | 5 +---- wb/cloud_agent/main.py | 4 ++++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/wb-cloud-agent b/wb-cloud-agent index 00319e7..d37b603 100755 --- a/wb-cloud-agent +++ b/wb-cloud-agent @@ -6,7 +6,4 @@ import sys from wb.cloud_agent import main if __name__ == "__main__": - try: - sys.exit(main.main()) - except KeyboardInterrupt: - sys.exit(0) + sys.exit(main.main()) diff --git a/wb/cloud_agent/main.py b/wb/cloud_agent/main.py index 7861fdd..3113a77 100644 --- a/wb/cloud_agent/main.py +++ b/wb/cloud_agent/main.py @@ -4,6 +4,7 @@ import json import logging import os +import signal import subprocess import sys import threading @@ -481,6 +482,9 @@ def main(): mqtt.on_connect = on_connect mqtt.on_message = on_message + signal.signal(signal.SIGINT, signal.SIG_DFL) + signal.signal(signal.SIGTERM, signal.SIG_DFL) + try: if hasattr(options, "func"): mqtt.start()