Skip to content

Commit

Permalink
Cleanup derivatives after error (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliotjordan authored and tampakis committed Sep 6, 2017
1 parent 9bb6634 commit 2a994fa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Metrics/AbcSize:
- 'lib/geo_works/fgdc_metadata_extractor.rb'

Metrics/MethodLength:
Max: 14
Exclude:
- 'Rakefile'
- 'app/models/concerns/geo_works/extractors/iso19139_helper.rb'
Expand Down
22 changes: 15 additions & 7 deletions app/processors/geo_works/processors/base_geo_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,25 @@ module BaseGeoProcessor
# @param out_path [String] processor output file path
# @param method_queue [Array] set of commands to run
# @param options [Hash] creation options to pass
# rubocop:disable Metrics/MethodLength
def self.run_commands(in_path, out_path, method_queue, options)
next_step = method_queue.shift
if method_queue.empty?
method(next_step).call(in_path, out_path, options)
else
temp = temp_path(out_path)
method(next_step).call(in_path, temp, options)
run_commands(temp, out_path, method_queue, options)
FileUtils.rm_rf(temp)
temp = temp_path(out_path)
begin
if method_queue.empty?
method(next_step).call(in_path, out_path, options)
else
method(next_step).call(in_path, temp, options)
run_commands(temp, out_path, method_queue, options)
FileUtils.rm_rf(temp)
end
rescue => e
FileUtils.rm_rf(in_path) if Dir.exist?(in_path)
FileUtils.rm_rf(temp) if Dir.exist?(temp)
raise e
end
end
# rubocop:enable Metrics/MethodLength

# Returns a path to an intermediate temp file or directory.
# @param path [String] input file path to base temp path on
Expand Down
2 changes: 0 additions & 2 deletions app/processors/geo_works/processors/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ module Image
# @param out_path [String] processor output file path.
# @param options [Hash] creation options
# @option options [String] `:output_size` as "w h" or "wxh"
# rubocop:disable Metrics/MethodLength
def self.convert(in_path, out_path, options)
size = options[:output_size].tr(' ', 'x')
convert = MiniMagick::Tool::Convert.new(whiny: false)
Expand All @@ -30,7 +29,6 @@ def self.convert(in_path, out_path, options)
# suppress stderr b/c geotiffs return 'unknown field' warnings
convert.call { |_stdout, _stderr| }
end
# rubocop:enable Metrics/MethodLength

# Trims extra whitespace.
# @param in_path [String] file input path
Expand Down
12 changes: 12 additions & 0 deletions spec/processors/geo_works/processors/base_geo_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ def source_path
expect(FileUtils).to receive(:rm_rf).twice
subject.class.run_commands(file_name, output_file, method_queue, options)
end

context 'when the processor raises an error' do
before do
allow(method_queue).to receive(:empty?).and_raise('error')
allow(Dir).to receive(:exist?).and_return(true)
end

it 'cleans the derivative intermediates' do
expect(FileUtils).to receive(:rm_rf).twice
expect { subject.class.run_commands(file_name, output_file, method_queue, options) }.to raise_error
end
end
end

describe '#temp_path' do
Expand Down

0 comments on commit 2a994fa

Please sign in to comment.