Class: OMF::Base::ExecApp
Overview
Run an application on the client.
Borrows from Open3
Constant Summary collapse
- @@all_apps =
Holds the pids for all active apps
Hash.new
Instance Attribute Summary collapse
-
#clean_exit ⇒ Object
readonly
Returns the value of attribute clean_exit.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
Class Method Summary collapse
-
.[](id) ⇒ Object
Return an application instance based on its ID.
- .signal_all(signal = 'KILL') ⇒ Object
Instance Method Summary collapse
-
#initialize(id, cmd, map_std_err_to_out = false, working_directory = nil, &observer) ⇒ ExecApp
constructor
Run an application ‘cmd’ in a separate thread and monitor its stdout.
- #signal(signal = 'KILL') ⇒ Object
- #stdin(line) ⇒ Object
Methods included from Loggable
#_logger, #debug, #error, #fatal, #info, init_log, logger, set_environment, #warn
Constructor Details
#initialize(id, cmd, map_std_err_to_out = false, working_directory = nil, &observer) ⇒ ExecApp
Run an application ‘cmd’ in a separate thread and monitor its stdout. Also send status reports to the ‘observer’ by calling its “call(eventType, appId, message”)“
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 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 136 137 138 139 140 141 142 |
# File 'lib/omf_base/exec_app.rb', line 70 def initialize(id, cmd, map_std_err_to_out = false, working_directory = nil, &observer) @id = id || self.object_id @observer = observer @@all_apps[@id] = self @exit_status = nil @threads = [] pw = IO::pipe # pipe[0] for read, pipe[1] for write pr = IO::pipe pe = IO::pipe debug "Starting application '#{@id}' - cmd: '#{cmd}'" #@observer.call(:STARTED, id, cmd) call_observer(:STARTED, cmd) @pid = fork { # child will remap pipes to std and exec cmd pw[1].close STDIN.reopen(pw[0]) pw[0].close pr[0].close STDOUT.reopen(pr[1]) pr[1].close pe[0].close STDERR.reopen(pe[1]) pe[1].close begin pgid = Process.setsid # Create a new process group # which includes all potential child processes STDOUT.puts "INTERNAL WARNING: Assuming process_group_id == pid" unless pgid == $$ Dir.chdir working_directory if working_directory exec(cmd) rescue => ex cmd = cmd.join(' ') if cmd.kind_of?(Array) STDERR.puts "exec failed for '#{cmd}' (#{$!}): #{ex}" end # Should never get here exit! } pw[0].close pr[1].close pe[1].close monitor_pipe(:stdout, pr[0]) monitor_pipe(map_std_err_to_out ? :stdout : :stderr, pe[0]) # Create thread which waits for application to exit @threads << Thread.new(id, @pid) do |id, pid| Process.waitpid(pid) # Exit status is sometimes nil (OSX 10.8, ping) @exit_status = $?.exitstatus || 0 if @exit_status > 127 @exit_status = 128 - @exit_status end @@all_apps.delete(@id) # app finished if (@exit_status == 0) || @clean_exit debug "Application '#{@id}' finished" else debug "Application '#{@id}' failed (code=#{@exit_status})" end end @stdin = pw[1] # wait for done in yet another thread Thread.new do @threads.each {|t| t.join } call_observer("EXIT", @exit_status) end debug "Application is running with PID #{@pid}" end |
Instance Attribute Details
#clean_exit ⇒ Object (readonly)
Returns the value of attribute clean_exit.
39 40 41 |
# File 'lib/omf_base/exec_app.rb', line 39 def clean_exit @clean_exit end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
39 40 41 |
# File 'lib/omf_base/exec_app.rb', line 39 def pid @pid end |
Class Method Details
.[](id) ⇒ Object
Return an application instance based on its ID
29 30 31 32 33 |
# File 'lib/omf_base/exec_app.rb', line 29 def self.[](id) app = @@all_apps[id] info "Unknown application '#{id}/#{id.class}'" if app.nil? return app end |
.signal_all(signal = 'KILL') ⇒ Object
35 36 37 |
# File 'lib/omf_base/exec_app.rb', line 35 def self.signal_all(signal = 'KILL') @@all_apps.each_value { |app| app.signal(signal) } end |
Instance Method Details
#signal(signal = 'KILL') ⇒ Object
54 55 56 57 58 |
# File 'lib/omf_base/exec_app.rb', line 54 def signal(signal = 'KILL') debug "Sending signal '#{signal}' to app '#{@id}' with pid #{@pid}" @clean_exit = true Process.kill(signal, -1 * @pid) # we are sending to the entire process group end |
#stdin(line) ⇒ Object
48 49 50 51 52 |
# File 'lib/omf_base/exec_app.rb', line 48 def stdin(line) debug "Writing '#{line}' to app '#{@id}'" @stdin.write("#{line}\n") @stdin.flush end |