Class: ExecApp

Inherits:
Object
  • Object
show all
Defined in:
omf_common/lib/omf_common/exec_app.rb

Overview

Run an application on the client.

Borrows from Open3

Constant Summary

@@all_apps =

Holds the pids for all active apps

Hash.new

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ExecApp) initialize(id, cmd, map_std_err_to_out = false, working_directory = nil, &observer)

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”)“



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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'omf_common/lib/omf_common/exec_app.rb', line 88

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

  logger.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|
    ret = Process.waitpid(pid)
    @exit_status = $?.exitstatus
    if @exit_status > 127 
      @exit_status = 128 - @exit_status
    end
    @@all_apps.delete(@id)
    # app finished
    if (@exit_status == 0) || @clean_exit
      logger.debug "Application '#{@id}' finished"
    else
      
      logger.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 }
    if (@exit_status == 0) || @clean_exit
      s = "OK"
    else
      s = "ERROR"
    end
    #@observer.call("DONE.#{s}", @id, "status: #{@exit_status}")
    call_observer("DONE.#{s}", "status: #{@exit_status}")

  end
  logger.debug "Application is running with PID #{@pid}"
end

Instance Attribute Details

- (Object) clean_exit (readonly)

Returns the value of attribute clean_exit



57
58
59
# File 'omf_common/lib/omf_common/exec_app.rb', line 57

def clean_exit
  @clean_exit
end

- (Object) pid (readonly)

Returns the value of attribute pid



57
58
59
# File 'omf_common/lib/omf_common/exec_app.rb', line 57

def pid
  @pid
end

Class Method Details

+ (Object) [](id)

Return an application instance based on its ID



47
48
49
50
51
# File 'omf_common/lib/omf_common/exec_app.rb', line 47

def self.[](id)
  app = @@all_apps[id]
  logger.info "Unknown application '#{id}/#{id.class}'" if app.nil?
  return app
end

+ (Object) signal_all(signal = 'KILL')



53
54
55
# File 'omf_common/lib/omf_common/exec_app.rb', line 53

def self.signal_all(signal = 'KILL')
  @@all_apps.each_value { |app| app.signal(signal) }
end

Instance Method Details

- (Object) call_observer(event_type, msg) (private)



196
197
198
199
200
201
202
203
204
# File 'omf_common/lib/omf_common/exec_app.rb', line 196

def call_observer(event_type, msg)
  return unless @observer
  begin
    @observer.call(event_type, @id, msg)
  rescue Exception => ex
    logger.warn "Exception while calling observer '#{@observer}': #{ex}"
    logger.debug "#{ex}\n\t#{ex.backtrace.join("\n\t")}"
  end
end

- (Object) monitor_pipe(name, pipe) (private)

Create a thread to monitor the process and its output and report that back to the server



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'omf_common/lib/omf_common/exec_app.rb', line 178

def monitor_pipe(name, pipe)
  @threads << Thread.new() do
    begin
      while true do
        s = pipe.readline.chomp
        call_observer(name.to_s.upcase, s)
      end
    rescue EOFError
      # do nothing
    rescue  => err
      logger.error "monitorApp(#{@id}): #{err}"
      logger.debug "#{err}\n\t#{err.backtrace.join("\n\t")}"
    ensure
      pipe.close
    end
  end
end

- (Object) signal(signal = 'KILL')



72
73
74
75
# File 'omf_common/lib/omf_common/exec_app.rb', line 72

def signal(signal = 'KILL')
  @clean_exit = true
  Process.kill(signal, -1 * @pid) # we are sending to the entire process group
end

- (Object) stdin(line)



66
67
68
69
70
# File 'omf_common/lib/omf_common/exec_app.rb', line 66

def stdin(line)
  logger.debug "Writing '#{line}' to app '#{@id}'"
  @stdin.write("#{line}\n")
  @stdin.flush
end