Class: EY::Serverside::Spawner

Inherits:
Object
  • Object
show all
Defined in:
lib/engineyard-serverside/spawner.rb

Defined Under Namespace

Classes: Child, Result

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpawner

Returns a new instance of Spawner.



13
14
15
16
# File 'lib/engineyard-serverside/spawner.rb', line 13

def initialize
  @poll_period = 0.5
  @children = []
end

Class Method Details

.run(cmd, shell, server = nil) ⇒ Object



7
8
9
10
11
# File 'lib/engineyard-serverside/spawner.rb', line 7

def self.run(cmd, shell, server = nil)
  s = new
  s.add(cmd, shell, server)
  s.run.first
end

Instance Method Details

#add(cmd, shell, server = nil) ⇒ Object



18
19
20
# File 'lib/engineyard-serverside/spawner.rb', line 18

def add(cmd, shell, server = nil)
  @children << Child.new(cmd, shell, server)
end

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/engineyard-serverside/spawner.rb', line 22

def run
  @child_by_fd = {}
  @child_by_pid = {}

  @children.each do |child|
    pid, stdout_fd, stderr_fd = child.spawn
    @child_by_pid[pid] = child
    @child_by_fd[stdout_fd] = child
    @child_by_fd[stderr_fd] = child
  end

  while @child_by_pid.any?
    process
    wait
  end

  @children.map { |child| child.result }
end