module Hydra::Derivatives::Processors::ShellBasedProcessor::ClassMethods

Public Instance Methods

all_eof?(files) click to toggle source
# File lib/hydra/derivatives/processors/shell_based_processor.rb, line 97
def all_eof?(files)
  files.find { |f| !f.eof }.nil?
end
execute(command) click to toggle source
# File lib/hydra/derivatives/processors/shell_based_processor.rb, line 42
def execute(command)
  context = {}
  if timeout
    execute_with_timeout(timeout, command, context)
  else
    execute_without_timeout(command, context)
  end
end
execute_with_timeout(timeout, command, context) click to toggle source
# File lib/hydra/derivatives/processors/shell_based_processor.rb, line 51
def execute_with_timeout(timeout, command, context)
  Timeout.timeout(timeout) do
    execute_without_timeout(command, context)
  end
rescue Timeout::Error
  pid = context[:pid]
  Process.kill("KILL", pid)
  raise Hydra::Derivatives::TimeoutError, "Unable to execute command \"#{command}\"\nThe command took longer than #{timeout} seconds to execute"
end
execute_without_timeout(command, context) click to toggle source
# File lib/hydra/derivatives/processors/shell_based_processor.rb, line 61
def execute_without_timeout(command, context)
  err_str = ''
  stdin, stdout, stderr, wait_thr = popen3(command)
  context[:pid] = wait_thr[:pid]
  files = [stderr, stdout]
  stdin.close

  until all_eof?(files)
    ready = IO.select(files, nil, nil, 60)

    next unless ready
    readable = ready[0]
    readable.each do |f|
      fileno = f.fileno

      begin
        data = f.read_nonblock(BLOCK_SIZE)

        case fileno
        when stderr.fileno
          err_str << data
        end
      rescue EOFError
        Hydra::Derivatives::Logger.debug "Caught an eof error in ShellBasedProcessor"
        # No big deal.
      end
    end
  end

  stdout.close
  stderr.close
  exit_status = wait_thr.value

  raise "Unable to execute command \"#{command}\". Exit code: #{exit_status}\nError message: #{err_str}" unless exit_status.success?
end