Class: EY::Serverside::Spawner::Child

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, shell, server = nil) ⇒ Child

Returns a new instance of Child.



102
103
104
105
106
107
# File 'lib/engineyard-serverside/spawner.rb', line 102

def initialize(command, shell, server = nil)
  @command = command
  @shell = shell
  @server = server
  @output = ""
end

Instance Attribute Details

#stderr_fdObject (readonly)

Returns the value of attribute stderr_fd.



100
101
102
# File 'lib/engineyard-serverside/spawner.rb', line 100

def stderr_fd
  @stderr_fd
end

#stdout_fdObject (readonly)

Returns the value of attribute stdout_fd.



100
101
102
# File 'lib/engineyard-serverside/spawner.rb', line 100

def stdout_fd
  @stdout_fd
end

Instance Method Details

#append_to_buffer(fd, data) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/engineyard-serverside/spawner.rb', line 161

def append_to_buffer(fd,data)
  case fd
  when @stdout_fd
    @shell.command_out data
    @output << data
  when @stderr_fd
    @shell.command_err data
    @output << data
  end
end

#close(fd) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/engineyard-serverside/spawner.rb', line 153

def close(fd)
  case fd
  when @stdout_fd then @stdout_fd = nil
  when @stderr_fd then @stderr_fd = nil
  end
  fd.close rescue true
end

#finished(status) ⇒ Object



141
142
143
# File 'lib/engineyard-serverside/spawner.rb', line 141

def finished(status)
  @status = status
end

#iosObject



137
138
139
# File 'lib/engineyard-serverside/spawner.rb', line 137

def ios
  [@stdout_fd, @stderr_fd].compact
end

#resultObject



145
146
147
148
149
150
151
# File 'lib/engineyard-serverside/spawner.rb', line 145

def result
  if @status
    Result.new(@command, @status.success?, @output, @server)
  else
    raise "No result from unfinished child process"
  end
end

#spawnObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/engineyard-serverside/spawner.rb', line 109

def spawn
  @shell.command_show @command
  #stdin, @stdout_fd, @stderr_fd, @waitthr = Open3.popen3(@cmd)
  #stdin.close

  stdin_rd, stdin_wr = IO.pipe
  @stdout_fd, stdout_wr = IO.pipe
  @stderr_fd, stderr_wr = IO.pipe

  @pid = fork do
    stdin_wr.close
    @stdout_fd.close
    @stderr_fd.close
    STDIN.reopen(stdin_rd)
    STDOUT.reopen(stdout_wr)
    STDERR.reopen(stderr_wr)
    Kernel.exec(@command)
    raise "Exec failed!"
  end

  stdin_rd.close
  stdin_wr.close
  stdout_wr.close
  stderr_wr.close

  [@pid, @stdout_fd, @stderr_fd]
end