Module: MassiveImport

Defined in:
lib/massive-import.rb,
lib/massive-import/planner_job.rb,
lib/massive-import/processor_job.rb,
lib/massive-import/dashboard_server.rb

Defined Under Namespace

Classes: Batch, Configuration, DashboardServer, Import, PlannerJob, ProcessorJob, Record

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



104
105
106
# File 'lib/massive-import.rb', line 104

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



110
111
112
# File 'lib/massive-import.rb', line 110

def configure
  yield(configuration)
end

.reset!Object



114
115
116
# File 'lib/massive-import.rb', line 114

def reset!
  @configuration = Configuration.new
end

.stage_records(import, enumerable, **options) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/massive-import.rb', line 118

def stage_records(import, enumerable, **options)
  import.reload
  max_attempts = options.fetch(:max_attempts, import.max_attempts)
  processor_class = options.fetch(:processor_class, import.processor_class)
  batch_size = options.fetch(:batch_size, import.batch_size)
  max_concurrency = options.fetch(:max_batch_concurrency, import.max_batch_concurrency)

  updated = Import
    .where(id: import.id, status: 'PENDING')
    .update_all(
      status: 'STAGING',
      max_attempts: max_attempts,
      processor_class: processor_class.to_s,
      batch_size: batch_size,
      max_batch_concurrency: max_concurrency
    )

  return unless updated > 0

  import.reload
  end_id = import.end_id
  enumerable.each_slice(import.batch_size) do |slice|
    records = slice.map do |record|
      {
        import_id: import.id,
        attempt: import.attempt,
        status: 'PENDING',
        data: record
      }
    end

    committed =
      ActiveRecord::Base.transaction do
        Record.insert_all(records)
        end_id = Record
          .where(import_id: import.id, attempt: import.attempt)
          .order(id: :desc)
          .limit(1)
          .pick(:id)
        raise ActiveRecord::Rollback unless end_id

        updated = Import
          .where(id: import.id, status: 'STAGING')
          .update_all(["total_records = total_records + ?, end_id = ?", records.size, end_id])
        raise ActiveRecord::Rollback unless updated > 0

        true
      end

    return unless committed
  end

  updated = Import
    .where(id: import.id, status: 'STAGING')
    .update_all(status: 'RUNNING', end_id: 0)
  return unless updated > 0

  PlannerJob.set(queue: MassiveImport.configuration.queue_name).perform_async({'import_id' => import.id})
end