chillout 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,6 +7,8 @@ require 'chillout/config'
7
7
  require 'chillout/error'
8
8
  require 'chillout/event_data_builder'
9
9
  require 'chillout/prefixed_logger'
10
+ require 'chillout/worker'
11
+ require 'thread'
10
12
 
11
13
  module Chillout
12
14
  class Client
@@ -16,6 +18,7 @@ module Chillout
16
18
 
17
19
  attr_reader :config
18
20
  attr_reader :logger
21
+ attr_reader :queue
19
22
 
20
23
  def initialize(config_or_api_key, options = {})
21
24
  build_config(config_or_api_key, options)
@@ -27,14 +30,26 @@ module Chillout
27
30
  @http_client = HttpClient.new(@config, logger)
28
31
  @event_data_builder = EventDataBuilder.new(@config)
29
32
  @server_side = ServerSide.new(@event_data_builder, @http_client)
30
- @filter = ErrorFilter.new
31
- @dispatcher = Dispatcher.new(@filter, @server_side)
33
+ @filter = ErrorFilter.new
34
+ @dispatcher = Dispatcher.new(@filter, @server_side)
35
+ @queue = Queue.new
32
36
  end
33
37
 
34
38
  def send_exception(exception, environment = {})
35
39
  send_error(Error.new(exception, environment))
36
40
  end
37
41
 
42
+ def enqueue(creations)
43
+ @queue << creations
44
+ end
45
+
46
+ def start_worker
47
+ thread = Thread.new do
48
+ worker = Worker.new(@dispatcher, @queue, @logger)
49
+ worker.run
50
+ end
51
+ end
52
+
38
53
  private
39
54
  def build_config(config_or_api_key, options)
40
55
  case config_or_api_key
@@ -4,18 +4,28 @@ module Chillout
4
4
  @container = Hash.new {|hash, key| hash[key] = 0}
5
5
  end
6
6
 
7
- def increment!(class_name)
8
- key = class_name.to_sym
9
- @container[key] += 1
7
+ def increment!(class_name, value=1)
8
+ key = key(class_name)
9
+ @container[key] += value
10
10
  end
11
11
 
12
12
  def [](name)
13
- key = name.to_sym
13
+ key = key(name)
14
14
  @container[key]
15
15
  end
16
16
 
17
+ def key(name)
18
+ name.to_sym
19
+ end
20
+
17
21
  def resource_keys
18
22
  @container.keys
19
23
  end
24
+
25
+ def merge(other_container)
26
+ for key in other_container.resource_keys
27
+ increment!(key, other_container[key])
28
+ end
29
+ end
20
30
  end
21
31
  end
@@ -8,8 +8,9 @@ module Chillout
8
8
 
9
9
  def call(env)
10
10
  response = @app.call(env)
11
+ ensure
11
12
  if Thread.current[:creations]
12
- @client.send_creations(Thread.current[:creations])
13
+ @client.enqueue(Thread.current[:creations])
13
14
  Thread.current[:creations] = nil
14
15
  end
15
16
  response
@@ -15,6 +15,8 @@ module Chillout
15
15
 
16
16
  app.middleware.use Middleware::ExceptionMonitor, client
17
17
  app.middleware.use Middleware::CreationsMonitor, client
18
+
19
+ client.start_worker
18
20
  end
19
21
  end
20
22
  end
@@ -1,3 +1,3 @@
1
1
  module Chillout
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -0,0 +1,31 @@
1
+ module Chillout
2
+ class Worker
3
+ attr_reader :dispatcher, :queue, :logger
4
+
5
+ def initialize(dispatcher, queue, logger)
6
+ @dispatcher = dispatcher
7
+ @queue = queue
8
+ @logger = logger
9
+ end
10
+
11
+ def run
12
+ loop do
13
+ containers_to_merge = [queue.pop]
14
+ loop do
15
+ begin
16
+ containers_to_merge << queue.pop(true)
17
+ rescue ThreadError
18
+ break
19
+ end
20
+ end
21
+ creations_container = CreationsContainer.new
22
+ for container in containers_to_merge
23
+ creations_container.merge(container)
24
+ end
25
+ dispatcher.send_creations(creations_container)
26
+ logger.info "Sending metrics"
27
+ sleep 5
28
+ end
29
+ end
30
+ end
31
+ end
@@ -3,10 +3,25 @@ require 'chillout/creations_container'
3
3
 
4
4
  module Chillout
5
5
  class CreationsContainerTest < ChilloutTestCase
6
+ def setup
7
+ @creations_container = CreationsContainer.new
8
+ end
9
+
6
10
  def test_increment_model
7
- creations_container = CreationsContainer.new
8
- creations_container.increment!("User")
9
- assert_equal 1, creations_container[:User]
11
+ @creations_container.increment!("User")
12
+ assert_equal 1, @creations_container[:User]
13
+ end
14
+
15
+ def test_merge
16
+ @first_to_merge = CreationsContainer.new
17
+ @first_to_merge.increment!("User", 2)
18
+ @second_to_merge = CreationsContainer.new
19
+ @second_to_merge.increment!("User")
20
+ @second_to_merge.increment!("Entity")
21
+ @creations_container.merge(@first_to_merge)
22
+ @creations_container.merge(@second_to_merge)
23
+ assert_equal 3, @creations_container[:User]
24
+ assert_equal 1, @creations_container[:Entity]
10
25
  end
11
26
  end
12
27
  end
@@ -11,6 +11,7 @@ module Chillout
11
11
  @config = Chillout::Config.new(api_key)
12
12
  @config.ssl = false
13
13
  @client = Chillout::Client.new(@config)
14
+ @client.start_worker
14
15
  end
15
16
 
16
17
  def app
@@ -29,6 +30,7 @@ module Chillout
29
30
 
30
31
  def test_creations_values
31
32
  get "/"
33
+ Thread.pass
32
34
  assert_equal 2, request_body["content"]["creations"]["User"]
33
35
  assert_equal 3, request_body["content"]["creations"]["Cart"]
34
36
  end
@@ -18,14 +18,13 @@ module Chillout
18
18
  end
19
19
 
20
20
  def test_behaves_like_rack_middleware
21
- @client.stubs(:send_creations)
21
+ @client.stubs(:enqueue)
22
22
  response = @middleware.call(@env)
23
-
24
23
  assert_equal [200, @env, ['hello']], response
25
24
  end
26
25
 
27
- def test_dispatch_creations_from_thread_current
28
- @client.expects(:send_creations).with(:creations)
26
+ def test_clients_queue_receive_creations
27
+ @client.expects(:enqueue).with(:creations)
29
28
 
30
29
  @middleware.call(@env)
31
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chillout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-03-11 00:00:00.000000000 Z
13
+ date: 2013-03-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: minitest
@@ -139,6 +139,7 @@ files:
139
139
  - lib/chillout/railtie.rb
140
140
  - lib/chillout/server_side.rb
141
141
  - lib/chillout/version.rb
142
+ - lib/chillout/worker.rb
142
143
  - test/client_test.rb
143
144
  - test/config_test.rb
144
145
  - test/creations_container_test.rb
@@ -168,7 +169,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
168
169
  version: '0'
169
170
  segments:
170
171
  - 0
171
- hash: 2687289254798822553
172
+ hash: -4082367495555425928
172
173
  required_rubygems_version: !ruby/object:Gem::Requirement
173
174
  none: false
174
175
  requirements:
@@ -177,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
178
  version: '0'
178
179
  segments:
179
180
  - 0
180
- hash: 2687289254798822553
181
+ hash: -4082367495555425928
181
182
  requirements: []
182
183
  rubyforge_project:
183
184
  rubygems_version: 1.8.25