Class: Procession::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/procession/process.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Process

Returns a new instance of Process.



5
6
7
8
9
10
11
# File 'lib/procession/process.rb', line 5

def initialize(options)
  @command = options.delete(:command)
  @await = options.delete(:await)
  @working_dir = options.delete(:working_dir)
  @environment = options.delete(:environment)
  @inherit_output = options.delete(:inherit_output)
end

Instance Method Details

#startObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/procession/process.rb', line 13

def start
  args = @command.split(' ')
  @proc = ChildProcess.build(*args)

  setup_cwd
  setup_environment

  r, w = IO.pipe

  @proc.io.stdout = @proc.io.stderr = w
  @proc.leader = true
  @proc.start
  w.close

  all_output = ""

  begin
    started = false
    until started
      partial = r.readpartial(8192)
      puts partial if @inherit_output
      all_output << partial
      if (all_output =~ @await)
        started = true
      end
    end
  rescue EOFError
    raise ProcessExitedError.new "The app process exited\nSTDOUT:\n#{all_output}"
  end

  Thread.new do
    while true
      partial = r.readpartial(8192)
      puts partial if @inherit_output
    end
  end

  at_exit do
    @proc.stop
  end

  @proc.io.inherit!

  @proc
end