Class: Ladle::JRubyProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/ladle/jruby_process.rb

Overview

Implementations of platform-specific behaviors for JRuby.

This separate strategy is necessary because you can't Process.waitpid2 on the PID returned by JRuby's IO.popen4.

Instance Method Summary collapse

Constructor Details

#initialize(*command_and_args) ⇒ JRubyProcess

Create a new process for the given command and its args.



14
15
16
# File 'lib/ladle/jruby_process.rb', line 14

def initialize(*command_and_args)
  @command_and_args = command_and_args
end

Instance Method Details

#pidFixnum

Returns the PID for the process.

Returns:

  • (Fixnum)

    the PID for the process



61
62
63
# File 'lib/ladle/jruby_process.rb', line 61

def pid
  @pid ||= Java::OrgJrubyUtil::ShellLauncher.getPidFromProcess(@process)
end

#popen[IO, IO, IO]

Start the process and return pipes to its standard streams.

Returns:

  • ([IO, IO, IO])

    stdin, stdout, and stderr for the running process.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ladle/jruby_process.rb', line 22

def popen
  # You can't wait for the PID returned by JRuby's IO.popen4, so
  # this is necessary.
  cmd = @command_and_args.collect(&:to_s).to_java(:string)
  @process = Java::JavaLang::ProcessBuilder.new(
    cmd
  ).start

  [
    # java.util.Process flips the meanings of "in" and "out"
    # relative to popen3
    @process.output_stream.to_io,
    @process.input_stream.to_io,
    @process.error_stream.to_io
  ]
end

#stop_gracefully

This method returns an undefined value.

Send signal 15 to the process.



51
52
53
54
55
56
57
# File 'lib/ladle/jruby_process.rb', line 51

def stop_gracefully
  begin
    Process.kill 15, pid
  rescue Errno::ESRCH
    # already gone
  end
end

#waitFixnum

Wait for the process to finish.

Returns:

  • (Fixnum)

    the return status of the process.



43
44
45
# File 'lib/ladle/jruby_process.rb', line 43

def wait
  @process.waitFor
end