Skip to content

Commit

Permalink
Merge pull request #556 from IU-Libraries-Joint-Development/essi-1838…
Browse files Browse the repository at this point in the history
…_wait_readable

Essi-1838 Handle IO::WaitReadable errors
  • Loading branch information
aploshay authored Jul 13, 2023
2 parents ce12658 + f5710b9 commit e6ab0ae
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/extensions/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,6 @@ def attribute_will_change!(attr)

# read pre-supplied file characterization, if present
Hydra::Works::CharacterizationService.prepend Extensions::Hydra::Works::CharacterizationService::Precharacterization

# Patch ShellBasedProcessor to handle IO::EAGAINWaitReadable
Hydra::Derivatives::Processors::Jpeg2kImage.prepend Extensions::Hydra::Derivatives::Processors::WaitReadable
57 changes: 57 additions & 0 deletions lib/extensions/hydra/derivatives/processors/wait_readable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module Extensions
module Hydra
module Derivatives
module Processors
module WaitReadable
def self.prepended(base)
base.class_eval do

# Modified execute_without_timeout from hydra-derivatives
# Retries IO::WaitReadable errors
# Remove when updated to hydra-derivatives >= 3.8.0
def self.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(::Hydra::Derivatives::Processors::ShellBasedProcessor::BLOCK_SIZE)

case fileno
when stderr.fileno
err_str << data
end
rescue IO::WaitReadable
::Hydra::Derivatives::Logger.warn "Caught an IO::WaitReadable error in ShellBasedProcessor. Retrying..."
IO.select([f], nil, nil, 60)
retry
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
end
end
end
end
end
end
end

0 comments on commit e6ab0ae

Please sign in to comment.