Class: Tessa::MigrateAssetsJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/tessa/jobs/migrate_assets_job.rb

Defined Under Namespace

Classes: FieldProcessingState, ModelProcessingState, ProcessingState

Instance Method Summary collapse

Instance Method Details

#perform(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tessa/jobs/migrate_assets_job.rb', line 7

def perform(*args)
  options = args&.extract_options!
  options = {
    batch_size: 10,
    interval: 10.minutes.to_i
  }.merge!(options&.symbolize_keys || {})

  interval = options[:interval].seconds
  processing_state = args.first ? Marshal.load(args.first) : load_models_from_registry

  if processing_state.fully_processed?
    Rails.logger.info("Nothing to do - all models have transitioned to ActiveStorage")
    return
  end

  processing_state.batch_count = 0
  while processing_state.batch_count < options[:batch_size]
    model_state = processing_state.next_model

    process(processing_state, model_state, options)

    break if processing_state.fully_processed?
  end

  if processing_state.fully_processed?
    Rails.logger.info("Finished processing all Tessa assets")
  else
    remaining_batches = (processing_state.count / options[:batch_size].to_f).ceil

    Rails.logger.info("Continuing processing in #{interval}, "\
      "ETA #{(remaining_batches * interval).from_now}. "\
      "Working on #{processing_state.next_model.next_field}")

    processing_state.batch_count = 0
    self.class.set(wait: interval)
      .perform_later(Marshal.dump(processing_state), options)
  end
end