Skip to content

Instantly share code, notes, and snippets.

@tinnvec
Last active December 24, 2015 15:04
Show Gist options
  • Select an option

  • Save tinnvec/0875dc6bd4682f7faa94 to your computer and use it in GitHub Desktop.

Select an option

Save tinnvec/0875dc6bd4682f7faa94 to your computer and use it in GitHub Desktop.
Example of trapping signals to gracefully shut down Cinch bot (for ruby 2+)
require 'cinch'
require 'thread'
mutex = Mutex.new
quit_signalled = ConditionVariable.new
signal_received = nil
bot = Cinch::Bot.new do
configure do |c|
c.nick = 'SignalQuitBot'
c.user = 'SignalQuitBot'
c.realname = 'SignalQuitBot'
c.server = 'irc.freenode.net'
c.port = 6697
c.ssl.use = true
c.sasl.username = 'l4bot'
c.sasl.password = IO.read("#{__dir__}/freenode-config/sasl-password").chomp
c.channels = ['##leftylink']
c.verbose = true
end
end
%w[QUIT INT].each do |signal_name|
Signal.trap(signal_name) do |signal_number|
signal_received = signal_number
quit_signalled.signal
end
end
Thread.new do
mutex.synchronize do
quit_signalled.wait(mutex) until signal_received
bot.quit("Received #{Signal.signame(signal_received)}")
end
end
bot.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment