Skip to content

Commit

Permalink
Merge pull request #150 from projecthydra/cancelled_status
Browse files Browse the repository at this point in the history
Handle the case where active_encode returns a 'cancelled' status
  • Loading branch information
cjcolvar committed Apr 11, 2017
2 parents e8fb97a + a2badd6 commit a831f7c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Expand Up @@ -43,6 +43,10 @@ Style/PredicateName:
Style/RaiseArgs:
Enabled: false

Style/HashSyntax:
Exclude:
- spec/processors/active_encode_spec.rb

RSpec/ExampleWording:
CustomTransform:
be: is
Expand Down
15 changes: 9 additions & 6 deletions lib/hydra/derivatives/processors/active_encode.rb
Expand Up @@ -4,19 +4,22 @@ module Hydra::Derivatives::Processors
class ActiveEncode < Processor
def process
encode = ::ActiveEncode::Base.create(source_path, directives)

# TODO: wait until the encode job succeeds then call output_file_service with the output url
# while(encode.reload.running?) do
# p "Wait: #{Time.now}"
# sleep 10
# end
# while(encode.reload.running?) { sleep 10 }

raise_exception_if_encoding_failed(encode)
raise_exception_if_encoding_cancelled(encode)

# TODO: call output_file_service with the output url
end

def raise_exception_if_encoding_failed(encode)
return unless encode.failed?
raise StandardError.new("Encoding failed: #{encode.errors.join(' ; ')}")
end

def raise_exception_if_encoding_cancelled(encode)
return unless encode.cancelled?
raise StandardError.new("Encoding cancelled: #{source_path}")
end
end
end
2 changes: 1 addition & 1 deletion lib/hydra/derivatives/services/uri_source_file_service.rb
Expand Up @@ -5,7 +5,7 @@ class UriSourceFileService
# @param [Hash] options
# @option options [Symbol] :source a method that can be called on the object to retrieve the source file
# @yield [Tempfile] a temporary source file that has a lifetime of the block
def self.call(object, options, &block)
def self.call(object, options, &_block)
source_name = options.fetch(:source)
yield(object.send(source_name).uri.to_s)
end
Expand Down
29 changes: 25 additions & 4 deletions spec/processors/active_encode_spec.rb
Expand Up @@ -11,22 +11,43 @@
describe '#process' do
subject { processor.process }

let(:encode_double) { double('encode double', reload: self, state: state, errors: errors) }
# Mock out the actual encoding, just pretend that the
# encode finished and returned a certain status.
let(:failed_status) { false }
let(:cancelled_status) { false }
let(:errors) { [] }
let(:encode_double) do
double('encode double',
reload: self, state: state, errors: errors,
:failed? => failed_status,
:cancelled? => cancelled_status)
end

context 'when the encoding failed' do
let(:state) { :failed }
let(:failed_status) { true }
let(:errors) { ['error 1', 'error 2'] }

# Mock out the actual encoding, just pretend that the
# encode returned a failed status.
before do
allow(encode_double).to receive(:failed?).and_return(true)
allow(::ActiveEncode::Base).to receive(:create).and_return(encode_double)
end

it 'raises an exception' do
expect { subject }.to raise_error('Encoding failed: error 1 ; error 2')
end
end

context 'when the encoding was cancelled' do
let(:state) { :cancelled }
let(:cancelled_status) { true }

before do
allow(::ActiveEncode::Base).to receive(:create).and_return(encode_double)
end

it 'raises an exception' do
expect { subject }.to raise_error("Encoding cancelled: #{file_path}")
end
end
end
end

0 comments on commit a831f7c

Please sign in to comment.