poller 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  # The HttpPoller will constuct an HttpProbe and sample it periodically based on
2
- # the value given for period_s until a timeout occurs after timeout_s seconds.
2
+ # the value given for period_seconds until a timeout occurs after timeout_seconds.
3
3
  #
4
4
  # The proxy information is expected to be either nil or given as an OpenStruct
5
5
  # with attributes 'hostname', 'port', 'user' and 'password'.
@@ -17,12 +17,12 @@ module Poller
17
17
  class HttpPoller
18
18
  include Poller
19
19
 
20
- def initialize(url, matcher, timeout_s, period_s, proxy = nil)
20
+ def initialize(url, matcher, timeout_seconds, period_seconds, proxy = nil)
21
21
  proxy = OpenStruct.new(proxy) if proxy.is_a?(Hash)
22
22
  probe = proxy.nil? \
23
23
  ? HttpProbe.new(url, matcher) \
24
24
  : HttpProbe.new(url, matcher, proxy.hostname, proxy.port, proxy.user, proxy.password)
25
- super(probe, timeout_s, period_s, url)
25
+ super(probe, timeout_seconds, period_seconds, url)
26
26
  end
27
27
 
28
28
  end
data/lib/poller/poller.rb CHANGED
@@ -1,36 +1,50 @@
1
- # This module handles the generic bits of the poller gem and
2
- # is intended to be mixed into any concrete Poller implementations
3
- # The generic parts include the periodic checking of a Probe and
4
- # raising a RuntimeError in case the timeout period has been exceeded.
1
+ # This module handles the generic parts of the poller gem and
2
+ # is intended to be mixed into all concrete Poller implementations.
3
+ #
4
+ # The generic parts include the periodic checking of a Probe and to
5
+ # raise a RuntimeError in case the Timeout period has been exceeded.
5
6
  #
6
7
  # Poller expects that the probe object has methods called 'satisfied?'
7
- # that will return a boolean and 'sample' which takes the next sample
8
- # and has no explicit return value.
8
+ # which must return a boolean and 'sample' which triggers taking the
9
+ # next sample and which has no explicit return value.
9
10
 
10
11
  require 'poller/timeout'
11
12
 
12
13
  module Poller
13
14
  module Poller
14
15
 
15
- def initialize(probe, timeout_s, period_s, name = nil)
16
+ def initialize(probe, timeout_seconds, period_seconds, name = nil)
16
17
  @probe = probe
17
- @timeout_s = timeout_s
18
- @period_s = period_s
18
+ @timeout_seconds = timeout_seconds
19
+ @period = period_seconds
19
20
  @name = name.nil? ? "no name given" : name
20
21
  end
21
22
 
22
23
  def check
23
- # This following line allows us to inject a timeout object from within our tests
24
- @timeout ||= Timeout.new(@timeout_s)
24
+ @timeout ||= Timeout.new(@timeout_seconds) # allow injecting a Timeout object from within tests
25
25
 
26
- while !@probe.satisfied?
27
- raise RuntimeError, "Timeout period has been exceeded for Poller (#{@name})" if @timeout.occured?
28
- Kernel.sleep @period_s
26
+ tries = 0
27
+ check_started_at = Time.now
28
+ last_sample_took = 0
29
29
 
30
+ while !@timeout.occured?
31
+ Kernel.sleep sleep_time(@period, last_sample_took)
32
+ sample_started_at = Time.now
30
33
  @probe.sample
34
+ satisfied = @probe.satisfied?
35
+ last_sample_took = Time.now - sample_started_at
36
+ tries += 1
37
+ return if satisfied
31
38
  end
32
39
 
40
+ raise RuntimeError, "Timeout period has been exceeded for Poller (#{@name})." \
41
+ + " Poller tried #{tries} times which in total took #{Time.now - check_started_at} seconds."
42
+ end
33
43
 
44
+ private
45
+ def sleep_time(period, last_sample_took)
46
+ st = period - last_sample_took
47
+ st < 0 ? 0 : st
34
48
  end
35
49
 
36
50
  end
@@ -2,9 +2,9 @@ module Poller
2
2
  class Timeout
3
3
 
4
4
  # Specify the timeout period in Integer or Floating Point representation
5
- # +period_s+:: The timeout period in seconds
6
- def initialize(period_s)
7
- @period = period_s
5
+ # +period_seconds+:: The timeout period in seconds
6
+ def initialize(period_seconds)
7
+ @period = period_seconds
8
8
  @start_time = Time.now
9
9
  end
10
10
 
@@ -1,3 +1,3 @@
1
1
  module Poller
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: poller
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-16 00:00:00.000000000 Z
12
+ date: 2013-03-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec