Merge pull request #13858 from jessicamack/13322-catch-sigterm

Catch SIGTERM or SIGINT and send offline message
This commit is contained in:
jessicamack
2023-04-26 12:24:34 -04:00
committed by GitHub

View File

@@ -2,6 +2,8 @@ import json
import logging import logging
import os import os
import time import time
import signal
import sys
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.conf import settings from django.conf import settings
@@ -50,6 +52,11 @@ class Command(BaseCommand):
} }
return json.dumps(payload) return json.dumps(payload)
def notify_listener_and_exit(self, *args):
with pg_bus_conn(new_connection=False) as conn:
conn.notify('web_heartbeet', self.construct_payload(action='offline'))
sys.exit(0)
def do_hearbeat_loop(self): def do_hearbeat_loop(self):
with pg_bus_conn(new_connection=True) as conn: with pg_bus_conn(new_connection=True) as conn:
while True: while True:
@@ -57,10 +64,10 @@ class Command(BaseCommand):
conn.notify('web_heartbeet', self.construct_payload()) conn.notify('web_heartbeet', self.construct_payload())
time.sleep(settings.BROADCAST_WEBSOCKET_BEACON_FROM_WEB_RATE_SECONDS) time.sleep(settings.BROADCAST_WEBSOCKET_BEACON_FROM_WEB_RATE_SECONDS)
# TODO: Send a message with action=offline if we notice a SIGTERM or SIGINT
# (wsrelay can use this to remove the node quicker)
def handle(self, *arg, **options): def handle(self, *arg, **options):
self.print_banner() self.print_banner()
signal.signal(signal.SIGTERM, self.notify_listener_and_exit)
signal.signal(signal.SIGINT, self.notify_listener_and_exit)
# Note: We don't really try any reconnect logic to pg_notify here, # Note: We don't really try any reconnect logic to pg_notify here,
# just let supervisor restart if we fail. # just let supervisor restart if we fail.