poller 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/poller/http/http_poller.rb +3 -3
- data/lib/poller/poller.rb +28 -14
- data/lib/poller/timeout.rb +3 -3
- data/lib/poller/version.rb +1 -1
- metadata +2 -2
@@ -1,5 +1,5 @@
|
|
1
1
|
# The HttpPoller will constuct an HttpProbe and sample it periodically based on
|
2
|
-
# the value given for
|
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,
|
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,
|
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
|
2
|
-
# is intended to be mixed into
|
3
|
-
#
|
4
|
-
#
|
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
|
-
#
|
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,
|
16
|
+
def initialize(probe, timeout_seconds, period_seconds, name = nil)
|
16
17
|
@probe = probe
|
17
|
-
@
|
18
|
-
@
|
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
|
-
|
24
|
-
@timeout ||= Timeout.new(@timeout_s)
|
24
|
+
@timeout ||= Timeout.new(@timeout_seconds) # allow injecting a Timeout object from within tests
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
data/lib/poller/timeout.rb
CHANGED
@@ -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
|
-
# +
|
6
|
-
def initialize(
|
7
|
-
@period =
|
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
|
|
data/lib/poller/version.rb
CHANGED
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.
|
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-
|
12
|
+
date: 2013-03-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|