Class: ArchiveStorage::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/archive_storage/migrator.rb

Instance Method Summary collapse

Constructor Details

#initialize(planner: nil) ⇒ Migrator

Returns a new instance of Migrator.



10
11
12
# File 'lib/archive_storage/migrator.rb', line 10

def initialize(planner: nil)
  @planner = planner
end

Instance Method Details

#cleanup_source!(file_record) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/archive_storage/migrator.rb', line 94

def cleanup_source!(file_record)
  return false unless cleanup_ready?(file_record)

  source_storage = file_record.source_storage.to_sym
  ArchiveStorage.adapter(source_storage).delete(file_record_source_key(file_record))
  file_record.update!(source_deleted_at: Time.now, source_delete_pending: false)
  true
end

#enqueue_or_migrate!(inline: false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/archive_storage/migrator.rb', line 14

def enqueue_or_migrate!(inline: false)
  count = 0

  planner.each_candidate do |candidate|
    file_record = ArchiveStorage.registry.claim_candidate(candidate)
    next unless file_record

    if inline
      migrate_record!(file_record)
    else
      Enqueuer.new.enqueue_migration(file_record.id)
    end

    count += 1
  end

  count
end

#migrate_record!(file_record) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/archive_storage/migrator.rb', line 33

def migrate_record!(file_record)
  file_record.with_lock do
    source_storage = (file_record.source_storage || file_record.current_storage).to_sym
    target_storage = file_record.target_storage.to_sym
    source_key = file_record_source_key(file_record)
    target_key = file_record_target_key(file_record)

    return file_record if source_storage == target_storage && file_record.verified_at

    file_record.update!(
      migration_started_at: Time.now,
      attempts: file_record.attempts.to_i + 1,
      last_error: nil
    )

    source = ArchiveStorage.adapter(source_storage)
    target = ArchiveStorage.adapter(target_storage)

    target.copy_from(source, source_key, target_key)
    verification = Verifier.new.verify!(
      source_adapter: source,
      target_adapter: target,
      source_key: source_key,
      target_key: target_key
    )
     = verification.

    file_record.update!(
      current_storage: target_storage.to_s,
      source_storage: source_storage.to_s,
      target_storage: target_storage.to_s,
      storage_key: target_key,
      byte_size: .byte_size,
      content_type: .content_type,
      checksum: .checksum || .etag,
      migrated_at: Time.now,
      verified_at: Time.now,
      source_delete_pending: source_storage != target_storage,
      last_error: nil
    )
  end

  file_record
rescue StandardError => error
  safe_update_error(file_record, error)
  raise
end

#verify_record!(file_record) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/archive_storage/migrator.rb', line 81

def verify_record!(file_record)
  target_storage = (file_record.target_storage || file_record.current_storage).to_sym
   = ArchiveStorage.adapter(target_storage).head(file_record.storage_key)

  file_record.update!(
    byte_size: .byte_size,
    content_type: .content_type,
    checksum: .etag,
    verified_at: Time.now,
    last_error: nil
  )
end