fix(main,reporter): handle SIGTERM gracefully and normalize history file path

This commit is contained in:
patrick committed 2026-03-05 20:12:17 -05:00
1 parent 9b53a4c491
commit 3c6c4e384b
2 files changed
+23 -6

No files matched your search

+18 -5
View File
@@ -1,5 +1,6 @@
import logging
import time
import signal
import threading
try:
from dotenv import load_dotenv
@@ -26,6 +27,15 @@ def main():
state_file=settings.state_file,
node_name=settings.node_name,
)
stop_event = threading.Event()
def _handle_stop_signal(signum, _frame):
sig_name = signal.Signals(signum).name
logging.info(f"Stop signal received: {sig_name}. Shutting down.")
stop_event.set()
signal.signal(signal.SIGTERM, _handle_stop_signal)
signal.signal(signal.SIGINT, _handle_stop_signal)
print(f"[*] Agent started. Check interval: {settings.check_interval}s")
logging.info(
@@ -34,7 +44,7 @@ def main():
f"log_path={settings.log_path} state_file={settings.state_file}"
)
while True:
while not stop_event.is_set():
try:
is_synced, count, drift = auditor.reconcile()
reporter.update_status(is_synced, count)
@@ -56,7 +66,7 @@ def main():
f"extra={drift['extra_count']}, metadata={drift['metadata_drift_count']})."
)
history_file = reporter.state_file.replace(".json", ".history")
history_file = reporter.get_history_file()
if should_heal(
history_file,
@@ -88,7 +98,7 @@ def main():
logging.info(f"Audit Success: {count} IPs in sync.")
print(f"[*] Sleeping for {settings.check_interval} seconds...")
time.sleep(settings.check_interval)
stop_event.wait(settings.check_interval)
except KeyboardInterrupt:
logging.info("Agent shutting down manually.")
@@ -103,7 +113,10 @@ def main():
"Reporter update failed during exception handling: "
f"{reporter_error}"
)
time.sleep(60)
if stop_event.wait(60):
break
logging.info("Agent shutdown complete.")
if __name__ == "__main__":