From bc2d127c27bd09020e0e8bce4b08c4927e8cbc29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Sat, 6 Nov 2021 19:54:41 +0200 Subject: [PATCH] Add SIGTERM handling in sendalerts --- CHANGELOG.md | 1 + hc/api/management/commands/sendalerts.py | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c7ead2f..69b3a217 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +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 ### 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 b3624ea2..77b1f3c5 100644 --- a/hc/api/management/commands/sendalerts.py +++ b/hc/api/management/commands/sendalerts.py @@ -1,4 +1,5 @@ from datetime import timedelta as td +import signal import time from threading import Thread @@ -137,20 +138,26 @@ class Command(BaseCommand): return True + def on_sigterm(self, *args): + self.stdout.write("received SIGTERM, finishing...\n") + self.sigterm = True + def handle(self, use_threads=True, loop=True, *args, **options): - self.stdout.write("sendalerts is now running\n") + self.sigterm = False + signal.signal(signal.SIGTERM, self.on_sigterm) + self.stdout.write("sendalerts is now running\n") i, sent = 0, 0 while True: # Create flips for any checks going down - while self.handle_going_down(): + while self.handle_going_down() and not self.sigterm: pass # Process the unprocessed flips - while self.process_one_flip(use_threads): + while self.process_one_flip(use_threads) and not self.sigterm: sent += 1 - if not loop: + if not loop or self.sigterm: break time.sleep(2)