Class: Import

Inherits:
Ekylibre::Record::Base show all
Defined in:
app/models/import.rb

Overview

Informations

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2016 Brice Texier, David Joulin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see www.gnu.org/licenses.

Table: imports

archive_content_type   :string
archive_file_name      :string
archive_file_size      :integer
archive_updated_at     :datetime
created_at             :datetime         not null
creator_id             :integer
id                     :integer          not null, primary key
imported_at            :datetime
importer_id            :integer
lock_version           :integer          default(0), not null
nature                 :string           not null
progression_percentage :decimal(19, 4)
state                  :string           not null
updated_at             :datetime         not null
updater_id             :integer

Defined Under Namespace

Classes: InterruptRequest

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Ekylibre::Record::Base

#already_updated?, attr_readonly_with_conditions, #check_if_destroyable?, #check_if_updateable?, columns_definition, complex_scopes, customizable?, #customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, human_attribute_name_with_id, nomenclature_reflections, #old_record, #others, refers_to, scope_with_registration, simple_scopes, #updateable?

Class Method Details

.launch(nature, file) ⇒ Object

Create an import and run it in background


61
62
63
64
65
66
# File 'app/models/import.rb', line 61

def launch(nature, file)
  f = File.open(file)
  import = create!(nature: nature, archive: f)
  ImportRunJob.perform_later(import.id)
  import
end

.launch!(nature, file, &block) ⇒ Object

Create an import and run it directly


69
70
71
72
73
74
# File 'app/models/import.rb', line 69

def launch!(nature, file, &block)
  f = File.open(file)
  import = create!(nature: nature, archive: f)
  import.run(&block)
  import
end

Instance Method Details

#abortObject

Removing progress is the signal to interrupt the process


118
119
120
121
# File 'app/models/import.rb', line 118

def abort
  FileUtils.rm_rf(progress_file)
  update_column(:state, :aborted)
end

#nameObject


77
78
79
# File 'app/models/import.rb', line 77

def name
  nature.respond_to?(:text) ? nature.text : nature.to_s.humanize
end

#notify(message, interpolations = {}, options = {}) ⇒ Object


123
124
125
126
127
# File 'app/models/import.rb', line 123

def notify(message, interpolations = {}, options = {})
  if creator
    creator.notify(message, interpolations.merge(name: name), options.merge(target: self))
  end
end

#progressObject


107
108
109
110
111
# File 'app/models/import.rb', line 107

def progress
  File.read(progress_file).to_i
rescue
  0
end

#progress_fileObject


103
104
105
# File 'app/models/import.rb', line 103

def progress_file
  Ekylibre::Tenant.private_directory.join('tmp', 'imports', "#{id}.progress")
end

#runObject

Run an import. The optional code block permit have access to progression on each check point

Raises:


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/import.rb', line 87

def run
  FileUtils.mkdir_p(progress_file.dirname)
  update_columns(state: :in_progress, progression_percentage: 0)
  File.write(progress_file, 0.to_s)
  Ekylibre::Record::Base.transaction do
    ActiveExchanger::Base.import(nature.to_sym, archive.path) do |progression, count|
      update_columns(progression_percentage: progression)
      raise InterruptRequest unless File.exist? progress_file
      File.write(progress_file, progression.to_i.to_s)
      break if block_given? && !yield(progression, count)
    end
  end
  raise InterruptRequest unless File.exist? progress_file
  update_columns(state: :finished, progression_percentage: 100, imported_at: Time.zone.now, importer_id: (User.stamper.is_a?(User) ? User.stamper.id : User.stamper.is_a?(Fixnum) ? User.stamper : nil))
end

#run_laterObject


81
82
83
# File 'app/models/import.rb', line 81

def run_later
  ImportRunJob.perform_later(id)
end

#runnable?Boolean

Returns:

  • (Boolean)

113
114
115
# File 'app/models/import.rb', line 113

def runnable?
  undone? && archive.file?
end