Skip to content

Commit

Permalink
Added globally configurable ping options and a way to disable ping.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Aug 21, 2018
1 parent edc8647 commit 44c13b9
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-05-06 23:29:13 -0400 using RuboCop version 0.55.0.
# on 2018-08-21 19:45:36 -0400 using RuboCop version 0.55.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ end

Each `SlackRubyBotServer::Server` instance will start a ping worker that will periodically check the online status of the bot and will forcefully close the connection if the bot goes offline, causing an automatic reconnect.

You can configure ping interval and disable the ping worker.

```ruby
SlackRubyBotServer.configure do |config|
config.ping = {
enabled: true, # set to false to disable the ping worker
ping_interval: 30, # interval in seconds
retry_count: 3 # number of unsuccessful retries until a restart
}
end
```

### Access Tokens

By default the implementation of [Team](lib/slack-ruby-bot-server/models/team) stores a `bot_access_token` that grants a certain amount of privileges to the bot user as described in [Slack OAuth Docs](https://api.slack.com/docs/oauth). You may not want a bot user at all, or may require different auth scopes, such as `users.profile:read` to access user profile information via `Slack::Web::Client#users_profile_get`. To obtain the non-bot access token make the following changes.
Expand Down
18 changes: 18 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
Upgrading Slack-Ruby-Bot-Server
===============================

### Upgrading to >= 0.7.0

#### New Ping Worker

Version 0.7.0 will automatically start a ping worker that checks for the bot's online status and forcefully terminate and restart disconnected bots. If you would like to disable this behavior, set the ping enabled option to false.

```ruby
SlackRubyBotServer.configure do |config|
config.ping = {
enabled: false
}
end
```

If you are currently using a custom ping worker as suggested in [slack-ruby-client#208](https://github.com/slack-ruby/slack-ruby-client/issues/208), delete it.

See [#74](https://github.com/slack-ruby/slack-ruby-bot-server/pull/74) for more information.

### Upgrading to >= 0.6.0

#### Mongoid and ActiveRecord support
Expand Down
2 changes: 2 additions & 0 deletions lib/slack-ruby-bot-server/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module Config
extend self

attr_accessor :server_class
attr_accessor :ping
attr_accessor :database_adapter

def reset!
self.ping = nil
self.server_class = SlackRubyBotServer::Server
self.database_adapter = if defined?(::Mongoid)
:mongoid
Expand Down
11 changes: 10 additions & 1 deletion lib/slack-ruby-bot-server/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Server < SlackRubyBot::Server
def initialize(attrs = {})
attrs = attrs.dup
@team = attrs.delete(:team)
@ping_options = attrs.delete(:ping) || {}
raise 'Missing team' unless @team
attrs[:token] = @team.token
super(attrs)
Expand All @@ -23,10 +24,18 @@ def restart!(wait = 1)

private

attr_reader :ping_options

def create_ping
return unless !ping_options.key?(:enabled) || ping_options[:enabled]
SlackRubyBotServer::Ping.new(client, ping_options)
end

def open!
client.owner = team
client.on :open do |_event|
SlackRubyBotServer::Ping.new(client).start!
worker = create_ping
worker.start! if worker
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/slack-ruby-bot-server/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def create!(team)
def start!(team)
run_callbacks :starting, team
logger.info "Starting team #{team}."
server = SlackRubyBotServer::Config.server_class.new(team: team)
options = { team: team }
options[:ping] = SlackRubyBotServer::Config.ping if SlackRubyBotServer::Config.ping
server = SlackRubyBotServer::Config.server_class.new(options)
start_server! team, server
run_callbacks :started, team
server
Expand Down
36 changes: 31 additions & 5 deletions spec/slack-ruby-bot-server/ping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

describe SlackRubyBotServer::Ping do
let(:team) { Team.new(token: 'token') }
let(:server) { SlackRubyBotServer::Server.new(team: team) }
let(:client) { server.send(:client) }
let(:options) { {} }
let(:server) { SlackRubyBotServer::Server.new({ team: team }.merge(options)) }
let(:client) { server.send(:client) }

subject do
SlackRubyBotServer::Ping.new(client, options)
server.send(:create_ping)
end

context 'with defaults' do
Expand Down Expand Up @@ -77,7 +77,7 @@

context 'with options' do
context 'ping interval' do
let(:options) { { ping_interval: 42 } }
let(:options) { { ping: { ping_interval: 42 } } }

it 'is used' do
expect(subject.send(:ping_interval)).to eq 42
Expand All @@ -87,7 +87,7 @@
end

context 'retry count' do
let(:options) { { retry_count: 42 } }
let(:options) { { ping: { retry_count: 42 } } }

it 'is set' do
expect(subject.send(:retry_count)).to eq 42
Expand All @@ -97,5 +97,31 @@
expect(subject.send(:retries_left)).to eq 42
end
end

context 'enabled' do
context 'nil' do
let(:options) { { ping: { enabled: nil } } }

it 'does not create a worker' do
expect(subject).to be_nil
end
end

context 'false' do
let(:options) { { ping: { enabled: false } } }

it 'does not create a worker' do
expect(subject).to be_nil
end
end

context 'true' do
let(:options) { { ping: { enabled: true } } }

it 'creates a worker' do
expect(subject).to_not be_nil
end
end
end
end
end
15 changes: 15 additions & 0 deletions spec/slack-ruby-bot-server/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ def initialize(options = {})
SlackRubyBotServer::Service.instance.stop!(team)
end
end
context 'overriding ping' do
after do
SlackRubyBotServer.config.reset!
end
it 'creates an instance of server class' do
expect(SlackRubyBotServer::Server).to receive(:new).with(team: team, ping: { retry_interval: 42 }).and_call_original
allow_any_instance_of(SlackRubyBotServer::Server).to receive(:start_async)
allow_any_instance_of(SlackRubyBotServer::Server).to receive(:stop!)
SlackRubyBotServer.configure do |config|
config.ping = { retry_interval: 42 }
end
SlackRubyBotServer::Service.instance.start!(team)
SlackRubyBotServer::Service.instance.stop!(team)
end
end
context 'callbacks' do
before do
@events = []
Expand Down

0 comments on commit 44c13b9

Please sign in to comment.