diff --git a/CHANGELOG.md b/CHANGELOG.md index 69b3a217..869ce768 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ All notable changes to this project will be documented in this file. - Implement automatic `api_ping` and `api_notification` pruning (#556) - Update Dockerfile to install apprise (#581) - Improve period and grace controls, allow up to 365 day periods (#281) -- Add SIGTERM handling in sendalerts +- Add SIGTERM handling in sendalerts and sendreports ### Bug Fixes - Fix hc.api.views.ping to handle non-utf8 data in request body (#574) diff --git a/hc/api/management/commands/sendalerts.py b/hc/api/management/commands/sendalerts.py index 77b1f3c5..88a953ea 100644 --- a/hc/api/management/commands/sendalerts.py +++ b/hc/api/management/commands/sendalerts.py @@ -139,7 +139,7 @@ class Command(BaseCommand): return True def on_sigterm(self, *args): - self.stdout.write("received SIGTERM, finishing...\n") + self.stdout.write("Received SIGTERM, finishing...\n") self.sigterm = True def handle(self, use_threads=True, loop=True, *args, **options): @@ -148,22 +148,27 @@ class Command(BaseCommand): self.stdout.write("sendalerts is now running\n") i, sent = 0, 0 - while True: + while not self.sigterm: # Create flips for any checks going down - while self.handle_going_down() and not self.sigterm: + while not self.sigterm and self.handle_going_down(): pass # Process the unprocessed flips - while self.process_one_flip(use_threads) and not self.sigterm: + while not self.sigterm and self.process_one_flip(use_threads): sent += 1 - if not loop or self.sigterm: + if not loop: break - time.sleep(2) - i += 1 + # Sleep for 2 seconds before looking for more work + if not self.sigterm: + i += 2 + time.sleep(2) + + # Print "-- MARK --" approx. every minute so the logs + # have evidence sendalerts is still running: if i % 60 == 0: timestamp = timezone.now().isoformat() self.stdout.write("-- MARK %s --\n" % timestamp) - return "Sent %d alert(s)" % sent + return f"Sent {sent} alert(s)." diff --git a/hc/api/management/commands/sendreports.py b/hc/api/management/commands/sendreports.py index 71e48e90..1ebca59f 100644 --- a/hc/api/management/commands/sendreports.py +++ b/hc/api/management/commands/sendreports.py @@ -1,3 +1,4 @@ +import signal import time from django.core.management.base import BaseCommand @@ -90,22 +91,35 @@ class Command(BaseCommand): return True - def handle(self, *args, **options): + def on_sigterm(self, *args): + self.stdout.write("Received SIGTERM, finishing...\n") + self.sigterm = True + + def handle(self, loop=False, *args, **options): + self.sigterm = False + signal.signal(signal.SIGTERM, self.on_sigterm) + self.stdout.write("sendreports is now running") - while True: + while not self.sigterm: # Monthly reports - while self.handle_one_report(): + while not self.sigterm and self.handle_one_report(): pass # Daily and hourly nags - while self.handle_one_nag(): + while not self.sigterm and self.handle_one_nag(): pass - if not options["loop"]: + if not loop: break + # Sleep for 60 seconds before looking for more work + for i in range(0, 60): + if not self.sigterm: + time.sleep(1) + + # Print "-- MARK --" approx. every minute so the logs + # have evidence sendreports is still running: formatted = timezone.now().isoformat() self.stdout.write("-- MARK %s --" % formatted) - # Sleep for 1 minute before looking for more work - time.sleep(60) + return "Done."