babysitter 0.0.12 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,6 +9,7 @@ require_relative "babysitter/tracker"
9
9
  require_relative "babysitter/monitor"
10
10
  require_relative "babysitter/counter"
11
11
  require_relative "babysitter/exception_notifiers"
12
+ require_relative "babysitter/logger_with_deprecation"
12
13
  require 'fozzie'
13
14
 
14
15
  module Babysitter
@@ -12,7 +12,7 @@ module Babysitter
12
12
  @log_every = log_every
13
13
  @stat_name = opts.delete(:stat_name)
14
14
  @counting = opts.delete(:counting)
15
- @timer_start = Time.now
15
+ @timer_start = opts.delete(:timer_start) || Time.now
16
16
  end
17
17
 
18
18
  def inc( template, amount=1, opts={} )
@@ -0,0 +1,53 @@
1
+ module Babysitter
2
+
3
+ class LoggerWithDeprecation
4
+
5
+ attr_reader :delegate
6
+
7
+ def initialize(opts={})
8
+ @delegate = opts.delete(:logger) || Babysitter.configuration.logger
9
+ end
10
+
11
+ def self.notice(caller)
12
+ return if caller_has_been_logged?(caller)
13
+ Kernel.warn("[DEPRECATED] You are using old style logging. Please use tracker (yielded by monitor.start) instead. Caller was #{caller.first}")
14
+ Kernel.warn("[DEPRECATED] See https://github.com/lonelyplanet/rowlf/blob/master/LOGGING.md for more info")
15
+ end
16
+
17
+ def warn(*args)
18
+ self.class.notice(caller)
19
+ delegate.warn(*args)
20
+ end
21
+
22
+ def error(*args)
23
+ self.class.notice(caller)
24
+ delegate.error(*args)
25
+ end
26
+
27
+ def info(*args)
28
+ delegate.info(*args)
29
+ end
30
+
31
+ def debug(*args)
32
+ delegate.debug(*args)
33
+ end
34
+
35
+ def fatal(*args)
36
+ delegate.fatal(*args)
37
+ end
38
+
39
+ private
40
+
41
+ def self.caller_has_been_logged?(caller)
42
+ return true if caller_seen.include?(caller.first)
43
+ caller_seen << caller.first
44
+ return false
45
+ end
46
+
47
+ def self.caller_seen
48
+ @caller_seen ||= []
49
+ end
50
+
51
+ end
52
+
53
+ end
@@ -2,18 +2,26 @@ module Babysitter
2
2
  class Tracker
3
3
  include Logging
4
4
 
5
- attr_reader :counting, :stat_name, :counter
5
+ attr_reader :counting, :stat_name, :timer_start
6
6
  attr_accessor :log_every
7
7
 
8
8
  def initialize(log_every, stat_name=nil)
9
9
  @stat_name = stat_name
10
- @counting = :iterations
10
+ @counting = default_counting
11
11
  @log_every = log_every
12
- @counter = Counter.new(log_every, stat_name: stat_name, counting: counting)
12
+ @timer_start = Time.now
13
+ @counters = Hash.new do |h, k|
14
+ h[k] = Counter.new(log_every, stat_name: stat_name, counting: k, timer_start: timer_start)
15
+ end
13
16
  end
14
17
 
15
- def inc(*args)
16
- counter.inc(*args)
18
+ def inc(template, inc=1, opts={})
19
+ counting = opts[:counting] || default_counting
20
+ counter(counting).inc(template, inc, opts)
21
+ end
22
+
23
+ def counter(counting=default_counting)
24
+ @counters[counting]
17
25
  end
18
26
 
19
27
  def count
@@ -21,7 +29,9 @@ module Babysitter
21
29
  end
22
30
 
23
31
  def final_report
24
- counter.log_counter_messsage if counter.final_report?
32
+ @counters.values.each do |counter|
33
+ counter.log_counter_messsage if counter.final_report?
34
+ end
25
35
  end
26
36
 
27
37
  def warn(topic_name, message)
@@ -33,7 +43,9 @@ module Babysitter
33
43
  end
34
44
 
35
45
  def send_total_stats
36
- counter.send_total_stats
46
+ @counters.values.each do |counter|
47
+ counter.send_total_stats
48
+ end
37
49
  end
38
50
 
39
51
  def logger_with_stats_for(topic_name)
@@ -47,6 +59,10 @@ module Babysitter
47
59
  stat_name+[topic_name]
48
60
  end
49
61
 
62
+ def default_counting
63
+ :iterations
64
+ end
65
+
50
66
  end
51
67
 
52
68
  end
@@ -1,3 +1,3 @@
1
1
  module Babysitter
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.14"
3
3
  end
@@ -115,6 +115,24 @@ module Babysitter
115
115
  end
116
116
  end # context 'when logging every 10th call, and the block increments the counter 7 times, each with a count of 9, and identifies counted objects' do
117
117
 
118
+ context 'when the block logs 3 increments to oranges counter, and 5 increments to apples:' do
119
+ let(:start_block_3_oranges_5_apples) do
120
+ Proc.new do |tracker|
121
+ 3.times{ tracker.inc('{{count}} oranges counted', 1, counting: :orangey_things) }
122
+ 5.times{ tracker.inc('{{count}} apples counted', 1, counting: :appley_things) }
123
+ end
124
+ end
125
+
126
+ it 'calls logger with 3 oranges counted and 5 apples counted' do
127
+ Counter.any_instance.stub(:logger).and_return(logger)
128
+ logger.stub(:info).with(anything)
129
+ logger.should_receive(:info).with(/3 oranges counted/)
130
+ logger.should_receive(:info).with(/5 apples counted/)
131
+ subject.start('short message', 10, &start_block_3_oranges_5_apples)
132
+ end
133
+
134
+ end # context 'when the block logs 3 increments to oranges counter, and 5 increments to apples:' do
135
+
118
136
  context "when the block logs a warning" do
119
137
  let(:start_block_with_warning) do
120
138
  Proc.new do |monitor|
@@ -75,7 +75,68 @@ module Babysitter
75
75
 
76
76
  end # describe 'logger returned by logger_with_stats_for(:something)' do
77
77
 
78
- end
78
+ describe 'counting:' do
79
+
80
+ context 'inc called 3 times, with no :counting' do
81
+ before(:each) do
82
+ subject.inc('some message', 3)
83
+ logger.stub(:info)
84
+ end
85
+
86
+ it 'the counter should be 3' do
87
+ subject.counter.count.should == 3
88
+ end
89
+
90
+ context 'and then incremented by 5 with no :counting' do
91
+ before(:each) do
92
+ subject.inc('some message', 5)
93
+ end
94
+
95
+ it 'the counter should be 8' do
96
+ subject.counter.count.should == 8
97
+ end
98
+ end
99
+
100
+ context 'and then incremented by 5 with :counting => apples' do
101
+ before(:each) do
102
+ subject.inc('some message', 5, counting: :apples)
103
+ end
104
+
105
+ it 'the counter should be 3' do
106
+ subject.counter.count.should == 3
107
+ end
108
+
109
+ it 'the apples counter should be 5' do
110
+ subject.counter(:apples).count.should == 5
111
+ end
112
+
113
+ context 'and then incremented by 4 with :counting => oranges' do
114
+ before(:each) do
115
+ subject.inc('some message', 4, counting: :oranges)
116
+ end
117
+
118
+ it 'the counter should be 3' do
119
+ subject.counter.count.should == 3
120
+ end
121
+
122
+ it 'the apples counter should be 5' do
123
+ subject.counter(:apples).count.should == 5
124
+ end
125
+
126
+ it 'the oranges counter should be 4' do
127
+ subject.counter(:oranges).count.should == 4
128
+ end
129
+
130
+ end
131
+
132
+ end
133
+
134
+ end
135
+
136
+ end # describe 'counting:' do
137
+
138
+ end # describe Tracker do
139
+
79
140
  end
80
141
 
81
142
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: babysitter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.14
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2013-02-13 00:00:00.000000000 Z
15
+ date: 2013-03-01 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: fozzie
@@ -115,6 +115,7 @@ files:
115
115
  - lib/babysitter/counter.rb
116
116
  - lib/babysitter/exception_notifiers.rb
117
117
  - lib/babysitter/exception_notifiers/simple_notification_service.rb
118
+ - lib/babysitter/logger_with_deprecation.rb
118
119
  - lib/babysitter/logger_with_stats.rb
119
120
  - lib/babysitter/logging.rb
120
121
  - lib/babysitter/monitor.rb
@@ -147,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
148
  version: '0'
148
149
  requirements: []
149
150
  rubyforge_project:
150
- rubygems_version: 1.8.24
151
+ rubygems_version: 1.8.22
151
152
  signing_key:
152
153
  specification_version: 3
153
154
  summary: Babysits long-running processes and reports progress or failures