Class: RFacter::Factset
- Inherits:
-
Object
- Object
- RFacter::Factset
- Extended by:
- Forwardable
- Defined in:
- lib/rfacter/factset.rb
Overview
A class that can retrieve facts from several nodes
A factset joins instances of Node to Util::Collection and enables parallel and asynchronous resolution of fact values across several nodes. Supports retrieving single facts asynchronously via #fetch and in a blocking fashion via #value. All facts can be retrieved asynchronously via #fetch_all and in a blocking fashion via #to_hash.
Instance Method Summary collapse
-
#fetch(queries) ⇒ Concurrent::Future{String => Hash}
Asynchronously fetch the value of a fact from each node.
-
#fetch_all ⇒ Concurrent::Future{String => Hash}
Asynchronously fetch all facts from each node.
-
#flush ⇒ void
Flush cached fact values.
-
#initialize(nodes, config: RFacter::Config.config, **opts) ⇒ Factset
constructor
Returns a new instance of Factset.
-
#to_hash ⇒ Hash{String => Hash}
Fetch all facts from each node.
-
#value(queries) ⇒ Hash{String => Hash}
Fetch the value of a fact from each node.
Constructor Details
#initialize(nodes, config: RFacter::Config.config, **opts) ⇒ Factset
Returns a new instance of Factset
28 29 30 31 32 33 34 |
# File 'lib/rfacter/factset.rb', line 28 def initialize(nodes, config: RFacter::Config.config, **opts) @config = config @collections = nodes.each_with_object({}) do |node, hash| hash[node.id] = RFacter::Util::Collection.new(node) end end |
Instance Method Details
#fetch(queries) ⇒ Concurrent::Future{String => Hash}
Asynchronously fetch the value of a fact from each node
This method spawns a background thread per node which resolves the value of a fact specified by ‘query`.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rfacter/factset.rb', line 46 def fetch(queries) queries = Array(queries) # Spawn async lookups in the background for each node. futures = @collections.each_with_object({}) do |(name, collection), hash| hash[name] = {} queries.each do |query| hash[name][query] = collection.async.value(query) end end # Return a future with the resolved values. Concurrent::Future.execute do futures.each_with_object({}) do |(name, ivars), hash| hash[name] = ivars.each_with_object({}) do |(query, ivar), results| # TODO: Add exception handling for failed futures. results[query] = ivar.value end end end end |
#fetch_all ⇒ Concurrent::Future{String => Hash}
Asynchronously fetch all facts from each node
This method spawns a background thread per node which resolves all fact values for each node.
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rfacter/factset.rb', line 85 def fetch_all futures = @collections.each_with_object({}) do |(name, collection), hash| collection.async.load_all hash[name] = collection.async.to_hash end Concurrent::Future.execute do futures.each_with_object({}) do |(name, future), hash| # TODO: Add exception handling for failed futures. hash[name] = future.value end end end |
#flush ⇒ void
This method returns an undefined value.
Flush cached fact values
113 114 115 |
# File 'lib/rfacter/factset.rb', line 113 def flush @collections.values.each {|c| c.flush} end |
#to_hash ⇒ Hash{String => Hash}
Fetch all facts from each node
This method calls #fetch_all and then blocks until the result is available.
106 107 108 |
# File 'lib/rfacter/factset.rb', line 106 def to_hash fetch_all.value end |
#value(queries) ⇒ Hash{String => Hash}
Fetch the value of a fact from each node
This method calls #fetch and then blocks until the result is available.
73 74 75 |
# File 'lib/rfacter/factset.rb', line 73 def value(queries) fetch(queries).value end |