Class: Grifork::Graph
- Inherits:
-
Object
- Object
- Grifork::Graph
- Includes:
- Configured, Loggable
- Defined in:
- lib/grifork/graph.rb
Defined Under Namespace
Classes: Node
Instance Attribute Summary collapse
-
#depth ⇒ Object
readonly
Returns the value of attribute depth.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #add_node_by_host(host) ⇒ Object
-
#grifork ⇒ Object
Run grifork command on child nodes recursively.
-
#initialize(hosts = []) ⇒ Graph
constructor
A new instance of Graph.
-
#launch_tasks ⇒ Object
Launch local and remote tasks through whole graph.
-
#print(node = root) ⇒ Object
Print graph structure for debug usage.
Methods included from Loggable
Methods included from Configured
Constructor Details
Instance Attribute Details
#depth ⇒ Object (readonly)
Returns the value of attribute depth.
4 5 6 |
# File 'lib/grifork/graph.rb', line 4 def depth @depth end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
4 5 6 |
# File 'lib/grifork/graph.rb', line 4 def nodes @nodes end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
4 5 6 |
# File 'lib/grifork/graph.rb', line 4 def root @root end |
Instance Method Details
#add_node_by_host(host) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/grifork/graph.rb', line 16 def add_node_by_host(host) parent = @acceptable_nodes.first node = Node.new(host, parent: parent) parent.add_child(node) unless parent.acceptable? @acceptable_nodes.shift end @last = node @depth = node.level @nodes += 1 @acceptable_nodes << node parent end |
#grifork ⇒ Object
Run grifork command on child nodes recursively
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/grifork/graph.rb', line 42 def grifork if root.children.size.zero? logger.debug("#{root} Reached leaf. Nothing to do.") return end Parallel.map(root.children, in_processes: root.children.size) do |child| logger.info("Run locally. localhost => #{child.host}") config.local_task.run(root.host, child.host) Grifork::Executor::Grifork.new(config.grifork).run(child) end end |
#launch_tasks ⇒ Object
Launch local and remote tasks through whole graph
31 32 33 34 35 36 37 38 39 |
# File 'lib/grifork/graph.rb', line 31 def launch_tasks # level = 1 Parallel.map(root.children, in_threads: root.children.size) do |node| logger.info("Run locally. localhost => #{node.host}") config.local_task.run(root.host, node.host) end # level in (2..depth) fork_remote_tasks(root.children) end |
#print(node = root) ⇒ Object
Print graph structure for debug usage
55 56 57 58 59 60 61 |
# File 'lib/grifork/graph.rb', line 55 def print(node = root) puts %( ) * node.level + "#{node}" node.children.each do |child| print(child) end true end |