Browse Source

Add SIGTERM handling in sendreports

master
Pēteris Caune 3 years ago
parent
commit
1299738f50
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
3 changed files with 35 additions and 16 deletions
  1. +1
    -1
      CHANGELOG.md
  2. +13
    -8
      hc/api/management/commands/sendalerts.py
  3. +21
    -7
      hc/api/management/commands/sendreports.py

+ 1
- 1
CHANGELOG.md View File

@ -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)


+ 13
- 8
hc/api/management/commands/sendalerts.py View File

@ -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)."

+ 21
- 7
hc/api/management/commands/sendreports.py View File

@ -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."

Loading…
Cancel
Save