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 logging
import time import signal
import threading
try: try:
from dotenv import load_dotenv from dotenv import load_dotenv
@@ -26,6 +27,15 @@ def main():
state_file=settings.state_file, state_file=settings.state_file,
node_name=settings.node_name, 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") print(f"[*] Agent started. Check interval: {settings.check_interval}s")
logging.info( logging.info(
@@ -34,7 +44,7 @@ def main():
f"log_path={settings.log_path} state_file={settings.state_file}" f"log_path={settings.log_path} state_file={settings.state_file}"
) )
while True: while not stop_event.is_set():
try: try:
is_synced, count, drift = auditor.reconcile() is_synced, count, drift = auditor.reconcile()
reporter.update_status(is_synced, count) reporter.update_status(is_synced, count)
@@ -56,7 +66,7 @@ def main():
f"extra={drift['extra_count']}, metadata={drift['metadata_drift_count']})." 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( if should_heal(
history_file, history_file,
@@ -88,7 +98,7 @@ def main():
logging.info(f"Audit Success: {count} IPs in sync.") logging.info(f"Audit Success: {count} IPs in sync.")
print(f"[*] Sleeping for {settings.check_interval} seconds...") print(f"[*] Sleeping for {settings.check_interval} seconds...")
time.sleep(settings.check_interval) stop_event.wait(settings.check_interval)
except KeyboardInterrupt: except KeyboardInterrupt:
logging.info("Agent shutting down manually.") logging.info("Agent shutting down manually.")
@@ -103,7 +113,10 @@ def main():
"Reporter update failed during exception handling: " "Reporter update failed during exception handling: "
f"{reporter_error}" f"{reporter_error}"
) )
time.sleep(60) if stop_event.wait(60):
break
logging.info("Agent shutdown complete.")
if __name__ == "__main__": if __name__ == "__main__":
+5 -1
View File
@@ -42,6 +42,10 @@ class LocalStateReporter:
except Exception as e: except Exception as e:
logging.error(f"Reporter: Failed to write state file: {e}") logging.error(f"Reporter: Failed to write state file: {e}")
def get_history_file(self):
base, _ = os.path.splitext(self.state_file)
return f"{base}.history"
def log_heal_event( def log_heal_event(
self, self,
kernel_count, kernel_count,
@@ -56,7 +60,7 @@ class LocalStateReporter:
""" """
Records a specific healing event in a persistent history log. Records a specific healing event in a persistent history log.
""" """
history_file = self.state_file.replace(".json", ".history") history_file = self.get_history_file()
timestamp = datetime.now().isoformat() timestamp = datetime.now().isoformat()
try: try: