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
-
#fork_remote_tasks(parents) ⇒ Object
private
Launch remote tasks recursively.
-
#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
#initialize(hosts = []) ⇒ Graph
Returns a new instance of Graph.
6 7 8 9 10 11 12 13 14 |
# File 'lib/grifork/graph.rb', line 6 def initialize(hosts = []) @root = Node.new('localhost') @depth = @root.level @nodes = 1 @acceptable_nodes = [@root] hosts.each do |host| self.add_node_by_host(host) end end |
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 |
#fork_remote_tasks(parents) ⇒ Object (private)
Launch remote tasks recursively
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/grifork/graph.rb', line 78 def fork_remote_tasks(parents) families = [] next_generation = [] parents.each do |parent| if parent.children.size.zero? logger.debug("#{parent} Reached leaf. Nothing to do.") next end parent.children.each do |child| families << [parent, child] next_generation << child end end if families.size.zero? logger.info("Reached bottom of the tree. Nothing to do.") return end Parallel.map(families, config.parallel => families.size) do |family| parent = family[0] child = family[1] logger.info("Run remote [#{parent.level}]. #{parent.host} => #{child.host}") config.remote_task.run(parent.host, child.host) end fork_remote_tasks(next_generation) end |
#grifork ⇒ Object
Run grifork command on child nodes recursively
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/grifork/graph.rb', line 46 def grifork config.prepare_task.run if config.prepare_task if root.children.size.zero? logger.debug("#{root} Reached leaf. Nothing to do.") return end Parallel.map(root.children, config.parallel => root.children.size) do |child| logger.info("Run locally. localhost => #{child.host}") config.local_task.run(root.host, child.host) if child.children.size.zero? logger.debug("#{child} Reached leaf. Nothing to do.") next end Grifork::Executor::Grifork.new.run(child) end config.finish_task.run if config.finish_task end |
#launch_tasks ⇒ Object
Launch local and remote tasks through whole graph
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/grifork/graph.rb', line 31 def launch_tasks config.prepare_task.run if config.prepare_task # level = 1 Parallel.map(root.children, config.parallel => 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) config.finish_task.run if config.finish_task end |
#print(node = root) ⇒ Object
Print graph structure for debug usage
67 68 69 70 71 72 73 |
# File 'lib/grifork/graph.rb', line 67 def print(node = root) puts %( ) * node.level + "#{node}" node.children.each do |child| print(child) end true end |