diff options
| author | Robert | 2017-09-14 10:02:49 +0200 | 
|---|---|---|
| committer | Robert | 2017-09-14 10:02:49 +0200 | 
| commit | 51c14fbd20c9f821acf67352143cf0eefdda3fd2 (patch) | |
| tree | 899bc0980dbb894382b7a8d310f35ed15c7b3e10 | |
| parent | 8540c95482f97ce47d2ebe916f82d3cea01a4344 (diff) | |
| download | chouette-core-4477-import-unzip-to-files.tar.bz2 | |
Refs: #4477@0.5h; Sketch of a solution4477-import-unzip-to-files
| -rw-r--r-- | app/services/zip_service.rb | 18 | ||||
| -rw-r--r-- | app/workers/workbench_import_worker.rb | 25 | 
2 files changed, 25 insertions, 18 deletions
| diff --git a/app/services/zip_service.rb b/app/services/zip_service.rb index cab301b01..0220152d9 100644 --- a/app/services/zip_service.rb +++ b/app/services/zip_service.rb @@ -1,7 +1,7 @@  class ZipService    # TODO: Remove me before merge https://github.com/rubyzip/rubyzip -  class Subdir < Struct.new(:name, :stream) +  class Subdir < Struct.new(:name, :filename)    end    attr_reader :current_key, :current_output, :yielder @@ -48,10 +48,8 @@ class ZipService    def finish_current_output      if current_output -      @yielder  << Subdir.new( -        current_key, -        # Second part of the solution, yield the closed stream -        current_output.close_buffer) +      filename = create_subdir_file current_key, current_output.close_buffer +      @yielder << Subdir.new(current_key, filename)      end    end @@ -65,4 +63,14 @@ class ZipService      # last dir name File.dirname.split("/").last      entry.name.split('/', -1)[-2]    end + +  def create_subdir_file eg_name, eg_stream +    Rails.root.join('tmp', 'imports', "WorkbenchImport_#{eg_name}_#{$$}.zip") +      .tap do | filename | +      File.open(filename, 'wb').tap do |file| +        eg_stream.rewind +        file.write eg_stream.read +      end +    end +  end  end diff --git a/app/workers/workbench_import_worker.rb b/app/workers/workbench_import_worker.rb index 994493944..da3589f77 100644 --- a/app/workers/workbench_import_worker.rb +++ b/app/workers/workbench_import_worker.rb @@ -33,29 +33,24 @@ class WorkbenchImportWorker    end    def upload zip_service -    entry_group_streams = zip_service.subdirs -    @workbench_import.update total_steps: entry_group_streams.size -    entry_group_streams.each_with_index(&method(:upload_entry_group)) +    entry_group_files = zip_service.subdirs +    @workbench_import.update total_steps: entry_group_files.size +    entry_group_files.each_with_index(&method(:upload_entry_group))    rescue Exception => e      logger.error e.message -    @workbench_import.update( current_step: entry_group_streams.size, status: 'failed' ) +    @workbench_import.update( current_step: entry_group_files.size, status: 'failed' )      raise    end    def upload_entry_group entry_pair, element_count      @workbench_import.update( current_step: element_count.succ ) -    # status = retry_service.execute(&upload_entry_group_proc(entry_pair)) -    eg_name = entry_pair.name -    eg_stream = entry_pair.stream +    eg_name     = entry_pair.name +    eg_filename = entry_pair.filename      FileUtils.mkdir_p(Rails.root.join('tmp', 'imports')) -    eg_file = File.new(Rails.root.join('tmp', 'imports', "WorkbenchImport_#{eg_name}_#{$$}.zip"), 'wb').tap do |file| -      eg_stream.rewind -      file.write eg_stream.read -    end -    eg_file.close -    eg_file = File.new(Rails.root.join('tmp', 'imports', "WorkbenchImport_#{eg_name}_#{$$}.zip")) +    #eg_file = File.new(Rails.root.join('tmp', 'imports', "WorkbenchImport_#{eg_name}_#{$$}.zip"), 'wb').tap do |file| +    eg_file = File.new(eg_filename)      result = execute_post eg_name, eg_file      if result && result.status < 400        result @@ -63,6 +58,10 @@ class WorkbenchImportWorker        raise StopIteration, result.body      end    ensure +    # N.B. IIANM we can unlink the uploaded file as Java will access the copy kept by Carrier Wave in its +    # cache. +    # Therefore we do not need to remove this file in one of the cronjobs. +    # However the question arises: Who cleans out Carrier Wave's cache, and **when**?      eg_file.close rescue nil      eg_file.unlink rescue nil    end | 
