Method: CommandExec::Command#run

Defined in:
lib/command_exec/command.rb

#runObject

Run the program

Raises:

Throw:

  • (:command_execution_failed)

    if an error occured and command_exec should throw an error (which you can catch) in the case of an error



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/command_exec/command.rb', line 265

def run
  process = CommandExec::Process.new(:lib_logger => @logger)
  process.log_file = @log_file if @log_file
  process.status = :success

  check_path

  process.start_time = Time.now

  case @run_via
  when :open3
    Open3::popen3(to_s, :chdir => @working_directory) do |stdin, stdout, stderr, wait_thr|
      process.stdout = stdout.readlines.map(&:chomp)
      process.stderr = stderr.readlines.map(&:chomp)
      process.pid = wait_thr.pid
      process.return_code = wait_thr.value.exitstatus
    end
  when :system
    Dir.chdir(@working_directory) do
      system(to_s)
      process.stdout = []
      process.stderr = []
      process.pid = $?.pid
      process.return_code = $?.exitstatus
    end
  else
    Open3::popen3(to_s, :chdir => @working_directory) do |stdin, stdout, stderr, wait_thr|
      process.stdout = stdout.readlines.map(&:chomp)
      process.stderr = stderr.readlines.map(&:chomp)
      process.pid = wait_thr.pid
      process.return_code = wait_thr.value.exitstatus
    end
  end

  process.end_time = Time.now

    if @error_detection_on.include?(:return_code)
      if not @error_indicators[:allowed_return_code].include? process.return_code or 
             @error_indicators[:forbidden_return_code].include? process.return_code

        @logger.debug "Error detection on return code found an error"
        process.status = :failed 
        process.reason_for_failure = :return_code
      end
    end

    if @error_detection_on.include?(:stderr) and not process.status == :failed
      if error_occured?( @error_indicators[:forbidden_words_in_stderr], @error_indicators[:allowed_words_in_stderr], process.stderr)
        @logger.debug "Error detection on stderr found an error"
        process.status = :failed 
        process.reason_for_failure = :stderr
      end
    end

    if @error_detection_on.include?(:stdout) and not process.status == :failed
      if error_occured?( @error_indicators[:forbidden_words_in_stdout], @error_indicators[:allowed_words_in_stdout], process.stdout)
        @logger.debug "Error detection on stdout found an error"
        process.status = :failed 
        process.reason_for_failure = :stdout
      end
    end

    if @error_detection_on.include?(:log_file) and not process.status == :failed
      if error_occured?( @error_indicators[:forbidden_words_in_log_file], @error_indicators[:allowed_words_in_log_file], process.log_file)
        @logger.debug "Error detection on log file found an error"
        process.status = :failed 
        process.reason_for_failure = :log_file
      end
    end

    @logger.debug "Result of command run #{process.status}"

  @result = process
  if process.status == :failed
    case @on_error_do
    when :nothing
      #nothing
    when :raise_error
      raise CommandExec::Exceptions::CommandExecutionFailed, "An error occured. Please check for reason via command.reason_for_failure and/or command.stdout, comand.stderr, command.log_file, command.return_code"
    when :throw_error
      throw :command_execution_failed 
    else
      #nothing
    end
  end
end