Skip to content

Commit 61d1c90

Browse files
authored
Merge pull request #79 from dblock/fix-78
Fix #78: ping thread terminates on a failed restart.
2 parents 861d974 + 746dea1 commit 61d1c90

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### 0.8.1 (Next)
44

5+
* [#79](https://github.com/slack-ruby/slack-ruby-bot-server/pull/79): Fix: ping worker terminates on a failed restart - [@dblock](https://github.com/dblock).
56
* Your contribution here.
67

78
#### 0.8.0 (2018/9/8)

lib/slack-ruby-bot-server/ping.rb

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,55 @@ def down!
6868
@error_count += 1
6969
return true if retries_left?
7070
restart!
71-
false
7271
end
7372

7473
def restart!
7574
logger.warn "RESTART: #{owner}"
76-
begin
77-
connection.close
78-
rescue Async::Wrapper::Cancelled
79-
# ignore, from connection.close
80-
end
75+
close_connection
76+
close_driver
77+
emit_close
78+
false
79+
rescue StandardError => e
80+
logger.warn "Error restarting team #{owner.id}: #{e.message}."
81+
true
82+
end
83+
84+
def close_connection
85+
return unless connection
86+
connection.close
87+
rescue Async::Wrapper::Cancelled
88+
# ignore, from connection.close
89+
rescue StandardError => e
90+
logger.warn "Error closing connection for #{owner.id}: #{e.message}."
91+
raise e
92+
end
93+
94+
def close_driver
95+
return unless driver
8196
driver.close
97+
rescue StandardError => e
98+
logger.warn "Error closing driver for #{owner.id}: #{e.message}."
99+
raise e
100+
end
101+
102+
def emit_close
103+
return unless driver
82104
driver.emit(:close, WebSocket::Driver::CloseEvent.new(1001, 'bot offline'))
83105
rescue StandardError => e
84-
logger.warn "Error restarting team #{owner.id}: #{e.message}."
106+
logger.warn "Error sending :close event to driver for #{owner.id}: #{e.message}."
107+
raise e
85108
end
86109

87110
def ping_interval
88111
options[:ping_interval] || 60
89112
end
90113

91114
def retries_left?
92-
retries_left >= 0
115+
retry_count - error_count >= 0
93116
end
94117

95118
def retries_left
96-
retry_count - error_count
119+
[0, retry_count - error_count].max
97120
end
98121

99122
def retry_count

spec/slack-ruby-bot-server/ping_spec.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,43 @@
6060
expect(subject.send(:error_count)).to eq 0
6161
end
6262
end
63+
64+
context 'after two more failed checks' do
65+
before do
66+
allow(subject).to receive(:online?).and_return(false).twice
67+
2.times { subject.send(:check!) }
68+
end
69+
70+
it 'does not decrement retries left below zero' do
71+
expect(subject.send(:retries_left)).to eq 0
72+
end
73+
74+
it 'sets error count' do
75+
expect(subject.send(:error_count)).to eq 3
76+
end
77+
end
6378
end
6479

6580
it 'terminates the ping worker after account_inactive' do
6681
allow(subject).to receive(:online?).and_raise('account_inactive')
6782
subject.start!
6883
end
6984

70-
it 'terminates after a number of retries' do
85+
it 'restarts and terminates after a number of retries' do
7186
allow(subject).to receive(:online?).and_return(false)
7287
expect(subject).to receive(:check!).exactly(3).times.and_call_original
88+
expect(subject).to receive(:restart!).and_call_original
89+
expect(subject).to receive(:close_connection).and_call_original
90+
expect(subject).to receive(:close_driver).and_call_original
91+
expect(subject).to receive(:emit_close).and_call_original
92+
subject.start!
93+
end
94+
95+
it 'does not terminate upon a failed restart' do
96+
allow(subject).to receive(:online?).and_return(false)
97+
allow(subject).to receive(:close_connection) { raise 'error closing connection' }
98+
expect(subject).to receive(:check!).exactly(3).times.and_call_original
99+
expect(subject).to receive(:check!).and_return(false)
73100
subject.start!
74101
end
75102
end

0 commit comments

Comments
 (0)