Class: Grifork::Graph

Inherits:
Object
  • Object
show all
Includes:
Configured, Loggable
Defined in:
lib/grifork/graph.rb

Defined Under Namespace

Classes: Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#logger

Methods included from Configured

#config

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

#depthObject (readonly)

Returns the value of attribute depth.



4
5
6
# File 'lib/grifork/graph.rb', line 4

def depth
  @depth
end

#nodesObject (readonly)

Returns the value of attribute nodes.



4
5
6
# File 'lib/grifork/graph.rb', line 4

def nodes
  @nodes
end

#rootObject (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

#griforkObject

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_tasksObject

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