queue_kit 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,15 +1,6 @@
1
1
  module QueueKit
2
2
  module Clients
3
3
  module CommandTimeout
4
- def self.with_ivars(klass)
5
- mod = self
6
- klass.class_eval do
7
- include mod
8
- attr_accessor :command_timeout_ms
9
- attr_accessor :max_command_timeout_ms
10
- end
11
- end
12
-
13
4
  def command_timeout(attempts = 0)
14
5
  timeout = command_timeout_ms
15
6
  timeout += timeout * (attempts / command_clients_size).floor
@@ -22,16 +13,16 @@ module QueueKit
22
13
  end
23
14
 
24
15
  def command_timeout_from(options)
25
- @command_timeout_ms = options[:command_timeout_ms] || 10
26
- @max_command_timeout_ms = options[:max_command_timeout_ms] || 1000
16
+ @command_timeout_ms = options[:command_timeout_ms]
17
+ @max_command_timeout_ms = options[:max_command_timeout_ms]
27
18
  end
28
19
 
29
20
  def command_timeout_ms
30
- 10
21
+ @command_timeout_ms ||= 10
31
22
  end
32
23
 
33
24
  def max_command_timeout_ms
34
- 1000
25
+ @max_command_timeout_ms ||= 1000
35
26
  end
36
27
 
37
28
  def command_clients_size
@@ -1,12 +1,11 @@
1
1
  module QueueKit
2
2
  module Clients
3
3
  module RoundRobinShuffler
4
- def self.with_ivars(klass)
5
- mod = self
6
- klass.class_eval do
7
- include mod
8
- attr_accessor :commands_per_client
4
+ include QueueKit::Instrumentable
9
5
 
6
+ def self.included(klass)
7
+ super(klass)
8
+ klass.class_eval do
10
9
  def command_clients_size
11
10
  @clients.size
12
11
  end
@@ -35,14 +34,15 @@ module QueueKit
35
34
  rotate_client
36
35
  end
37
36
 
38
- clients[@client_index]
37
+ @current_client
39
38
  end
40
39
 
41
40
  def round_robin_from(options)
42
- @commands_per_client = options[:commands_per_client] || 100
41
+ @commands_per_client = options[:commands_per_client]
43
42
  end
44
43
 
45
44
  def rotate_client
45
+ instrument "worker.rotate_client"
46
46
  @client_index ||= -1
47
47
  @client_len ||= clients.size
48
48
 
@@ -52,10 +52,12 @@ module QueueKit
52
52
  if @client_index >= @client_len
53
53
  @client_index = 0
54
54
  end
55
+
56
+ @current_client = clients[@client_index]
55
57
  end
56
58
 
57
59
  def commands_per_client
58
- 100
60
+ @commands_per_client ||= 100
59
61
  end
60
62
 
61
63
  def clients
@@ -0,0 +1,47 @@
1
+ module QueueKit
2
+ module Instrumentable
3
+ def instrumenter_from(options)
4
+ @instrumenter = options[:instrumenter]
5
+ if options.fetch(:debug) { false }
6
+ enable_debug_mode
7
+ end
8
+ end
9
+
10
+ def instrumenter
11
+ @instrumenter ||= default_instrumenter
12
+ end
13
+
14
+ def instrument(name, payload = nil)
15
+ options = default_instrument_options
16
+ options.update(payload) if payload
17
+ instrumenter.instrument("queuekit.#{name}", options)
18
+ end
19
+
20
+ def force_debug
21
+ instrument(*yield)
22
+ end
23
+
24
+ def debug
25
+ end
26
+
27
+ def enable_debug_mode
28
+ class << self
29
+ alias debug force_debug
30
+ end
31
+ end
32
+
33
+ def default_instrument_options
34
+ {}
35
+ end
36
+
37
+ def default_instrumenter
38
+ PutsInstrumenter.new
39
+ end
40
+
41
+ class PutsInstrumenter
42
+ def instrument(name, payload = nil)
43
+ puts "[#{Time.now}] #{name}: #{payload.inspect}"
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,18 +1,15 @@
1
1
  module QueueKit
2
2
  module Worker
3
+ include Instrumentable
4
+
3
5
  def initialize(queue, options = {})
4
6
  @queue = queue
5
7
  @processor = options.fetch(:processor) { method(:process) }
6
8
  @cooler = options.fetch(:cooler) { method(:cool) }
7
9
  @error_handler = options.fetch(:error_handler) { method(:handle_error) }
8
- @instrumenter = options.fetch(:instrumenter) { PutsInstrumenter.new }
9
10
  @stopped = true
10
11
 
11
- if options.fetch(:debug) { false }
12
- class << self
13
- alias debug force_debug
14
- end
15
- end
12
+ instrumenter_from(options)
16
13
  end
17
14
 
18
15
  def process(item)
@@ -26,7 +23,7 @@ module QueueKit
26
23
  raise err
27
24
  end
28
25
 
29
- def trap(signal_handler)
26
+ def trap_signals(signal_handler)
30
27
  SignalChecker.trap(self, signal_handler)
31
28
  end
32
29
 
@@ -35,7 +32,8 @@ module QueueKit
35
32
  interval_debugger = lambda { "worker.interval" }
36
33
 
37
34
  loop do
38
- working? ? work : break
35
+ work
36
+ break unless working?
39
37
  debug(&interval_debugger)
40
38
  end
41
39
  end
@@ -55,7 +53,7 @@ module QueueKit
55
53
  @processor.call(item)
56
54
  set_popping_procline
57
55
  else
58
- @cooler.call
56
+ @cooler.call if working?
59
57
  end
60
58
  end
61
59
 
@@ -84,11 +82,6 @@ module QueueKit
84
82
  !@stopped
85
83
  end
86
84
 
87
- def instrument(name, payload = nil)
88
- (payload ||= {}).update(:worker => self)
89
- @instrumenter.instrument("queuekit.#{name}", payload)
90
- end
91
-
92
85
  def set_working_procline
93
86
  procline("Processing since #{Time.now.to_i}")
94
87
  end
@@ -98,17 +91,8 @@ module QueueKit
98
91
  procline("Waiting since #{@last_job_at.to_i}")
99
92
  end
100
93
 
101
- def force_debug
102
- instrument(*yield)
103
- end
104
-
105
- def debug
106
- end
107
-
108
- class PutsInstrumenter
109
- def instrument(name, payload = nil)
110
- puts "[#{Time.now}] #{name}: #{payload.inspect}"
111
- end
94
+ def default_instrument_options
95
+ {:worker => self}
112
96
  end
113
97
  end
114
98
  end
data/lib/queue_kit.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module QueueKit
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.8"
3
3
  ROOT = File.expand_path("../queue_kit", __FILE__)
4
4
 
5
5
  def self.require_lib(*libs)
@@ -12,6 +12,6 @@ module QueueKit
12
12
  alias require_libs require_lib
13
13
  end
14
14
 
15
- require_lib "worker"
15
+ require_lib "instrumentable", "worker"
16
16
  end
17
17
 
@@ -6,8 +6,8 @@ class CommandTimeoutTest < Test::Unit::TestCase
6
6
 
7
7
  def test_with_ivars
8
8
  object = FakeQueue.new
9
- assert_nil object.command_timeout_ms
10
- assert_nil object.max_command_timeout_ms
9
+ assert_equal 10, object.command_timeout_ms
10
+ assert_equal 1000, object.max_command_timeout_ms
11
11
 
12
12
  object.command_timeout_from({})
13
13
  assert_equal 10, object.command_timeout_ms
@@ -33,7 +33,7 @@ class CommandTimeoutTest < Test::Unit::TestCase
33
33
  end
34
34
 
35
35
  class FakeQueue
36
- QueueKit::Clients::CommandTimeout.with_ivars(self)
36
+ include QueueKit::Clients::CommandTimeout
37
37
  end
38
38
  end
39
39
 
data/test/helper.rb CHANGED
@@ -2,3 +2,8 @@ require 'bundler'
2
2
  require 'test/unit'
3
3
  require File.expand_path("../../lib/queue_kit", __FILE__)
4
4
 
5
+ class NullInstrumenter
6
+ def instrument(name, payload = nil)
7
+ end
8
+ end
9
+
@@ -37,7 +37,7 @@ class RoundRobinShufflerTest < Test::Unit::TestCase
37
37
 
38
38
  def test_with_ivars
39
39
  object = FakeQueue.new
40
- assert_nil object.commands_per_client
40
+ assert_equal 100, object.commands_per_client
41
41
 
42
42
  object.round_robin_from({})
43
43
  assert_equal 100, object.commands_per_client
@@ -73,7 +73,15 @@ class RoundRobinShufflerTest < Test::Unit::TestCase
73
73
  end
74
74
 
75
75
  class FakeQueue
76
- QueueKit::Clients::RoundRobinShuffler.with_ivars(self)
76
+ include QueueKit::Clients::RoundRobinShuffler
77
+
78
+ def default_instrumenter
79
+ NullInstrumenter.new
80
+ end
81
+ end
82
+
83
+ def default_instrumenter
84
+ NullInstrumenter.new
77
85
  end
78
86
  end
79
87
 
data/test/worker_test.rb CHANGED
@@ -3,12 +3,13 @@ require File.expand_path("../helper", __FILE__)
3
3
  class WorkerTest < Test::Unit::TestCase
4
4
  def test_cooler
5
5
  cooled = false
6
- cooler = lambda { cooled = true }
6
+ worker = nil
7
+ cooler = lambda { worker.stop;cooled = true }
7
8
 
8
9
  worker = new_worker [], :processor => lambda { |_| fail 'item found?' },
9
10
  :cooler => cooler
10
11
 
11
- worker.work
12
+ worker.run
12
13
  assert cooled
13
14
  end
14
15
 
@@ -72,10 +73,5 @@ class WorkerTest < Test::Unit::TestCase
72
73
  class Worker
73
74
  include QueueKit::Worker
74
75
  end
75
-
76
- class NullInstrumenter
77
- def instrument(name, payload = nil)
78
- end
79
- end
80
76
  end
81
77
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: queue_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -23,6 +23,7 @@ files:
23
23
  - queue_kit.gemspec
24
24
  - lib/queue_kit/clients/command_timeout.rb
25
25
  - lib/queue_kit/clients/round_robin_shuffler.rb
26
+ - lib/queue_kit/instrumentable.rb
26
27
  - lib/queue_kit/signal_checker.rb
27
28
  - lib/queue_kit/signal_handlers/graceful_quit.rb
28
29
  - lib/queue_kit/worker.rb