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

Log reconnects #244

Merged
merged 21 commits into from
Mar 25, 2020
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.12.1 (Next)

* [#244](https://github.com/slack-ruby/slack-ruby-bot/pull/244): Change log message when the bot is reconnected - [@wasabigeek](https://github.com/wasabigeek).
* [#209](https://github.com/slack-ruby/slack-ruby-bot/pull/209): Allow `respond_to_slack_message` and `respond_to_slack_messages` without arguments - [@dblock](https://github.com/dblock).
* [#216](https://github.com/slack-ruby/slack-ruby-bot/pull/216): Added `start_typing` RSpec matcher - [@dblock](https://github.com/dblock).
* [#214](https://github.com/slack-ruby/slack-ruby-bot/pull/214): Add passenger deployment documentation - [@cybercrediators](https://github.com/cybercrediators).
Expand Down
20 changes: 18 additions & 2 deletions lib/slack-ruby-bot/hooks/hello.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
module SlackRubyBot
module Hooks
class Hello
attr_accessor :logger
attr_accessor :logger, :connected_at

def initialize(logger)
self.logger = logger
end

def call(client, _data)
return unless client && client.team
logger.info "Successfully connected team #{client.team.name} (#{client.team.id}) to https://#{client.team.domain}.slack.com."
new_connected_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
log = [
'Successfully',
connected_at ? 'reconnected' : 'connected',
"team #{client.team.name} (#{client.team.id}) to https://#{client.team.domain}.slack.com",
connected_at ? "after #{last_connection_till(new_connected_at)}s" : nil
].compact.join(' ') + '.'

logger.info log

self.connected_at = new_connected_at
end

private

def last_connection_till(time)
(time - connected_at).round(2)
end
end
end
Expand Down
49 changes: 49 additions & 0 deletions spec/slack-ruby-bot/hooks/hello_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
describe SlackRubyBot::Hooks::Hello do
let(:logger) { double(:logger, info: nil) }
let(:hello_hook) { described_class.new(logger) }

describe '#call' do
let(:team_name) { 'Example Team' }
let(:team_id) { SecureRandom.uuid }
let(:team_domain) { 'example' }
let(:team) { double(:team, name: team_name, id: team_id, domain: team_domain) }
let(:client) { instance_double(SlackRubyBot::Client, team: team) }

def receive_hello
hello_hook.call(client, double)
end

it 'logs the connection' do
expect(logger).to receive(:info).with("Successfully connected team #{team_name} (#{team_id}) to https://#{team_domain}.slack.com.")
receive_hello
end

context 'with no client' do
let(:client) { nil }
it 'does nothing' do
expect(logger).not_to receive(:info)
receive_hello
end
end

context 'when client has no team' do
let(:team) { nil }
it 'does nothing' do
expect(logger).not_to receive(:info)
receive_hello
end
end

context 'when hook is received multiple times' do
before do
receive_hello
end

it 'logs the reconnections' do
expect(logger).to receive(:info).with(/^Successfully reconnected .+ after \S+s\.$/).twice
receive_hello
receive_hello
end
end
end
end