okcomputer 1.13.0 → 1.14.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 37e364f1ba01c9b0870d4ffbcd50eda067955be8
4
- data.tar.gz: e48b355fab4c30d2d93b6e7522cfe04fa9fd8896
3
+ metadata.gz: 772de60494a5720e23c186c4fc990e17e4457391
4
+ data.tar.gz: 70f52075d802e604aa84580aee2a50b2eb563060
5
5
  SHA512:
6
- metadata.gz: 926f7f507821ba687d71819408ed602449ec719c76ff15465cbef8144d7c8069d89dc9afced614c530339e99e4c49d46bb112b45d68ab0c4cbba1825bb6c3999
7
- data.tar.gz: 06f15affbb329a66e9ce414c6deb67aa697b0454ce8f4e61535bcc07078f5c7e100588b96a60abff0145613af2d154ad86510b11461daabc875c9b1993ba9deb
6
+ metadata.gz: 8996e1acb30679e1ec535a82415bbdf05560d3823fc6dfc59d62a6367c481da6e8965d46cc37d8dfb3647d8eb79c6694f13451da65211b98ab7f621e9de60e83
7
+ data.tar.gz: 3708ef0ded13c99216a1d00befcba8e3e33011526df7fd3030d4e4bb71544a9511310fbade738bf507ec1ff44a28dc498f45c46c99c493c0614a4d0d7eff7c73
@@ -42,7 +42,7 @@ module OkComputer
42
42
  rescue SocketError => e
43
43
  addl_message = "connection to #{host} on port #{port} failed with '#{e.message}': "
44
44
  raise ConnectionFailed, addl_message + e.message
45
- rescue TimeoutError => e
45
+ rescue Timeout::Error => e
46
46
  addl_message = "#{host} did not respond on port #{port} within #{request_timeout} seconds: "
47
47
  raise ConnectionFailed, addl_message + e.message
48
48
  rescue => e
@@ -45,6 +45,14 @@ module OkComputer
45
45
  {registrant_name => {:message => message, :success => success?, :time => time}}.to_json
46
46
  end
47
47
 
48
+ def <=>(check)
49
+ if check.is_a?(CheckCollection)
50
+ -1
51
+ else
52
+ registrant_name <=> check.registrant_name
53
+ end
54
+ end
55
+
48
56
  # Public: Whether the check passed
49
57
  #
50
58
  # Returns a boolean
@@ -1,32 +1,88 @@
1
1
  module OkComputer
2
2
  class CheckCollection
3
- attr_accessor :registry
3
+ attr_accessor :collection, :registrant_name, :display
4
4
 
5
5
  # Public: Initialize a new CheckCollection
6
6
  #
7
- # registry - a Hash of checks, with keys being unique names and values
8
- # being Check instances
9
- def initialize(registry={})
10
- self.registry = registry
7
+ # display - the display name for the Check Collection
8
+ def initialize(display)
9
+ self.display = display
10
+ self.collection = {}
11
11
  end
12
12
 
13
- # Public: Run the registry's checks
13
+ # Public: Run the collection's checks
14
14
  def run
15
15
  OkComputer.check_in_parallel ? check_in_parallel : check_in_sequence
16
16
  end
17
17
 
18
+ # Public: Returns a check or collection if it's in the check collection
19
+ #
20
+ # key - a check or collection name
21
+ # throws a KeyError when the key is not found
22
+ def fetch(key, default=nil)
23
+ found_in = self_and_sub_collections.detect{ |c| c[key] }
24
+ raise KeyError unless found_in
25
+ found_in[key]
26
+ end
27
+
28
+ # Public: Returns a check or collection if it's in the check collection
29
+ #
30
+ # key - a check or collection name
31
+ def [](key)
32
+ fetch(key)
33
+ rescue KeyError
34
+ end
35
+
18
36
  # Public: The list of checks in the collection
19
37
  #
20
- # Returns an Array of the registry's values
38
+ # Returns an Array of the collection's values
21
39
  def checks
22
- registry.values
40
+ collection.values
41
+ end
42
+
43
+ def <=>(check)
44
+ if check.is_a?(CheckCollection)
45
+ registrant_name <=> check.registrant_name
46
+ else
47
+ 1
48
+ end
49
+ end
50
+
51
+ alias_method :values, :checks
52
+
53
+ def check_names
54
+ collection.keys
55
+ end
56
+
57
+ alias_method :keys, :check_names
58
+
59
+ def sub_collections
60
+ checks.select{ |c| c.is_a?(CheckCollection)}
61
+ end
62
+
63
+ def self_and_sub_collections
64
+ [collection] + sub_collections
65
+ end
66
+
67
+ # Public: Registers a check into the collection
68
+ #
69
+ # Returns the check
70
+ def register(name, check)
71
+ collection[name] = check
72
+ end
73
+
74
+ # Public: Deregisters a check from the collection
75
+ #
76
+ # Returns the check
77
+ def deregister(name)
78
+ check = collection.delete(name)
23
79
  end
24
80
 
25
81
  # Public: The text of each check in the collection
26
82
  #
27
83
  # Returns a String
28
84
  def to_text
29
- checks.map(&:to_text).join("\n")
85
+ "#{display}\n#{checks.sort.map{ |c| "#{"\s\s" unless c.is_a?(CheckCollection)}#{c.to_text}"}.join("\n")}"
30
86
  end
31
87
 
32
88
  # Public: The JSON of each check in the collection
@@ -9,43 +9,58 @@ module OkComputer
9
9
  # check_name - The name of the check to retrieve
10
10
  #
11
11
  # Returns the registered check or raises Registry::CheckNotFound
12
- def self.fetch(check_name)
13
- registry.fetch(check_name)
12
+ def self.fetch(name)
13
+ default_collection.fetch(name)
14
14
  rescue KeyError
15
15
  raise CheckNotFound, "No matching check"
16
16
  end
17
17
 
18
18
  # Public: Return an object containing all the registered checks
19
19
  #
20
- # Returns a CheckCollection instance
20
+ # Returns the default_collection CheckCollection instance
21
21
  def self.all
22
- CheckCollection.new registry
22
+ default_collection
23
+ end
24
+
25
+ # Private: The list of registered checks, keyed by their unique names
26
+ #
27
+ # Returns a Hash
28
+ singleton_class.send(:alias_method, :registry, :all)
29
+
30
+ # Public: The default collection of checks
31
+ #
32
+ # Returns @default_collection
33
+ def self.default_collection
34
+ @default_collection ||= CheckCollection.new('Default Collection')
23
35
  end
24
36
 
25
37
  # Public: Register the given check with OkComputer
26
38
  #
27
39
  # check_name - The name of the check to retrieve
28
40
  # check_object - Instance of Checker to register
29
- def self.register(check_name, check_object)
41
+ # collection_name - The name of the check collection the check should be registered to
42
+ def self.register(check_name, check_object, collection_name=nil)
30
43
  check_object.registrant_name = check_name
31
- registry[check_name] = check_object
44
+ find_collection(collection_name).register(check_name, check_object)
32
45
  end
33
46
 
34
47
  # Public: Remove the check of the given name being checked
35
48
  #
36
49
  # check_name - The name of the check to retrieve
37
- def self.deregister(check_name)
38
- registry.delete(check_name)
50
+ # collection_name - The name of the check collection the check should be deregistered from
51
+ def self.deregister(check_name, collection_name=nil)
52
+ find_collection(collection_name).deregister(check_name)
39
53
  end
40
54
 
41
- # Private: The list of registered checks, keyed by their unique names
42
- #
43
- # Returns a Hash
44
- def self.registry
45
- @registry ||= {}
55
+ private_class_method def self.find_collection(collection_name=nil)
56
+ collection_name ? default_collection.fetch(collection_name) : default_collection
57
+ rescue KeyError
58
+ raise CollectionNotFound
46
59
  end
47
60
 
61
+
48
62
  # used when fetching a check that has not been registered
49
63
  CheckNotFound = Class.new(StandardError)
64
+ CollectionNotFound = Class.new(StandardError)
50
65
  end
51
66
  end
@@ -1,3 +1,3 @@
1
1
  module OkComputer
2
- VERSION = '1.13.0'
2
+ VERSION = '1.14.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: okcomputer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.13.0
4
+ version: 1.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Byrne
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-10-24 00:00:00.000000000 Z
13
+ date: 2017-01-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sqlite3
@@ -124,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
124
  version: '0'
125
125
  requirements: []
126
126
  rubyforge_project:
127
- rubygems_version: 2.5.1
127
+ rubygems_version: 2.4.8
128
128
  signing_key:
129
129
  specification_version: 4
130
130
  summary: A simple, extensible health-check monitor