-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
server.rb
88 lines (72 loc) · 1.65 KB
/
server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true
module SlackRubyBot
class Server
include Loggable
attr_accessor :token, :aliases
include SlackRubyBot::Hooks::HookSupport
TRAPPED_SIGNALS = %w[INT TERM].freeze
def initialize(options = {})
@token = options[:token]
@aliases = options[:aliases]
# Hook Handling
flush_hook_blocks
add_hook_handlers options[:hook_handlers] || {
hello: SlackRubyBot::Hooks::Hello.new(logger),
message: SlackRubyBot::Hooks::Message.new
}
end
def run
loop do
handle_exceptions do
handle_signals
start!
end
end
end
def start!
client.start!
end
def start_async
client.start_async
end
def stop!
client.stop! if @client
end
private
def handle_exceptions
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::TimeoutError, Faraday::ConnectionFailed, Faraday::SSLError => e
logger.error e
sleep 1 # ignore, try again
rescue StandardError => e
logger.error e.message
logger.error e.backtrace.join("\n")
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, token: token)
_hooks.client = client
client
end
end
end
end