rails_runner 0.0.2 → 0.0.4

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
  SHA1:
3
- metadata.gz: 41d4daada8041fb8cb32f31f1abf2213e2b272b1
4
- data.tar.gz: 864d7359d31123a2ffdb2014ef7a9490f00870b5
3
+ metadata.gz: 7fe6d85e7c06db4cc3bbcb423aeb8c1648141f72
4
+ data.tar.gz: a34cc5a4faa0cf463116520178c39dcf42b098c0
5
5
  SHA512:
6
- metadata.gz: 6dc7e7f3ae396845b873df5966ca48bcb20735fc3f9280f37c3ccd98475c888f6cd163798715bd4082241a2dc80f3e939adb46ad88a7059b75899407728e95a5
7
- data.tar.gz: 4e549cc52732cd1903f36393f3507103d8ab979518fb094e967f7005d1b83413000b04da54c821151207aa9a029c6c17fdc852acbe28f4a409756ee446da9e0e
6
+ metadata.gz: dbbaf49f496fc5b8f35ecf36f5af2a5917194422f3b5cea1d5c00065f192171b647cd7d3be17e69300034a4fd6d60bb84d67f21f83d9cac440f5bf251faf5cae
7
+ data.tar.gz: ff5fd166586a9c6236fab17527e667c002cc8b35625372ca7a33615f1e18c1aeb19cca18ecca51afb5b24dba2cca7bcfd0554d19b70c89c2369f00c3b22be6c3
@@ -3,13 +3,11 @@ module RailsRunner
3
3
  initializer 'rails_runner.configure' do |app|
4
4
  RailsRunner.config = RailsRunner::Config.new(app.root.join('config/railsrunner.yml'))
5
5
 
6
- Rails.logger.debug RailsRunner.config.to_yaml
6
+ RailsRunner.logger.debug RailsRunner.config.to_yaml
7
7
 
8
8
  if RailsRunner.config.active?
9
- Instrumenter.start!
10
- app.middleware.insert 0, Middleware
11
-
12
- Rails.logger.debug "[RAILSRUNNER] started #{RailsRunner.config.collector_url}"
9
+ RailsRunner::Subscriber.register
10
+ RailsRunner.logger.debug "[RAILSRUNNER] started #{RailsRunner.config.collector_url}"
13
11
  end
14
12
  end
15
13
  end
@@ -0,0 +1,43 @@
1
+ module RailsRunner
2
+ class Subscriber
3
+
4
+ class << self
5
+ def register
6
+ ActiveSupport::Notifications.subscribe /process_action/, self.new
7
+ end
8
+ end
9
+
10
+ def call(*args)
11
+ event = ActiveSupport::Notifications::Event.new(*args)
12
+
13
+ payload = event.payload
14
+ endpoint = "#{payload[:controller]}##{payload[:action]}"
15
+ minute = event.time.to_i - (event.time.to_i % 60)
16
+
17
+ if payload[:exception]
18
+ trigger_event(minute, gauge(endpoint, 'error'), event.duration)
19
+ trigger_event(minute, gauge('error'), event.duration)
20
+ else
21
+ trigger_event(minute, gauge(endpoint, 'total'), event.duration)
22
+ trigger_event(minute, gauge(endpoint, 'view'), payload[:view_runtime]) if payload[:view_runtime]
23
+ trigger_event(minute, gauge(endpoint, 'db'), payload[:db_runtime]) if payload[:db_runtime]
24
+ trigger_event(minute, gauge(endpoint, 'ext'), payload[:ext_runtime]) if payload[:ext_runtime]
25
+ end
26
+ trigger_event(minute, gauge('total'), event.duration)
27
+ end
28
+
29
+ # @return [RailsRunner::Worker]
30
+ def worker
31
+ RailsRunner::Worker.instance
32
+ end
33
+
34
+ def trigger_event(minute, gauge, duration)
35
+ RailsRunner.logger.debug [minute, gauge, duration]
36
+ worker.push(RailsRunner::Event.new(minute, gauge, duration.to_i))
37
+ end
38
+
39
+ def gauge(*args)
40
+ args.join('/')
41
+ end
42
+ end
43
+ end
@@ -20,7 +20,7 @@ module RailsRunner
20
20
  request.body = gzipped
21
21
  request['Authorization'] = "#{RailsRunner.config.client_id}"
22
22
 
23
- Rails.logger.debug "uri: #{uri.host}:#{uri.port} #{uri.request_uri}, header: #{RailsRunner.config.client_id} body length: #{gzipped.length}"
23
+ RailsRunner.logger.debug "uri: #{uri.host}:#{uri.port} #{uri.request_uri}, header: #{RailsRunner.config.client_id} body length: #{gzipped.length}"
24
24
 
25
25
  http.request(request)
26
26
  end
@@ -1,3 +1,3 @@
1
1
  module RailsRunner
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,30 +1,33 @@
1
1
  module RailsRunner
2
2
  class Worker
3
- MUTEX = Mutex.new
3
+ MUTEX = Mutex.new
4
+ FLUSH_PERIOD = 10.seconds
5
+ MAX_MULTIPLIER = 5
4
6
 
5
7
  attr_accessor :requests
6
8
 
7
9
  class << self
8
- def start!
9
- return if @instance
10
+ def instance
11
+ return @instance if @instance
10
12
  MUTEX.synchronize do
11
13
  return @instance if @instance
12
14
  @instance = new.start
13
15
  end
14
16
  end
15
-
16
- def instance
17
- @instance
18
- end
19
17
  end
20
18
 
21
19
  def initialize
22
- @queue = Queue.new
20
+ @queue = Queue.new
21
+ @multiplier = 1
23
22
  reset
24
23
  end
25
24
 
26
25
  def reset
27
- @requests = Hash.new { |h,k| h[k] = Hash.new { |h,k| h[k] = 0 } }
26
+ @requests = Hash.new do |requests, minutes|
27
+ requests[minutes] = Hash.new do |minute, gauge|
28
+ minute[gauge] = {sum: 0, count: 0}
29
+ end
30
+ end
28
31
  end
29
32
 
30
33
  def current_minute
@@ -34,41 +37,50 @@ module RailsRunner
34
37
 
35
38
  def start
36
39
  Thread.new do
37
- while (request = @queue.pop)
40
+ while (event = @queue.pop)
38
41
  begin
39
- case request
40
- when Integer
41
- @requests[current_minute][request] += 1
42
+ case event
43
+ when Event
44
+ update_event(event)
42
45
  when :flush
43
46
  send_to_collector
44
47
  end
45
48
  rescue Exception => e
46
- puts e.message
47
- puts e.backtrace.join("\n")
49
+ RailsRunner.logger.debug ["Event error:", event.inspect, e.message]
48
50
  end
49
51
  end
50
52
  end
53
+ start_flusher
54
+
55
+ self
56
+ end
57
+
58
+ def update_event(event)
59
+ @requests[event.minute][event.gauge][:sum] += event.duration
60
+ @requests[event.minute][event.gauge][:count] += 1
61
+ end
51
62
 
63
+ def start_flusher
52
64
  Thread.new do
53
65
  loop do
54
- sleep 10.seconds
55
- Rails.logger.debug "Sending flush message"
66
+ sleep @multiplier * FLUSH_PERIOD
56
67
  push(:flush)
57
68
  end
58
69
  end
59
-
60
- self
61
70
  end
62
71
 
63
- def push(request)
64
- @queue << request
72
+ def push(event)
73
+ @queue << event
65
74
  end
66
75
 
67
76
  def send_to_collector
68
- Rails.logger.debug "Sending to collector"
69
- Rails.logger.flush
77
+ RailsRunner.logger.debug "Sending to collector"
70
78
 
71
- Util.post(requests.to_json)
79
+ if Util.post(requests.to_json)
80
+ @multiplier = 1
81
+ else
82
+ @multiplier += 1 if @multiplier < MAX_MULTIPLIER
83
+ end
72
84
 
73
85
  reset
74
86
  end
data/lib/rails_runner.rb CHANGED
@@ -1,9 +1,9 @@
1
- require 'beefcake'
2
-
3
1
  module RailsRunner
2
+ Event = Struct.new(:minute, :gauge, :duration)
3
+
4
4
  require 'rails_runner/util'
5
- require 'rails_runner/instrumenter'
6
5
  require 'rails_runner/middleware'
6
+ require 'rails_runner/subscriber'
7
7
  require 'rails_runner/railtie'
8
8
  require 'rails_runner/worker'
9
9
  require 'rails_runner/config'
@@ -17,4 +17,10 @@ module RailsRunner
17
17
  @config || RailsRunner::Config.new(Rails.root.join('config/railsrunner.yml'))
18
18
  end
19
19
  module_function :config
20
+
21
+ class << self
22
+ def logger
23
+ @logger ||= Logger.new('log/rails_runner.log')
24
+ end
25
+ end
20
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benoist Claassen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-20 00:00:00.000000000 Z
11
+ date: 2013-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.0.0
27
- - !ruby/object:Gem::Dependency
28
- name: beefcake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ~>
32
- - !ruby/object:Gem::Version
33
- version: '0.3'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ~>
39
- - !ruby/object:Gem::Version
40
- version: '0.3'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: sqlite3
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -60,9 +46,9 @@ extensions: []
60
46
  extra_rdoc_files: []
61
47
  files:
62
48
  - lib/rails_runner/config.rb
63
- - lib/rails_runner/instrumenter.rb
64
49
  - lib/rails_runner/middleware.rb
65
50
  - lib/rails_runner/railtie.rb
51
+ - lib/rails_runner/subscriber.rb
66
52
  - lib/rails_runner/util.rb
67
53
  - lib/rails_runner/version.rb
68
54
  - lib/rails_runner/worker.rb
@@ -1,29 +0,0 @@
1
- module RailsRunner
2
- class Instrumenter
3
- MUTEX = Mutex.new
4
-
5
- class << self
6
- def start!
7
- return @instance if @instance
8
-
9
- MUTEX.synchronize do
10
- return @instance if @instance
11
- @instance = new.start
12
- end
13
- end
14
-
15
- def instance
16
- @instance
17
- end
18
- end
19
-
20
- def start
21
- @worker = Worker.start!
22
- self
23
- end
24
-
25
- def publish(time)
26
- @worker.push(time)
27
- end
28
- end
29
- end