From 5f8bcd2ffa42d6a69ae60957be3ff30097c50693 Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 11 Feb 2022 16:33:24 -0500 Subject: [PATCH] add graceful shutdown --- folder.rb | 4 ++++ main.rb | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/folder.rb b/folder.rb index 94c26cb..126c4a0 100644 --- a/folder.rb +++ b/folder.rb @@ -19,6 +19,10 @@ class Folder end end + def close() + self.update() + end + def num_subs() @submissions.length end diff --git a/main.rb b/main.rb index 70535aa..654916e 100755 --- a/main.rb +++ b/main.rb @@ -10,6 +10,8 @@ require 'logger' require 'astro/moon' require './folder.rb' + + logger = Logger.new(STDOUT) logger.level = Logger::INFO @@ -17,6 +19,16 @@ config_obj = YAML::load_file( './config.yaml' ) logger.info("loaded config") bot = Discordrb::Commands::CommandBot.new token: config_obj["bot_token"], prefix: "!folder" folder = Folder.new + +def shutdown + log.info("shutting down") + if bot.connected + bot.stop + end + folder.close +end + + config_obj['steves'].each do |steve| folder.add_steve(steve) end @@ -89,7 +101,17 @@ bot.message(private: true) do |event| folder.update() end +#set up signals +Signal.trap("INT") { + shut_down + exit +} +# Trap `Kill ` +Signal.trap("TERM") { + shut_down + exit +} logger.info("Logging into discord")