Class: RFacter::Factset

Inherits:
Object
  • Object
show all
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.

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(nodes, config: RFacter::Config.config, **opts) ⇒ Factset

Returns a new instance of Factset

Parameters:

  • nodes (Array<RFacter::Node>)

    An array of node objects to collect facts from.

Since:

  • 0.1.0



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`.

Parameters:

  • queries (Array<String>)

    The names of the facts to fetch.

Returns:

  • (Concurrent::Future{String => Hash})

    A future that will return a hash mapping the node id to a hash containing the resolved facts when ‘value` is called.

Since:

  • 0.1.0



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_allConcurrent::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.

Returns:

  • (Concurrent::Future{String => Hash})

    A future that will return a hash mapping the node id to a hash containing the resolved facts when ‘value` is called.

Since:

  • 0.1.0



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

#flushvoid

This method returns an undefined value.

Flush cached fact values

Since:

  • 0.1.0



113
114
115
# File 'lib/rfacter/factset.rb', line 113

def flush
  @collections.values.each {|c| c.flush}
end

#to_hashHash{String => Hash}

Fetch all facts from each node

This method calls #fetch_all and then blocks until the result is available.

Returns:

  • (Hash{String => Hash})

    A hash mapping the node id to a hash containing the resolved facts.

Since:

  • 0.1.0



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.

Returns:

  • (Hash{String => Hash})

    A hash mapping the node id to a hash containing the resolved facts.

Since:

  • 0.1.0



73
74
75
# File 'lib/rfacter/factset.rb', line 73

def value(queries)
  fetch(queries).value
end