add graceful shutdown

This commit is contained in:
stryan 2022-02-11 16:33:24 -05:00
parent 84c684b182
commit 5f8bcd2ffa
2 changed files with 26 additions and 0 deletions

View File

@ -19,6 +19,10 @@ class Folder
end end
end end
def close()
self.update()
end
def num_subs() def num_subs()
@submissions.length @submissions.length
end end

22
main.rb
View File

@ -10,6 +10,8 @@ require 'logger'
require 'astro/moon' require 'astro/moon'
require './folder.rb' require './folder.rb'
logger = Logger.new(STDOUT) logger = Logger.new(STDOUT)
logger.level = Logger::INFO logger.level = Logger::INFO
@ -17,6 +19,16 @@ config_obj = YAML::load_file( './config.yaml' )
logger.info("loaded config") logger.info("loaded config")
bot = Discordrb::Commands::CommandBot.new token: config_obj["bot_token"], prefix: "!folder" bot = Discordrb::Commands::CommandBot.new token: config_obj["bot_token"], prefix: "!folder"
folder = Folder.new folder = Folder.new
def shutdown
log.info("shutting down")
if bot.connected
bot.stop
end
folder.close
end
config_obj['steves'].each do |steve| config_obj['steves'].each do |steve|
folder.add_steve(steve) folder.add_steve(steve)
end end
@ -89,7 +101,17 @@ bot.message(private: true) do |event|
folder.update() folder.update()
end end
#set up signals
Signal.trap("INT") {
shut_down
exit
}
# Trap `Kill `
Signal.trap("TERM") {
shut_down
exit
}
logger.info("Logging into discord") logger.info("Logging into discord")