slacky 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c41bbb7f8e9ad2598d6036ad9a1c67166a449328036242723a2faaec2b49aff6
4
- data.tar.gz: 51a726c0a6535d4fe1b6ca838497ef20fa9fc8c7629116989ce80f3547f9e452
3
+ metadata.gz: e608d8b4af8ddfadf4c45aa64a4dc4bcd5d03dd8c1b78fb5e362ea16c4bf602e
4
+ data.tar.gz: dfee525f94af1a3aca6aa048e9c714a377750bc34980d1e033f9387d6cc50ecf
5
5
  SHA512:
6
- metadata.gz: 7af303486e41000bd32c184c1cb699a513b105c40a743b4b55549b07905ee5d188663d129c0cd1b47ee8c3f1dafe8ccfbfa36111311c989f08746a2c851b38a5
7
- data.tar.gz: 33163cd7a246f666046684b89c0ef07dffcf5d1f25890798238dd1c957ce526c0c2ffebb7020b10859cfa098f08c9fef431c179dfd8ab7ad432c65ca8c7ed934
6
+ metadata.gz: 4ab7bccbff9e0aa4b0a1a6873ec4e21db25321714a38d3b85c0a01c469267d7caf1c470a521e62a5475080dbdfe5735cd8fa080e96fdf2930ec4388e9e25758c
7
+ data.tar.gz: f53e5ae04d5674352bc2f9915340acf8228a164bf67254add03348359b3b8294351f11172d28f2f06ca7617263c713c989ea8b99fdce8652b55d2a487b05a80d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slacky (0.3.0)
4
+ slacky (0.3.1)
5
5
  dotenv
6
6
  em-cron
7
7
  eventmachine
data/lib/slacky.rb CHANGED
@@ -2,7 +2,6 @@ module Slacky
2
2
  require 'slacky/version'
3
3
  require 'slacky/cli'
4
4
  require 'slacky/config'
5
- require 'slacky/daemon'
6
5
  require 'slacky/service'
7
6
  require 'slacky/bot'
8
7
  require 'slacky/bookkeeper'
data/lib/slacky/bot.rb CHANGED
@@ -8,9 +8,6 @@ module Slacky
8
8
  attr_reader :client, :config, :slack_id
9
9
 
10
10
  def initialize(config)
11
- config.log "#{config.name} is starting up..."
12
- puts "#{config.name} is starting up..."
13
-
14
11
  @config = config
15
12
  @command_handlers = []
16
13
  @channel_handlers = []
@@ -19,8 +16,7 @@ module Slacky
19
16
  @cron_handlers = []
20
17
 
21
18
  unless @config.slack_api_token
22
- @config.log "No Slack API token found. Use environment variable SLACK_API_TOKEN."
23
- return
19
+ raise "No Slack API token found. Use environment variable SLACK_API_TOKEN."
24
20
  end
25
21
 
26
22
  Slack.configure do |slack_cfg|
@@ -30,13 +26,8 @@ module Slacky
30
26
  @client = Slack::RealTime::Client.new
31
27
 
32
28
  auth = web_client.auth_test
33
- if auth.ok
34
- @slack_id = auth.user_id
35
- @config.log "Slackbot is active!"
36
- else
37
- @config.log "Slackbot is doomed :-("
38
- return
39
- end
29
+ @slack_id = auth.user_id
30
+ puts "Slackbot is active!"
40
31
 
41
32
  @bookkeeper = Bookkeeper.new @client
42
33
 
@@ -47,6 +38,7 @@ module Slacky
47
38
  populate_channels
48
39
  stay_alive
49
40
 
41
+ on_command 'blowup', &(method :blowup)
50
42
  end
51
43
 
52
44
  def web_client
@@ -159,27 +151,28 @@ module Slacky
159
151
  end
160
152
  end
161
153
 
162
- Thread.abort_on_exception = true
163
- @cron_thread ||= Thread.new do
164
- EM.run do
165
- @cron_handlers.each do |h|
166
- cron, handler = h.values_at :cron, :handler
167
- EM::Cron.schedule cron do |time|
168
- handler.call
169
- end
154
+ puts "#{@config.name} is listening to: #{@config.slack_accept_channels}"
155
+
156
+ Thread.report_on_exception = false if defined? Thread.report_on_exception
157
+
158
+ @client.start! do
159
+ # This code must run in the callback / block because it requires EventMachine
160
+ # be running before it gets executed. If we can find another way to handle
161
+ # this cron syntax we can move to using the async-websocket library instead
162
+ # of EventMachine. -mike
163
+
164
+ @cron_handlers.each do |h|
165
+ cron, handler = h.values_at :cron, :handler
166
+ EM::Cron.schedule cron do |time|
167
+ handler.call
170
168
  end
171
169
  end
172
170
  end
173
-
174
- puts "#{@config.name} is listening to: #{@config.slack_accept_channels}"
175
-
176
- @client.start!
177
171
  end
178
172
 
179
173
  def populate_users
180
174
  print "Getting users from Slack..."
181
175
  resp = web_client.users_list
182
- throw resp unless resp.ok
183
176
  User.invalidate_all_users
184
177
  whitelist = @config.whitelist_users || []
185
178
  resp.members.map do |member|
@@ -201,13 +194,11 @@ module Slacky
201
194
  def populate_channels
202
195
  print "Getting channels from Slack..."
203
196
  resp = web_client.channels_list
204
- throw resp unless resp.ok
205
197
  resp.channels.map do |channel|
206
198
  Channel.channel channel
207
199
  end
208
200
 
209
201
  resp = web_client.groups_list
210
- throw resp unless resp.ok
211
202
  resp.groups.map do |group|
212
203
  Channel.group group
213
204
  end
@@ -226,14 +217,14 @@ module Slacky
226
217
  now = Time.now.to_f
227
218
  stamp = data.stamp
228
219
  delta = now - stamp
229
- @config.log "Slow ping pong response: #{delta}s" if delta > 5
220
+ raise Exception.new("Slow ping pong response: #{delta}s") if delta > 5
230
221
  end
231
222
  end
232
223
 
233
- def blowup(user, data, args, &respond)
234
- respond.call "Tick... tick... tick... BOOM! Goodbye."
224
+ def blowup(message)
225
+ message.reply "Tick... tick... tick... BOOM! Goodbye."
235
226
  EM.next_tick do
236
- raise "kablammo!"
227
+ raise Exception.new("kablammo!")
237
228
  end
238
229
  end
239
230
  end
@@ -58,7 +58,7 @@ module Slacky
58
58
  when :channel ; @member
59
59
  when :group ; @members[@@bot.slack_id]
60
60
  when :im ; true
61
- else throw "Unknown channel type: #{@type}"
61
+ else raise "Unknown channel type: #{@type}"
62
62
  end
63
63
  end
64
64
 
data/lib/slacky/cli.rb CHANGED
@@ -3,32 +3,13 @@ module Slacky
3
3
  attr_reader :bot
4
4
 
5
5
  def initialize(name, bot_class, opts)
6
- throw "CLI must be passed a name" unless name
7
- @options = { :verbose => false }.merge opts
6
+ raise "CLI must be passed a name" unless name
8
7
  config = Config.new name
9
- daemon = Daemon.new config, bot_class
10
- @service = Service.new config, daemon
8
+ @service = Service.new config, bot_class
11
9
  end
12
10
 
13
11
  def run(params)
14
12
  @service.run
15
13
  end
16
-
17
- def start(params)
18
- @service.start
19
- end
20
-
21
- def stop(params)
22
- @service.stop
23
- end
24
-
25
- def restart(params)
26
- @service.restart
27
- end
28
-
29
- def status(params)
30
- @service.status
31
- end
32
-
33
14
  end
34
15
  end
data/lib/slacky/config.rb CHANGED
@@ -5,14 +5,12 @@ require 'dotenv'
5
5
 
6
6
  module Slacky
7
7
  class Config
8
- attr_reader :pid_file, :name, :db
8
+ attr_reader :name, :db
9
9
 
10
10
  def initialize(name, opts = {})
11
11
  @name = name
12
12
  Dotenv.load ".env", "#{config_dir}/.env"
13
13
  FileUtils.mkdir config_dir unless File.directory? config_dir
14
- @pid_file = "#{config_dir}/pid"
15
- @timestamps = {}
16
14
  User.config = self
17
15
  end
18
16
 
@@ -55,26 +53,10 @@ module Slacky
55
53
  ENV['WHITELIST_USERS'].split(',').map {|u| u.strip}
56
54
  end
57
55
 
58
- def log(msg, ex = nil)
59
- log = File.new(log_file, 'a')
60
- timestamp = Time.now.strftime('%Y-%m-%d %H:%M:%S')
61
- type = ex ? 'ERROR' : ' INFO'
62
- log.puts "#{type} #{timestamp} #{msg}"
63
- if ex
64
- log.puts ex.message
65
- log.puts("Stacktrace:\n" + ex.backtrace.join("\n"))
66
- end
67
- log.flush
68
- end
69
-
70
56
  private
71
57
 
72
58
  def db_connect_params
73
59
  ENV['DATABASE_URL'] || { dbname: "slacky_#{down_name}" }
74
60
  end
75
-
76
- def log_file
77
- "#{config_dir}/#{down_name}.log"
78
- end
79
61
  end
80
62
  end
@@ -1,73 +1,15 @@
1
- require 'fileutils'
2
-
3
- class Slacky::Service
4
- def initialize(config, daemon)
5
- @config = config
6
- @daemon = daemon
7
- end
8
-
9
- def run
10
- @daemon.start false
11
- end
12
-
13
- def start(persist = false)
14
- pid = get_pid
15
- if pid
16
- puts "#{@config.name} is already running with PID #{pid}"
17
- return
1
+ module Slacky
2
+ class Service
3
+ def initialize(config, bot_class)
4
+ @config = config
5
+ @bot_class = bot_class
18
6
  end
19
7
 
20
- print "Starting #{@config.name}... "
21
- new_pid = Process.fork { @daemon.start }
22
- Process.detach new_pid
23
- puts "started"
24
- end
25
-
26
- def stop(persist = false)
27
- pid = get_pid
28
- unless pid
29
- puts "#{@config.name} is not running"
30
- return
31
- end
32
-
33
- print "Stopping #{@config.name}..."
34
-
35
- begin
36
- Process.kill 'HUP', pid
37
- rescue
38
- @daemon.cleanup
39
- end
40
-
41
- ticks = 0
42
- while pid = get_pid && ticks < 40
43
- sleep 0.5
44
- ticks += 1
45
- print '.' if ticks % 4 == 0
46
- end
47
- puts " #{pid.nil? ? 'stopped' : 'failed'}"
48
- end
49
-
50
- def restart
51
- stop
52
- start
53
- end
54
-
55
- def status
56
- pid = get_pid
57
- if pid
58
- puts "#{@config.name} is running with PID #{pid}"
59
- true
60
- else
61
- puts "#{@config.name} is not running"
62
- false
8
+ def run
9
+ puts "#{@config.name} is running"
10
+ bot = Bot.new @config
11
+ @bot_class.new bot
12
+ bot.run
63
13
  end
64
14
  end
65
-
66
- private
67
-
68
- def get_pid
69
- return nil unless File.exists? @config.pid_file
70
- IO.read(@config.pid_file).to_i
71
- end
72
-
73
15
  end
@@ -1,3 +1,3 @@
1
1
  module Slacky
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slacky
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Wynholds
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-25 00:00:00.000000000 Z
11
+ date: 2019-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slack-ruby-client
@@ -183,7 +183,6 @@ files:
183
183
  - lib/slacky/channel.rb
184
184
  - lib/slacky/cli.rb
185
185
  - lib/slacky/config.rb
186
- - lib/slacky/daemon.rb
187
186
  - lib/slacky/message.rb
188
187
  - lib/slacky/service.rb
189
188
  - lib/slacky/user.rb
data/lib/slacky/daemon.rb DELETED
@@ -1,70 +0,0 @@
1
- module Slacky
2
- class Daemon
3
-
4
- def initialize(config, bot_class)
5
- @config = config
6
- @bot_class = bot_class
7
- @active = true
8
- @running = false
9
- end
10
-
11
- def start(daemonize = true)
12
- Process.daemon if daemonize
13
- write_pid
14
-
15
- [ 'HUP', 'INT', 'QUIT', 'TERM' ].each do |sig|
16
- Signal.trap(sig) do
17
- @config.log "Interrupted with signal: #{sig}"
18
- kill
19
- end
20
- end
21
-
22
- begin
23
- Thread.abort_on_exception = true
24
- @slackthread = Thread.new do
25
- bot = Bot.new @config
26
- @bot_class.new bot
27
- bot.run
28
- end
29
- run
30
- rescue => e
31
- @config.log "Unexpected error", e
32
- ensure
33
- cleanup
34
- end
35
- end
36
-
37
- def cleanup
38
- delete_pid
39
- end
40
-
41
- private
42
-
43
- def run
44
- @config.log "#{@config.name} is running."
45
- while active? do
46
- sleep 0.5
47
- end
48
- @config.log "#{@config.name} got killed"
49
- @slackthread.kill
50
- end
51
-
52
- def active?
53
- @active
54
- end
55
-
56
- def kill
57
- @active = false
58
- end
59
-
60
- def write_pid
61
- File.open @config.pid_file, 'w' do |f|
62
- f.write Process.pid.to_s
63
- end
64
- end
65
-
66
- def delete_pid
67
- File.delete @config.pid_file if File.exists? @config.pid_file
68
- end
69
- end
70
- end