Class: Gofer::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/gofer/cluster.rb

Overview

A collection of Gofer::Host instances that can run commands simultaneously

Gofer::Cluster supports most of the methods of Gofer::Host. Commands will be run simultaneously, with up to max_concurrency commands running at the same time. If max_concurrency is unset all hosts in the cluster will receive commands at the same time.

Results from commands run are returned in a Hash, keyed by host.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parties = [], opts = {}) ⇒ Cluster

Create a new cluster of Gofer::Host connections.

parties

Gofer::Host or other Gofer::Cluster instances

Options:

max_concurrency

Maximum number of commands to run simultaneously


27
28
29
30
31
32
# File 'lib/gofer/cluster.rb', line 27

def initialize(parties=[], opts={})
  @hosts = []
  @max_concurrency = opts.delete(:max_concurrency)

  parties.each { |i| self << i }
end

Instance Attribute Details

#hostsObject (readonly)

Hosts in this cluster


15
16
17
# File 'lib/gofer/cluster.rb', line 15

def hosts
  @hosts
end

#max_concurrencyObject

Maximum number of commands to run simultaneously


18
19
20
# File 'lib/gofer/cluster.rb', line 18

def max_concurrency
  @max_concurrency
end

Instance Method Details

#<<(other) ⇒ Object

Add a Gofer::Host or the hosts belonging to a Gofer::Cluster to this instance.


41
42
43
44
45
46
47
48
# File 'lib/gofer/cluster.rb', line 41

def <<(other)
  case other
  when Cluster
    other.hosts.each { |host| self << host }
  when Host
    @hosts << other
  end
end

#concurrencyObject

Currency effective concurrency, either max_concurrency or the number of Gofer::Host instances we contain.


36
37
38
# File 'lib/gofer/cluster.rb', line 36

def concurrency
  max_concurrency.nil? ? hosts.length : [max_concurrency, hosts.length].min
end

#directory?(*args) ⇒ Boolean

Check if a path is a directory on each host in the cluster. See Gofer::Host#directory?

Returns:

  • (Boolean)

61
62
63
# File 'lib/gofer/cluster.rb', line 61

def directory? *args
  threaded(:directory?, *args)
end

#exist?(*args) ⇒ Boolean

Check if a path exists on each host in the cluster. See Gofer::Host#exist?

Returns:

  • (Boolean)

56
57
58
# File 'lib/gofer/cluster.rb', line 56

def exist? *args
  threaded(:exist?, *args)
end

#ls(*args) ⇒ Object

List a directory on each host in the cluster. See Gofer::Host#ls


66
67
68
# File 'lib/gofer/cluster.rb', line 66

def ls *args
  threaded(:ls, *args)
end

#read(*args) ⇒ Object

Read a file on each host in the cluster. See Gofer::Host#read


76
77
78
# File 'lib/gofer/cluster.rb', line 76

def read *args
  threaded(:read, *args)
end

#run(*args) ⇒ Object

Run a command on this Gofer::Cluster. See Gofer::Host#run


51
52
53
# File 'lib/gofer/cluster.rb', line 51

def run *args
  threaded(:run, *args)
end

#upload(*args) ⇒ Object

Upload to each host in the cluster. See Gofer::Host#ls


71
72
73
# File 'lib/gofer/cluster.rb', line 71

def upload *args
  threaded(:upload, *args)
end

#write(*args) ⇒ Object

Write a file to each host in the cluster. See Gofer::Host#write


81
82
83
# File 'lib/gofer/cluster.rb', line 81

def write *args
  threaded(:write, *args)
end