Class: ArchiveStorage::Registry

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

Instance Method Summary collapse

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
# File 'lib/archive_storage/registry.rb', line 8

def available?
  defined?(::ActiveRecord::Base) &&
    ::ActiveRecord::Base.connected? &&
    record_class.table_exists?
rescue StandardError
  false
end

#claim_candidate(candidate) ⇒ Object Also known as: ensure_for_candidate



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/archive_storage/registry.rb', line 52

def claim_candidate(candidate)
  raise RegistryUnavailableError, "archive_storage_files table is not available" unless available?

  with_unique_retry do
    record = record_class.find_or_initialize_by(
      identity_for_candidate(candidate)
    )
    return nil unless claimable?(record)

    record.uploader = candidate.uploader.class.name
    record.current_storage = candidate.current_storage.to_s
    record.source_storage = candidate.current_storage.to_s
    record.target_storage = candidate.target_storage.to_s
    record.source_storage_key = candidate.source_storage_key.to_s if record.respond_to?(:source_storage_key=)
    record.target_storage_key = candidate.target_storage_key.to_s if record.respond_to?(:target_storage_key=)
    record.enqueued_at = Time.now if record.respond_to?(:enqueued_at=)
    record.source_delete_pending = false if record.respond_to?(:source_delete_pending=) && record.new_record?
    record.byte_size ||= candidate.byte_size
    record.content_type ||= candidate.content_type
    record.save!
    record
  end
end

#current_storage_for(uploader, identifier:, storage_key:, default:) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/archive_storage/registry.rb', line 25

def current_storage_for(uploader, identifier:, storage_key:, default:)
  find_for_uploader(
    uploader,
    identifier: identifier,
    storage_key: storage_key
  )&.current_storage&.to_sym || default
end

#find_for_uploader(uploader, identifier:, storage_key:) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/archive_storage/registry.rb', line 16

def find_for_uploader(uploader, identifier:, storage_key:)
  return nil unless available?
  return nil unless uploader_identity_available?(uploader)

  record_class.find_by(
    identity_for_uploader(uploader, identifier: identifier, storage_key: storage_key)
  )
end

#upsert_for_uploader(uploader, identifier:, storage_key:, current_storage:, metadata: {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/archive_storage/registry.rb', line 33

def upsert_for_uploader(uploader, identifier:, storage_key:, current_storage:, metadata: {})
  return nil unless available?
  return nil unless uploader_identity_available?(uploader)

  with_unique_retry do
    record = record_class.find_or_initialize_by(
      identity_for_uploader(uploader, identifier: identifier, storage_key: storage_key)
    )

    record.uploader = uploader.class.name
    record.current_storage = current_storage.to_s
    record.byte_size ||= [:byte_size]
    record.content_type ||= [:content_type]
    record.checksum ||= [:checksum]
    record.save!
    record
  end
end