Skip to content

Commit 2de6062

Browse files
authored
Merge pull request #75 from dblock/async
Use async-websocket instead of celluloid-io.
2 parents 74a5164 + b902846 commit 2de6062

File tree

10 files changed

+67
-38
lines changed

10 files changed

+67
-38
lines changed

.rubocop_todo.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2018-08-22 08:29:35 -0400 using RuboCop version 0.58.2.
3+
# on 2018-09-08 18:09:56 -0400 using RuboCop version 0.58.2.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -16,6 +16,11 @@ Layout/IndentHeredoc:
1616
- 'sample_apps/sample_app_activerecord/commands/help.rb'
1717
- 'sample_apps/sample_app_mongoid/commands/help.rb'
1818

19+
# Offense count: 1
20+
Lint/HandleExceptions:
21+
Exclude:
22+
- 'lib/slack-ruby-bot-server/ping.rb'
23+
1924
# Offense count: 3
2025
# Configuration parameters: Blacklist.
2126
# Blacklist: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$))

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
### Changelog
22

3-
#### 0.7.1 (Next)
3+
#### 0.8.0 (Next)
44

5+
* [#75](https://github.com/slack-ruby/slack-ruby-bot-server/pull/75): Default to `async-websocket` instead of `celluloid-io` - [@dblock](https://github.com/dblock).
56
* Your contribution here.
67

78
#### 0.7.0 (8/22/2018)

UPGRADING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Upgrading Slack-Ruby-Bot-Server
22
===============================
33

4+
### Upgrading to >= 0.8.0
5+
6+
### Different Asynchronous I/O Library
7+
8+
The library now uses [async-websocket](https://github.com/socketry/async-websocket) instead of [celluloid-io](https://github.com/celluloid/celluloid-io). If your application is built on Celluloid you may need to make changes and use `Async::Reactor.run` and the likes.
9+
10+
See [#75](https://github.com/slack-ruby/slack-ruby-bot-server/pull/75) for more information.
11+
412
### Upgrading to >= 0.7.0
513

614
#### New Ping Worker

lib/slack-ruby-bot-server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'celluloid/current'
1+
require 'async/websocket'
22

33
require 'grape-swagger'
44
require 'slack-ruby-bot'

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

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
require 'active_support/core_ext/string/filters'
2+
13
module SlackRubyBotServer
24
class Ping
3-
include Celluloid
4-
55
attr_reader :client
66
attr_reader :options
77
attr_reader :error_count
@@ -13,26 +13,41 @@ def initialize(client, options = {})
1313
end
1414

1515
def start!
16-
every ping_interval do
17-
check!
16+
::Async::Reactor.run do |task|
17+
run(task)
1818
end
1919
end
2020

2121
private
2222

23+
def run(task)
24+
logger.debug "PING: #{owner}, every #{ping_interval} second(s)"
25+
loop do
26+
task.sleep ping_interval
27+
break unless check!
28+
end
29+
logger.debug "PING: #{owner}, done."
30+
rescue StandardError => e
31+
logger.error e
32+
raise e
33+
end
34+
2335
def check!
2436
if online?
2537
@error_count = 0
38+
true
2639
else
2740
down!
2841
end
2942
rescue StandardError => e
3043
case e.message
3144
when 'account_inactive', 'invalid_auth' then
3245
logger.warn "Error pinging team #{owner.id}: #{e.message}, terminating."
33-
terminate
46+
false
3447
else
35-
logger.warn "Error pinging team #{owner.id}: #{e.message}."
48+
message = e.message.truncate(24, separator: "\n", omission: '...')
49+
logger.warn "Error pinging team #{owner.id}: #{message}."
50+
true
3651
end
3752
end
3853

@@ -51,14 +66,22 @@ def presence
5166
def down!
5267
logger.warn "DOWN: #{owner}, #{retries_left} #{retries_left == 1 ? 'retry' : 'retries'} left"
5368
@error_count += 1
54-
return if retries_left?
69+
return true if retries_left?
5570
restart!
71+
false
5672
end
5773

5874
def restart!
5975
logger.warn "RESTART: #{owner}"
60-
driver.emit(:close, WebSocket::Driver::CloseEvent.new(1001, 'bot offline')) if driver
61-
terminate
76+
begin
77+
connection.close
78+
rescue Async::Wrapper::Cancelled
79+
# ignore, from connection.close
80+
end
81+
driver.close
82+
driver.emit(:close, WebSocket::Driver::CloseEvent.new(1001, 'bot offline'))
83+
rescue StandardError => e
84+
logger.warn "Error restarting team #{owner.id}: #{e.message}."
6285
end
6386

6487
def ping_interval
@@ -85,6 +108,10 @@ def driver
85108
socket.instance_variable_get(:@driver) if socket
86109
end
87110

111+
def connection
112+
driver.instance_variable_get(:@socket) if driver
113+
end
114+
88115
def logger
89116
@logger ||= begin
90117
STDOUT.sync = true
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module SlackRubyBotServer
2-
VERSION = '0.7.1'.freeze
2+
VERSION = '0.8.0'.freeze
33
end

sample_apps/sample_app_activerecord/config.ru

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ require 'yaml'
77

88
ActiveRecord::Base.establish_connection(YAML.load_file('config/postgresql.yml')[ENV['RACK_ENV']])
99

10-
if ENV['RACK_ENV'] == 'development'
11-
puts 'Loading NewRelic in developer mode ...'
12-
require 'new_relic/rack/developer_mode'
13-
use NewRelic::Rack::DeveloperMode
14-
end
15-
1610
NewRelic::Agent.manual_start
1711

1812
SlackRubyBotServer::App.instance.prepare!

sample_apps/sample_app_mongoid/config.ru

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ require_relative 'commands'
66

77
Mongoid.load!(File.expand_path('config/mongoid.yml', __dir__), ENV['RACK_ENV'])
88

9-
if ENV['RACK_ENV'] == 'development'
10-
puts 'Loading NewRelic in developer mode ...'
11-
require 'new_relic/rack/developer_mode'
12-
use NewRelic::Rack::DeveloperMode
13-
end
14-
159
NewRelic::Agent.manual_start
1610

1711
SlackRubyBotServer::App.instance.prepare!

slack-ruby-bot-server.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
1414
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
1515
spec.require_paths = ['lib']
1616

17-
spec.add_dependency 'celluloid-io'
17+
spec.add_dependency 'async-websocket'
1818
spec.add_dependency 'foreman'
1919
spec.add_dependency 'grape'
2020
spec.add_dependency 'grape-roar', '>= 0.4.0'

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
context 'with defaults' do
1414
before do
15-
allow(subject.wrapped_object).to receive(:every).and_yield
15+
allow_any_instance_of(Async::Task).to receive(:sleep)
1616
end
1717

1818
it 'defaults retry count' do
@@ -28,14 +28,14 @@
2828
end
2929

3030
it 'checks for connection' do
31-
expect(subject.wrapped_object).to receive(:check!)
31+
expect(subject).to receive(:check!).and_return(false)
3232
subject.start!
3333
end
3434

3535
context 'after a failed check' do
3636
before do
37-
allow(subject.wrapped_object).to receive(:online?).and_return(false)
38-
subject.start!
37+
allow(subject).to receive(:online?).and_return(false)
38+
subject.send(:check!)
3939
end
4040

4141
it 'decrements retries left' do
@@ -48,8 +48,8 @@
4848

4949
context 'after a successful check' do
5050
before do
51-
allow(subject.wrapped_object).to receive(:online?).and_return(true)
52-
subject.start!
51+
allow(subject).to receive(:online?).and_return(true)
52+
subject.send(:check!)
5353
end
5454

5555
it 're-increments retries left' do
@@ -63,15 +63,14 @@
6363
end
6464

6565
it 'terminates the ping worker after account_inactive' do
66-
allow(subject.wrapped_object).to receive(:online?).and_raise('account_inactive')
67-
expect(subject.wrapped_object).to receive(:terminate)
66+
allow(subject).to receive(:online?).and_raise('account_inactive')
6867
subject.start!
6968
end
7069

7170
it 'terminates after a number of retries' do
72-
allow(subject.wrapped_object).to receive(:online?).and_return(false)
73-
expect(subject.wrapped_object).to receive(:terminate)
74-
3.times { subject.start! }
71+
allow(subject).to receive(:online?).and_return(false)
72+
expect(subject).to receive(:check!).exactly(3).times.and_call_original
73+
subject.start!
7574
end
7675
end
7776

@@ -80,8 +79,9 @@
8079
let(:options) { { ping: { ping_interval: 42 } } }
8180

8281
it 'is used' do
82+
expect_any_instance_of(Async::Task).to receive(:sleep).with(42)
8383
expect(subject.send(:ping_interval)).to eq 42
84-
expect(subject.wrapped_object).to receive(:every).with(42)
84+
expect(subject).to receive(:check!).and_return(false)
8585
subject.start!
8686
end
8787
end

0 commit comments

Comments
 (0)