Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server graceful shutdown #44

Merged
merged 1 commit into from
Jan 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 0.6.2 (Next)

* [#44](https://github.com/dblock/slack-ruby-bot/pull/44): Bot graceful shutdown - [@accessd](https://github.com/accessd).
* Your contribution here.

### 0.6.1 (1/29/2016)
Expand Down
60 changes: 35 additions & 25 deletions lib/slack-ruby-bot/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module SlackRubyBot
class Server
include Loggable
cattr_accessor :hooks
attr_accessor :token
attr_accessor :aliases
attr_accessor :send_gifs
attr_accessor :token, :aliases, :send_gifs

TRAPPED_SIGNALS = %w(INT TERM).freeze

include SlackRubyBot::Hooks::Hello
include SlackRubyBot::Hooks::Message
Expand All @@ -18,24 +18,9 @@ def initialize(options = {})
def run
auth!
loop do
begin
handle_execeptions do
handle_signals
start!
rescue Slack::Web::Api::Error => e
logger.error e
case e.message
when 'migration_in_progress'
sleep 1 # ignore, try again
else
raise e
end
rescue Faraday::Error::TimeoutError, Faraday::Error::ConnectionFailed, Faraday::Error::SSLError => e
logger.error e
sleep 1 # ignore, try again
rescue StandardError => e
logger.error e
raise e
ensure
@client = nil
end
end
end
Expand Down Expand Up @@ -63,11 +48,7 @@ def stop!
end

def restart!(wait = 1)
if @async
start_async
else
start!
end
@async ? start_async : start!
rescue StandardError => e
sleep wait
logger.error "#{e.message}, reconnecting in #{wait} second(s)."
Expand All @@ -77,6 +58,35 @@ def restart!(wait = 1)

private

def handle_execeptions
yield
rescue Slack::Web::Api::Error => e
logger.error e
case e.message
when 'migration_in_progress'
sleep 1 # ignore, try again
else
raise e
end
rescue Faraday::Error::TimeoutError, Faraday::Error::ConnectionFailed, Faraday::Error::SSLError => e
logger.error e
sleep 1 # ignore, try again
rescue StandardError => e
logger.error e
raise e
ensure
@client = nil
end

def handle_signals
TRAPPED_SIGNALS.each do |signal|
Signal.trap(signal) do
stop!
exit
end
end
end

def client
@client ||= begin
client = SlackRubyBot::Client.new(aliases: aliases, send_gifs: send_gifs, token: token)
Expand Down
18 changes: 18 additions & 0 deletions spec/slack-ruby-bot/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,22 @@
end
end
end
context 'exits without error' do
subject do
SlackRubyBot::Server.new(token: 'token')
end
before do
allow(subject).to receive(:auth!)
allow(subject).to receive(:start!)
end
SlackRubyBot::Server::TRAPPED_SIGNALS.each do |signal|
it "if #{signal} signal received" do
server_pid = fork { subject.run }
sleep 0.1
Process.kill(signal, server_pid)
_, status = Process.waitpid2(server_pid)
expect(status.success?).to be true
end
end
end
end