From 367c1b244e0ba78bfe76c1a70d33ca066a4a99ea Mon Sep 17 00:00:00 2001 From: Freeyorp Date: Sat, 20 Apr 2024 19:07:04 +0000 Subject: Implement basic systemd keepalives There are greater underlying issues here. But this serves as a kludge while we're busy with fire extinguishers elsewhere. --- main.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/main.py b/main.py index cd4397e..cdb3e71 100755 --- a/main.py +++ b/main.py @@ -46,6 +46,7 @@ import utils import eliza from onlineusers import SqliteDbManager from ircbot import IRCBot +from sdnotify import SystemdNotifier chatbot = eliza.eliza() shop_broadcaster = utils.Broadcast() @@ -59,6 +60,10 @@ ItemLog = utils.ItemLog() logger = logging.getLogger('ManaLogger') db_manager = SqliteDbManager(config.sqlite3_dbfile) ircbot = IRCBot() +sd = SystemdNotifier() + +# How long to wait between WATCHDOG=1 systemd keepalives, in seconds +sd_min_keepalive_rate = 5 def process_whisper(nick, msg, mapserv): msg = filter(lambda x: x in utils.allowed_chars, msg) @@ -854,6 +859,15 @@ def main(): ircbot.broadcastFunc = broadcast_from_irc ircbot.start() + # Functionality for systemd watchdog keepalives + last_notify = None + def notify_systemd(): + global last_sd_notify + sd.notify("WATCHDOG=1") + last_sd_notify = time.time() + return True + notify_systemd() + # Map server packet loop print "Entering map packet loop\n"; while True: @@ -862,6 +876,10 @@ def main(): break pb.feed(data) + # If it's been more than five seconds since we last notified systemd that we're still alive, do so now. + if time.time() - last_notify > sd_min_keepalive_rate: + notify_systemd() + # For unfinished trades - one way to distrupt service would be leaving a trade active. if trader_state.Trading.test(): if time.time() - trader_state.timer > 2*60: -- cgit v1.2.3-60-g2f50