slack-ruby-bot-server 0.11.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/FUNDING.yml +1 -0
- data/.rubocop.yml +1 -1
- data/.rubocop_todo.yml +45 -13
- data/.travis.yml +7 -6
- data/CHANGELOG.md +49 -22
- data/DEBUGGING.md +0 -20
- data/Dangerfile +1 -0
- data/Gemfile +3 -2
- data/LICENSE +1 -1
- data/README.md +106 -52
- data/UPGRADING.md +14 -0
- data/images/create-app.png +0 -0
- data/lib/slack-ruby-bot-server.rb +11 -13
- data/lib/slack-ruby-bot-server/api.rb +4 -4
- data/lib/slack-ruby-bot-server/api/endpoints.rb +3 -3
- data/lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb +11 -3
- data/lib/slack-ruby-bot-server/api/helpers.rb +4 -4
- data/lib/slack-ruby-bot-server/api/helpers/cursor_helpers.rb +2 -0
- data/lib/slack-ruby-bot-server/api/helpers/sort_helpers.rb +1 -0
- data/lib/slack-ruby-bot-server/api/middleware.rb +5 -10
- data/lib/slack-ruby-bot-server/api/presenters.rb +5 -5
- data/lib/slack-ruby-bot-server/api/presenters/status_presenter.rb +1 -0
- data/lib/slack-ruby-bot-server/config.rb +16 -2
- data/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb +2 -2
- data/lib/slack-ruby-bot-server/config/database_adapters/mongoid.rb +4 -2
- data/lib/slack-ruby-bot-server/ext.rb +1 -1
- data/lib/slack-ruby-bot-server/ext/mongoid/slack-ruby-bot/commands/base.rb +2 -2
- data/lib/slack-ruby-bot-server/loggable.rb +25 -0
- data/lib/slack-ruby-bot-server/models/team/activerecord.rb +8 -5
- data/lib/slack-ruby-bot-server/models/team/methods.rb +12 -0
- data/lib/slack-ruby-bot-server/models/team/mongoid.rb +8 -4
- data/lib/slack-ruby-bot-server/rspec.rb +0 -1
- data/lib/slack-ruby-bot-server/service.rb +48 -32
- data/lib/slack-ruby-bot-server/version.rb +1 -1
- data/public/index.html.erb +2 -1
- data/public/scripts/messages.js +33 -0
- data/public/scripts/register.js +0 -31
- data/slack-ruby-bot-server.gemspec +3 -3
- data/tasks/db.rake +0 -36
- metadata +19 -57
- data/images/new.png +0 -0
- data/images/slackbotserver.gif +0 -0
- data/images/slackbutton.gif +0 -0
- data/lib/slack-ruby-bot-server/ext/activerecord/slack-ruby-bot/commands/base.rb +0 -18
- data/lib/slack-ruby-bot-server/ext/slack-ruby-bot.rb +0 -2
- data/lib/slack-ruby-bot-server/ext/slack-ruby-bot/client.rb +0 -12
- data/lib/slack-ruby-bot-server/server.rb +0 -30
- data/sample_apps/README.md +0 -11
- data/sample_apps/sample_app_activerecord/.rspec +0 -3
- data/sample_apps/sample_app_activerecord/.standalone_migrations +0 -3
- data/sample_apps/sample_app_activerecord/Gemfile +0 -24
- data/sample_apps/sample_app_activerecord/Procfile +0 -1
- data/sample_apps/sample_app_activerecord/README.md +0 -11
- data/sample_apps/sample_app_activerecord/Rakefile +0 -14
- data/sample_apps/sample_app_activerecord/commands.rb +0 -2
- data/sample_apps/sample_app_activerecord/commands/help.rb +0 -19
- data/sample_apps/sample_app_activerecord/commands/whoami.rb +0 -6
- data/sample_apps/sample_app_activerecord/config.ru +0 -22
- data/sample_apps/sample_app_activerecord/config/newrelic.yml +0 -217
- data/sample_apps/sample_app_activerecord/config/postgresql.yml +0 -17
- data/sample_apps/sample_app_activerecord/db/migrate/20170307164946_create_teams_table.rb +0 -12
- data/sample_apps/sample_app_activerecord/db/migrate/20190323181453_add_activated_fields.rb +0 -7
- data/sample_apps/sample_app_activerecord/db/schema.rb +0 -29
- data/sample_apps/sample_app_activerecord/spec/api/root_spec.rb +0 -16
- data/sample_apps/sample_app_activerecord/spec/commands/help_spec.rb +0 -14
- data/sample_apps/sample_app_activerecord/spec/commands/whoami_spec.rb +0 -14
- data/sample_apps/sample_app_activerecord/spec/spec_helper.rb +0 -19
- data/sample_apps/sample_app_mongoid/.rspec +0 -3
- data/sample_apps/sample_app_mongoid/Gemfile +0 -20
- data/sample_apps/sample_app_mongoid/Procfile +0 -1
- data/sample_apps/sample_app_mongoid/README.md +0 -14
- data/sample_apps/sample_app_mongoid/Rakefile +0 -10
- data/sample_apps/sample_app_mongoid/commands.rb +0 -2
- data/sample_apps/sample_app_mongoid/commands/help.rb +0 -19
- data/sample_apps/sample_app_mongoid/commands/whoami.rb +0 -6
- data/sample_apps/sample_app_mongoid/config.ru +0 -14
- data/sample_apps/sample_app_mongoid/config/mongoid.yml +0 -27
- data/sample_apps/sample_app_mongoid/config/newrelic.yml +0 -217
- data/sample_apps/sample_app_mongoid/spec/api/root_spec.rb +0 -16
- data/sample_apps/sample_app_mongoid/spec/commands/help_spec.rb +0 -14
- data/sample_apps/sample_app_mongoid/spec/commands/whoami_spec.rb +0 -14
- data/sample_apps/sample_app_mongoid/spec/spec_helper.rb +0 -27
data/UPGRADING.md
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
Upgrading Slack-Ruby-Bot-Server
|
|
2
2
|
===============================
|
|
3
3
|
|
|
4
|
+
### Upgrading to >= 1.0.0
|
|
5
|
+
|
|
6
|
+
#### Extracted RealTime (Legacy) Support
|
|
7
|
+
|
|
8
|
+
New slack apps may no longer access RTM. Classic Slack apps can no longer be submitted to the app directory as of December 4th, 2020. In preparation for these changes slack-ruby-bot-server no longer includes RTM components by default. These have been extracted to a new gem [slack-ruby-bot-server-rtm](https://github.com/slack-ruby/slack-ruby-bot-server-rtm).
|
|
9
|
+
|
|
10
|
+
To upgrade an existing classic Slack app that uses slack-ruby-bot-server do the following.
|
|
11
|
+
|
|
12
|
+
1. Add `slack-ruby-bot-server-rtm` as an additional dependency.
|
|
13
|
+
2. Replace any reference to `SlackRubyBotServer::Server` to `SlackRubyBotServer::RealTime::Server`.
|
|
14
|
+
3. Replace any `require 'slack-ruby-bot-server/rspec'` with `require 'slack-ruby-bot-server-rtm/rspec'`.
|
|
15
|
+
|
|
16
|
+
Existing RTM Slack bots will continue working and be listed in the Slack App Directory. On December 4th, 2020 Slack will no longer accept resubmissions from apps that are not using granular permissions. On November 18, 2021 Slack will start delisting apps that have not migrated to use granular permissions. Use [slack-ruby-bot-server-events](https://github.com/slack-ruby/slack-ruby-bot-server-events) to create a Slack bot with granular permissions. See [migration](https://api.slack.com/authentication/migration) for more details.
|
|
17
|
+
|
|
4
18
|
### Upgrading to >= 0.11.0
|
|
5
19
|
|
|
6
20
|
#### Removed Legacy Migrations
|
|
Binary file
|
|
@@ -1,16 +1,14 @@
|
|
|
1
|
-
require 'async
|
|
1
|
+
require 'async'
|
|
2
2
|
|
|
3
|
+
require 'slack-ruby-client'
|
|
3
4
|
require 'grape-swagger'
|
|
4
|
-
require 'slack-ruby-bot'
|
|
5
|
-
require 'slack-ruby-bot-server/service'
|
|
6
|
-
require 'slack-ruby-bot-server/server'
|
|
7
|
-
require 'slack-ruby-bot-server/config'
|
|
8
5
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
6
|
+
require_relative 'slack-ruby-bot-server/loggable'
|
|
7
|
+
require_relative 'slack-ruby-bot-server/service'
|
|
8
|
+
require_relative 'slack-ruby-bot-server/config'
|
|
9
|
+
require_relative 'slack-ruby-bot-server/ext'
|
|
10
|
+
require_relative 'slack-ruby-bot-server/version'
|
|
11
|
+
require_relative 'slack-ruby-bot-server/info'
|
|
12
|
+
require_relative "slack-ruby-bot-server/config/database_adapters/#{SlackRubyBotServer::Config.database_adapter}.rb"
|
|
13
|
+
require_relative 'slack-ruby-bot-server/api'
|
|
14
|
+
require_relative 'slack-ruby-bot-server/app'
|
|
@@ -3,7 +3,7 @@ require 'roar'
|
|
|
3
3
|
require 'grape-roar'
|
|
4
4
|
|
|
5
5
|
require_relative 'ext/grape/sort_extension'
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
require_relative 'api/helpers'
|
|
7
|
+
require_relative 'api/presenters'
|
|
8
|
+
require_relative 'api/endpoints'
|
|
9
|
+
require_relative 'api/middleware'
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
require_relative 'endpoints/teams_endpoint'
|
|
2
|
+
require_relative 'endpoints/status_endpoint'
|
|
3
|
+
require_relative 'endpoints/root_endpoint'
|
|
@@ -46,20 +46,28 @@ module SlackRubyBotServer
|
|
|
46
46
|
code: params[:code]
|
|
47
47
|
)
|
|
48
48
|
|
|
49
|
-
token = rc['bot']['bot_access_token']
|
|
50
|
-
bot_user_id = rc['bot']['bot_user_id']
|
|
51
|
-
user_id = rc['user_id']
|
|
52
49
|
access_token = rc['access_token']
|
|
50
|
+
user_id = rc['user_id']
|
|
51
|
+
|
|
52
|
+
bot = rc['bot']
|
|
53
|
+
|
|
54
|
+
token = bot ? bot['bot_access_token'] : access_token
|
|
55
|
+
bot_user_id = bot['bot_user_id'] if bot
|
|
56
|
+
|
|
53
57
|
team = Team.where(token: token).first
|
|
54
58
|
team ||= Team.where(team_id: rc['team_id']).first
|
|
55
59
|
|
|
56
60
|
if team
|
|
61
|
+
team.ping_if_active!
|
|
62
|
+
|
|
57
63
|
team.update_attributes!(
|
|
58
64
|
activated_user_id: user_id,
|
|
59
65
|
activated_user_access_token: access_token,
|
|
60
66
|
bot_user_id: bot_user_id
|
|
61
67
|
)
|
|
68
|
+
|
|
62
69
|
raise "Team #{team.name} is already registered." if team.active?
|
|
70
|
+
|
|
63
71
|
team.activate!(token)
|
|
64
72
|
else
|
|
65
73
|
team = Team.create!(
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
require_relative 'helpers/cursor_helpers'
|
|
2
|
+
require_relative 'helpers/pagination_parameters'
|
|
3
|
+
require_relative 'helpers/sort_helpers'
|
|
4
|
+
require_relative 'helpers/error_helpers'
|
|
@@ -11,6 +11,7 @@ module SlackRubyBotServer
|
|
|
11
11
|
if SlackRubyBotServer::Config.mongoid?
|
|
12
12
|
def paginate_by_cursor(coll, _options)
|
|
13
13
|
raise 'Both cursor and offset parameters are present, these are mutually exclusive.' if params.key?(:offset) && params.key?(:cursor)
|
|
14
|
+
|
|
14
15
|
results = { results: [], next: nil }
|
|
15
16
|
coll = coll.skip(params[:offset].to_i) if params.key?(:offset)
|
|
16
17
|
size = (params[:size] || 10).to_i
|
|
@@ -26,6 +27,7 @@ module SlackRubyBotServer
|
|
|
26
27
|
elsif SlackRubyBotServer::Config.activerecord?
|
|
27
28
|
def paginate_by_cursor(coll, options)
|
|
28
29
|
raise 'Both cursor and offset parameters are present, these are mutually exclusive.' if params.key?(:offset) && params.key?(:cursor)
|
|
30
|
+
|
|
29
31
|
results = { results: [], next: nil }
|
|
30
32
|
size = (params[:size] || 10).to_i
|
|
31
33
|
results[:total_count] = coll.count(:all) if params[:total_count]
|
|
@@ -7,6 +7,7 @@ module SlackRubyBotServer
|
|
|
7
7
|
def sort_order(options = {})
|
|
8
8
|
params[:sort] = options[:default_sort_order] unless params[:sort]
|
|
9
9
|
return [] unless params[:sort]
|
|
10
|
+
|
|
10
11
|
sort_order = params[:sort].to_s
|
|
11
12
|
unless options[:default_sort_order] == sort_order
|
|
12
13
|
supported_sort_orders = route_sort
|
|
@@ -6,11 +6,10 @@ require 'otr-activerecord' if SlackRubyBotServer::Config.activerecord? && !defin
|
|
|
6
6
|
module SlackRubyBotServer
|
|
7
7
|
module Api
|
|
8
8
|
class Middleware
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
end
|
|
9
|
+
include SlackRubyBotServer::Loggable
|
|
10
|
+
|
|
11
|
+
def self.reset!
|
|
12
|
+
@instance = nil
|
|
14
13
|
end
|
|
15
14
|
|
|
16
15
|
def self.instance
|
|
@@ -30,11 +29,7 @@ module SlackRubyBotServer
|
|
|
30
29
|
end
|
|
31
30
|
|
|
32
31
|
use Rack::ServerPages do |config|
|
|
33
|
-
config.view_path =
|
|
34
|
-
'views', # relative to Dir.pwd
|
|
35
|
-
'public', # relative to Dir.pwd
|
|
36
|
-
File.expand_path(File.join(__dir__, '../../../public')) # built-in fallback
|
|
37
|
-
]
|
|
32
|
+
config.view_path = SlackRubyBotServer::Config.view_paths
|
|
38
33
|
end
|
|
39
34
|
|
|
40
35
|
run Middleware.new
|
|
@@ -2,8 +2,8 @@ require 'roar/representer'
|
|
|
2
2
|
require 'roar/json'
|
|
3
3
|
require 'roar/json/hal'
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
require_relative 'presenters/paginated_presenter'
|
|
6
|
+
require_relative 'presenters/status_presenter'
|
|
7
|
+
require_relative 'presenters/team_presenter'
|
|
8
|
+
require_relative 'presenters/teams_presenter'
|
|
9
|
+
require_relative 'presenters/root_presenter'
|
|
@@ -2,13 +2,23 @@ module SlackRubyBotServer
|
|
|
2
2
|
module Config
|
|
3
3
|
extend self
|
|
4
4
|
|
|
5
|
-
attr_accessor :
|
|
5
|
+
attr_accessor :logger
|
|
6
6
|
attr_accessor :service_class
|
|
7
7
|
attr_accessor :database_adapter
|
|
8
|
+
attr_accessor :view_paths
|
|
9
|
+
attr_accessor :oauth_scope
|
|
8
10
|
|
|
9
11
|
def reset!
|
|
10
|
-
self.
|
|
12
|
+
self.logger = nil
|
|
11
13
|
self.service_class = SlackRubyBotServer::Service
|
|
14
|
+
self.oauth_scope = nil
|
|
15
|
+
|
|
16
|
+
self.view_paths = [
|
|
17
|
+
'views',
|
|
18
|
+
'public',
|
|
19
|
+
File.expand_path(File.join(__dir__, '../../public'))
|
|
20
|
+
]
|
|
21
|
+
|
|
12
22
|
self.database_adapter = if defined?(::Mongoid)
|
|
13
23
|
:mongoid
|
|
14
24
|
elsif defined?(::ActiveRecord)
|
|
@@ -18,6 +28,10 @@ module SlackRubyBotServer
|
|
|
18
28
|
end
|
|
19
29
|
end
|
|
20
30
|
|
|
31
|
+
def oauth_scope_s
|
|
32
|
+
oauth_scope&.join('+')
|
|
33
|
+
end
|
|
34
|
+
|
|
21
35
|
def activerecord?
|
|
22
36
|
database_adapter == :activerecord
|
|
23
37
|
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
require_relative '../../models/team/activerecord.rb'
|
|
2
2
|
|
|
3
3
|
module SlackRubyBotServer
|
|
4
4
|
module DatabaseAdapter
|
|
@@ -28,4 +28,4 @@ module SlackRubyBotServer
|
|
|
28
28
|
end
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
-
::Boolean =
|
|
31
|
+
::Boolean = Grape::API::Boolean
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
require_relative '../../models/team/mongoid.rb'
|
|
2
|
+
|
|
2
3
|
require 'kaminari/grape'
|
|
3
4
|
require 'mongoid-scroll'
|
|
4
5
|
|
|
@@ -6,7 +7,8 @@ module SlackRubyBotServer
|
|
|
6
7
|
module DatabaseAdapter
|
|
7
8
|
def self.check!
|
|
8
9
|
rc = Mongoid.default_client.command(ping: 1)
|
|
9
|
-
return if rc
|
|
10
|
+
return if rc&.ok?
|
|
11
|
+
|
|
10
12
|
raise rc.documents.first['error'] || 'Unexpected error.'
|
|
11
13
|
rescue StandardError => e
|
|
12
14
|
warn "Error connecting to MongoDB: #{e.message}"
|
|
@@ -8,12 +8,12 @@ module SlackRubyBot
|
|
|
8
8
|
_invoke client, data
|
|
9
9
|
rescue Mongoid::Errors::Validations => e
|
|
10
10
|
logger.info "#{name.demodulize.upcase}: #{client.owner}, error - #{e.document.class}, #{e.document.errors.to_hash}"
|
|
11
|
-
client.say(channel: data.channel, text: e.document.errors.first[1]
|
|
11
|
+
client.say(channel: data.channel, text: e.document.errors.first[1])
|
|
12
12
|
true
|
|
13
13
|
rescue StandardError => e
|
|
14
14
|
logger.info "#{name.demodulize.upcase}: #{client.owner}, #{e.class}: #{e}"
|
|
15
15
|
logger.debug e.backtrace.join("\n")
|
|
16
|
-
client.say(channel: data.channel, text: e.message
|
|
16
|
+
client.say(channel: data.channel, text: e.message)
|
|
17
17
|
true
|
|
18
18
|
end
|
|
19
19
|
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SlackRubyBotServer
|
|
4
|
+
module Loggable
|
|
5
|
+
def self.included(base)
|
|
6
|
+
base.send :include, InstanceMethods
|
|
7
|
+
base.extend(ClassMethods)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
module ClassMethods
|
|
11
|
+
def logger
|
|
12
|
+
@logger ||= SlackRubyBotServer::Config.logger || begin
|
|
13
|
+
$stdout.sync = true
|
|
14
|
+
Logger.new($stdout)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
module InstanceMethods
|
|
20
|
+
def logger
|
|
21
|
+
self.class.logger
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -3,11 +3,14 @@ require_relative 'methods'
|
|
|
3
3
|
class Team < ActiveRecord::Base
|
|
4
4
|
include Methods
|
|
5
5
|
|
|
6
|
-
def self.purge!
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
def self.purge!(dt = 2.weeks.ago)
|
|
7
|
+
Team.where(active: false).where('updated_at <= ?', dt).each do |team|
|
|
8
|
+
begin
|
|
9
|
+
logger.info "Destroying #{team}, inactive since #{team.updated_at}."
|
|
10
|
+
team.destroy
|
|
11
|
+
rescue StandardError => e
|
|
12
|
+
logger.warn "Error destroying #{team}, #{e.message}."
|
|
13
|
+
end
|
|
11
14
|
end
|
|
12
15
|
end
|
|
13
16
|
end
|
|
@@ -38,5 +38,17 @@ module Methods
|
|
|
38
38
|
presence: client.users_getPresence(user: auth['user_id'])
|
|
39
39
|
}
|
|
40
40
|
end
|
|
41
|
+
|
|
42
|
+
def ping_if_active!
|
|
43
|
+
return unless active?
|
|
44
|
+
|
|
45
|
+
ping!
|
|
46
|
+
rescue Slack::Web::Api::Errors::SlackError => e
|
|
47
|
+
logger.warn "Active team #{self} ping, #{e.message}."
|
|
48
|
+
case e.message
|
|
49
|
+
when 'account_inactive', 'invalid_auth'
|
|
50
|
+
deactivate!
|
|
51
|
+
end
|
|
52
|
+
end
|
|
41
53
|
end
|
|
42
54
|
end
|
|
@@ -15,11 +15,15 @@ class Team
|
|
|
15
15
|
|
|
16
16
|
include Methods
|
|
17
17
|
|
|
18
|
-
def self.purge!
|
|
18
|
+
def self.purge!(dt = 2.weeks.ago)
|
|
19
19
|
# destroy teams inactive for two weeks
|
|
20
|
-
Team.where(active: false, :updated_at.lte =>
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
Team.where(active: false, :updated_at.lte => dt).each do |team|
|
|
21
|
+
begin
|
|
22
|
+
logger.info "Destroying #{team}, inactive since #{team.updated_at}."
|
|
23
|
+
team.destroy
|
|
24
|
+
rescue StandardError => e
|
|
25
|
+
logger.warn "Error destroying #{team}, #{e.message}."
|
|
26
|
+
end
|
|
23
27
|
end
|
|
24
28
|
end
|
|
25
29
|
end
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
module SlackRubyBotServer
|
|
2
2
|
class Service
|
|
3
|
-
include
|
|
3
|
+
include SlackRubyBotServer::Loggable
|
|
4
4
|
|
|
5
5
|
def self.start!
|
|
6
6
|
Thread.new do
|
|
@@ -15,10 +15,29 @@ module SlackRubyBotServer
|
|
|
15
15
|
|
|
16
16
|
def initialize
|
|
17
17
|
@callbacks = Hash.new { |h, k| h[k] = [] }
|
|
18
|
+
@intervals = Hash.new { |h, k| h[k] = [] }
|
|
18
19
|
end
|
|
19
20
|
|
|
20
|
-
def on(
|
|
21
|
-
|
|
21
|
+
def on(*types, &block)
|
|
22
|
+
Array(types).each do |type|
|
|
23
|
+
@callbacks[type.to_s] << block
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def every(*intervals, &block)
|
|
28
|
+
Array(intervals).each do |interval|
|
|
29
|
+
case interval
|
|
30
|
+
when :minute
|
|
31
|
+
interval = 60
|
|
32
|
+
when :hour
|
|
33
|
+
interval = 60 * 60
|
|
34
|
+
when :day
|
|
35
|
+
interval = 60 * 60 * 24
|
|
36
|
+
end
|
|
37
|
+
raise "Invalid interval \"#{interval}\"." unless interval.is_a?(Integer) && interval > 0
|
|
38
|
+
|
|
39
|
+
@intervals[interval] << block
|
|
40
|
+
end
|
|
22
41
|
end
|
|
23
42
|
|
|
24
43
|
def create!(team, options = {})
|
|
@@ -28,34 +47,30 @@ module SlackRubyBotServer
|
|
|
28
47
|
end
|
|
29
48
|
|
|
30
49
|
def start!(team)
|
|
31
|
-
run_callbacks :starting, team
|
|
32
50
|
logger.info "Starting team #{team}."
|
|
33
|
-
|
|
34
|
-
server = SlackRubyBotServer::Config.server_class.new(options)
|
|
35
|
-
start_server! team, server
|
|
51
|
+
run_callbacks :starting, team
|
|
36
52
|
run_callbacks :started, team
|
|
37
|
-
server
|
|
38
53
|
rescue StandardError => e
|
|
39
54
|
run_callbacks :error, team, e
|
|
40
55
|
logger.error e
|
|
41
56
|
end
|
|
42
57
|
|
|
43
|
-
def restart!(team
|
|
58
|
+
def restart!(team)
|
|
59
|
+
logger.info "Restarting team #{team}."
|
|
44
60
|
run_callbacks :restarting, team
|
|
45
|
-
start_server! team, server, wait
|
|
46
61
|
run_callbacks :restarted, team
|
|
62
|
+
rescue StandardError => e
|
|
63
|
+
run_callbacks :error, team, e
|
|
64
|
+
logger.error e
|
|
47
65
|
end
|
|
48
66
|
|
|
49
67
|
def stop!(team)
|
|
50
68
|
logger.info "Stopping team #{team}."
|
|
51
69
|
run_callbacks :stopping, team
|
|
52
|
-
team.server.stop! if team.server
|
|
53
70
|
run_callbacks :stopped, team
|
|
54
71
|
rescue StandardError => e
|
|
55
72
|
run_callbacks :error, team, e
|
|
56
73
|
logger.error e
|
|
57
|
-
ensure
|
|
58
|
-
team.server = nil
|
|
59
74
|
end
|
|
60
75
|
|
|
61
76
|
def start_from_database!
|
|
@@ -64,20 +79,24 @@ module SlackRubyBotServer
|
|
|
64
79
|
start!(team)
|
|
65
80
|
run_callbacks :booted, team
|
|
66
81
|
end
|
|
82
|
+
start_intervals!
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def start_intervals!
|
|
86
|
+
@intervals.each_pair do |period, calls|
|
|
87
|
+
_every period do
|
|
88
|
+
calls.each(&:call)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
67
91
|
end
|
|
68
92
|
|
|
69
93
|
def deactivate!(team)
|
|
70
94
|
run_callbacks :deactivating, team
|
|
71
95
|
team.deactivate!
|
|
72
96
|
run_callbacks :deactivated, team
|
|
73
|
-
rescue Mongoid::Errors::Validations => e
|
|
74
|
-
run_callbacks :error, team, e
|
|
75
|
-
logger.error "#{team.name}: #{e.message}, error - #{e.document.class}, #{e.document.errors.to_hash}, ignored."
|
|
76
97
|
rescue StandardError => e
|
|
77
98
|
run_callbacks :error, team, e
|
|
78
99
|
logger.error "#{team.name}: #{e.class}, #{e.message}, ignored."
|
|
79
|
-
ensure
|
|
80
|
-
team.server = nil
|
|
81
100
|
end
|
|
82
101
|
|
|
83
102
|
def self.reset!
|
|
@@ -86,26 +105,23 @@ module SlackRubyBotServer
|
|
|
86
105
|
|
|
87
106
|
private
|
|
88
107
|
|
|
89
|
-
def
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
wait = e.retry_after if e.is_a?(Slack::Web::Api::Errors::TooManyRequestsError)
|
|
100
|
-
logger.error "#{team.name}: #{e.message}, restarting in #{wait} second(s)."
|
|
101
|
-
sleep(wait)
|
|
102
|
-
start_server! team, server, [wait * 2, 60].min
|
|
108
|
+
def _every(tt, &_block)
|
|
109
|
+
::Async::Reactor.run do |task|
|
|
110
|
+
loop do
|
|
111
|
+
begin
|
|
112
|
+
task.sleep tt
|
|
113
|
+
yield
|
|
114
|
+
rescue StandardError => e
|
|
115
|
+
logger.error e
|
|
116
|
+
end
|
|
117
|
+
end
|
|
103
118
|
end
|
|
104
119
|
end
|
|
105
120
|
|
|
106
121
|
def run_callbacks(type, team = nil, error = nil, options = {})
|
|
107
122
|
callbacks = @callbacks[type.to_s]
|
|
108
123
|
return false unless callbacks
|
|
124
|
+
|
|
109
125
|
callbacks.each do |c|
|
|
110
126
|
c.call team, error, options
|
|
111
127
|
end
|