roo_on_rails 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ module RooOnRails
2
+ module Sidekiq
3
+ class Settings
4
+ def self.queues
5
+ %w(
6
+ monitoring
7
+ realtime
8
+ within1minute
9
+ within5minutes
10
+ within30minutes
11
+ within1hour
12
+ within1day
13
+ ).freeze
14
+ end
15
+
16
+ def self.concurrency
17
+ ENV.fetch('SIDEKIQ_THREADS', 25)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,75 @@
1
+ # Returns stats on the current SLA performance of queues in a sidekiq instance.
2
+ # Assumes workers are not bound to queues
3
+ require 'sidekiq/api'
4
+ require 'active_support'
5
+ require 'active_support/core_ext/numeric'
6
+ module RooOnRails
7
+ module Sidekiq
8
+ class SlaMetric
9
+ def self.queue
10
+ queues = ::Sidekiq::Queue.all.map { |q| SidekiqQueueMetrics.new(q) }
11
+ global_stats = SidekiqWorkerCount.new(queues)
12
+ global_stats.requested_processes
13
+ end
14
+
15
+ class SidekiqQueueMetrics
16
+ extend Forwardable
17
+ def_delegators :@queue, :size, :latency, :name
18
+ attr_reader :queue
19
+
20
+ def initialize(queue)
21
+ @queue = queue
22
+ end
23
+
24
+ def normalised_latency
25
+ metric = queue.latency.to_f / permitted_latency
26
+ metric.round(3)
27
+ end
28
+
29
+ def permitted_latency
30
+ prefix, number, unit = queue.name.partition(/[0-9]+/)
31
+ case prefix
32
+ when 'monitoring', 'realtime' then 10.seconds.to_i
33
+ when 'default' then 1.day.to_i
34
+ when 'within' then number.to_i.public_send(unit.to_sym).to_i
35
+ else raise "Cannot determine permitted latency for queue #{queue.name}"
36
+ end
37
+ end
38
+ end
39
+
40
+ class SidekiqWorkerCount
41
+ def initialize(metrics)
42
+ @metrics = metrics
43
+ end
44
+
45
+ def current_processes
46
+ ::Sidekiq::ProcessSet.new.count
47
+ end
48
+
49
+ def max_normalised_latency
50
+ @metrics.any? ? @metrics.map(&:normalised_latency).max : 0
51
+ end
52
+
53
+ def requested_processes
54
+ if max_normalised_latency > increasing_latency
55
+ current_processes + 1
56
+ elsif max_normalised_latency < decreasing_latency
57
+ [current_processes - 1, 1].max
58
+ else
59
+ current_processes
60
+ end
61
+ end
62
+
63
+ protected
64
+
65
+ def increasing_latency
66
+ ENV.fetch('WORKER_INCREASE_THRESHOLD', 0.5).to_f
67
+ end
68
+
69
+ def decreasing_latency
70
+ ENV.fetch('WORKER_DECREASE_THRESHOLD', 0.1).to_f
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,27 @@
1
+ namespace :db do
2
+ desc 'Prints out the database statement timeout'
3
+ task statement_timeout: :environment do
4
+ result = ActiveRecord::Base.connection.execute('SHOW statement_timeout').first
5
+ puts result['statement_timeout']
6
+ end
7
+
8
+ namespace :migrate do
9
+ task extend_statement_timeout: :environment do
10
+ if ActiveRecord::VERSION::MAJOR >= 4
11
+ config = ActiveRecord::Base.configurations[Rails.env]
12
+ config['variables'] ||= {}
13
+ config['variables']['statement_timeout'] = ENV.fetch('MIGRATION_STATEMENT_TIMEOUT', 10_000)
14
+ ActiveRecord::Base.establish_connection
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ %i[
21
+ db:create
22
+ db:migrate
23
+ db:migrate:down
24
+ db:rollback
25
+ ].each do |task|
26
+ Rake::Task[task].enhance(%i[db:migrate:extend_statement_timeout])
27
+ end
@@ -1,3 +1,3 @@
1
1
  module RooOnRails
2
- VERSION = '1.2.0'
2
+ VERSION = '1.3.0'
3
3
  end
data/lib/roo_on_rails.rb CHANGED
@@ -7,5 +7,8 @@ if defined?(Rails)
7
7
  require 'dotenv/rails-now'
8
8
  require 'roo_on_rails/railtie'
9
9
  require 'roo_on_rails/railties/new_relic'
10
+ require 'roo_on_rails/railties/database'
10
11
  require 'roo_on_rails/railties/http'
12
+ require 'roo_on_rails/railties/sidekiq'
13
+ require 'roo_on_rails/railties/rake_tasks'
11
14
  end
data/roo_on_rails.gemspec CHANGED
@@ -29,7 +29,8 @@ Gem::Specification.new do |spec|
29
29
  spec.add_runtime_dependency 'rack-timeout'
30
30
  spec.add_runtime_dependency 'rack-ssl-enforcer'
31
31
  spec.add_runtime_dependency 'octokit'
32
-
32
+ spec.add_runtime_dependency 'hirefire-resource'
33
+ spec.add_runtime_dependency 'sidekiq'
33
34
  spec.add_development_dependency 'bundler', '~> 1.13'
34
35
  spec.add_development_dependency 'rake', '~> 10.0'
35
36
  spec.add_development_dependency 'rspec', '~> 3.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roo_on_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julien Letessier
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-21 00:00:00.000000000 Z
11
+ date: 2017-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dotenv-rails
@@ -128,6 +128,34 @@ dependencies:
128
128
  - - ">="
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: hirefire-resource
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :runtime
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ - !ruby/object:Gem::Dependency
146
+ name: sidekiq
147
+ requirement: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ type: :runtime
153
+ prerelease: false
154
+ version_requirements: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
131
159
  - !ruby/object:Gem::Dependency
132
160
  name: bundler
133
161
  requirement: !ruby/object:Gem::Requirement
@@ -286,14 +314,26 @@ files:
286
314
  - lib/roo_on_rails/checks/heroku/token.rb
287
315
  - lib/roo_on_rails/checks/heroku/toolbelt_installed.rb
288
316
  - lib/roo_on_rails/checks/heroku/toolbelt_working.rb
317
+ - lib/roo_on_rails/checks/sidekiq/settings.rb
318
+ - lib/roo_on_rails/checks/sidekiq/sidekiq.rb
289
319
  - lib/roo_on_rails/config.rb
320
+ - lib/roo_on_rails/context_logging.rb
290
321
  - lib/roo_on_rails/default.env
322
+ - lib/roo_on_rails/environment.rb
291
323
  - lib/roo_on_rails/harness.rb
324
+ - lib/roo_on_rails/logfmt.rb
292
325
  - lib/roo_on_rails/rack/safe_timeouts.rb
293
326
  - lib/roo_on_rails/railtie.rb
327
+ - lib/roo_on_rails/railties/database.rb
294
328
  - lib/roo_on_rails/railties/http.rb
295
329
  - lib/roo_on_rails/railties/new_relic.rb
330
+ - lib/roo_on_rails/railties/rake_tasks.rb
331
+ - lib/roo_on_rails/railties/sidekiq.rb
296
332
  - lib/roo_on_rails/shell.rb
333
+ - lib/roo_on_rails/sidekiq/loader.rb
334
+ - lib/roo_on_rails/sidekiq/settings.rb
335
+ - lib/roo_on_rails/sidekiq/sla_metric.rb
336
+ - lib/roo_on_rails/tasks/db.rake
297
337
  - lib/roo_on_rails/version.rb
298
338
  - roo_on_rails.gemspec
299
339
  homepage: https://github.com/deliveroo/roo_on_rails