Class: GollyUtils::ChildProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/golly-utils/child_process.rb

Overview

Start, manage, and stop a child process.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ChildProcess

Returns a new instance of ChildProcess.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):



32
33
34
35
36
# File 'lib/golly-utils/child_process.rb', line 32

def initialize(options={})
  options= {env: {}, quiet: false, spawn_options: {}}.merge(options)
  options[:spawn_options][:in] ||= '/dev/null'
  options.each {|k,v| send "#{k}=", v}
end

Instance Attribute Details

#envHash

Environment variables to set in the child process.

Returns:



22
23
24
# File 'lib/golly-utils/child_process.rb', line 22

def env
  @env
end

#pidFixnum? (readonly)

The PID of the child process if running.

Returns:

  • (Fixnum, nil)


26
27
28
# File 'lib/golly-utils/child_process.rb', line 26

def pid
  @pid
end

#quietBoolean Also known as: quiet?

Whether to print startup/shutdown info to stdout, and whether or not to the stdout and stderr streams of the child process (unless explictly redirected via :spawn_options)

Returns:

  • (Boolean)


13
14
15
# File 'lib/golly-utils/child_process.rb', line 13

def quiet
  @quiet
end

#spawn_optionsHash

Options to pass to Process#spawn.

Returns:



18
19
20
# File 'lib/golly-utils/child_process.rb', line 18

def spawn_options
  @spawn_options
end

#start_commandString

The shell command to start the child process.

Returns:

  • (String)


8
9
10
# File 'lib/golly-utils/child_process.rb', line 8

def start_command
  @start_command
end

Instance Method Details

#alive?Boolean

Checks if the process [previously started by this class] is still alive.

If it is determined that the process is no longer alive then the internal PID is cleared.

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
# File 'lib/golly-utils/child_process.rb', line 86

def alive?
  return false if @pid.nil?
  alive= begin
      Process.getpgid(@pid) != -1
    rescue Errno::ESRCH
      false
    end
  @pid= nil unless alive
  alive
end

#shutdownBoolean

Stops the child process.

If it is already running, then this will do nothing.

Returns:

  • (Boolean)

    Boolean indicating whether shutdown was successful and process is down.



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/golly-utils/child_process.rb', line 68

def shutdown
  if alive?
    t= Thread.new{ attempt_kill }
    if quiet?
      t.join
    else
      puts "Stopping process #@pid..."
      t.join
    end
  end
  !alive?
end

#startupself

Starts the child process.

If it is already running, then this will do nothing.

Returns:

  • (self)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/golly-utils/child_process.rb', line 43

def startup
  unless alive?
    opt= self.spawn_options
    if quiet?
      opt= opt.dup
      mute= [:out,:err] - opt.keys.flatten
      mute.each {|fd| opt[fd] ||= '/dev/null'}
    end
    unless quiet?
      e= ''
      env.each{|k,v| e+="#{k}=#{v} "} if env
      puts "> #{e}#{start_command}"
    end
    @pid= spawn env, start_command, opt
    Process.detach @pid
    puts "< Spawned process #@pid" unless quiet?
  end
  self
end