Skip to Content Skip to Search

class ActiveStorage::Service::MirrorService

Active Storage Mirror Service

Wraps a set of mirror services and provides a single ActiveStorage::Service object that will all have the files uploaded to them. A primary service is designated to answer calls to:

  • download

  • exists?

  • url

  • url_for_direct_upload

  • headers_for_direct_upload

Inherits From

Attributes

[R] mirrors
[R] primary

Public class methods

Source code GitHub
# File activestorage/lib/active_storage/service/mirror_service.rb, line 31
def initialize(primary:, mirrors:)
  @primary, @mirrors = primary, mirrors
end

Public instance methods

Delete the file at the key on all services.

Source code GitHub
# File activestorage/lib/active_storage/service/mirror_service.rb, line 45
def delete(key)
  perform_across_services :delete, key
end

Delete files at keys starting with the prefix on all services.

Source code GitHub
# File activestorage/lib/active_storage/service/mirror_service.rb, line 50
def delete_prefixed(prefix)
  perform_across_services :delete_prefixed, prefix
end

Copy the file at the key from the primary service to each of the mirrors where it doesn’t already exist.

Source code GitHub
# File activestorage/lib/active_storage/service/mirror_service.rb, line 59
def mirror(key, checksum:)
  instrument :mirror, key: key, checksum: checksum do
    if (mirrors_in_need_of_mirroring = mirrors.select { |service| !service.exist?(key) }).any?
      primary.open(key, checksum: checksum) do |io|
        mirrors_in_need_of_mirroring.each do |service|
          io.rewind
          service.upload key, io, checksum: checksum
        end
      end
    end
  end
end

Upload the io to the key specified to all services. The upload to the primary service is done synchronously whereas the upload to the mirrors is done asynchronously. If a checksum is provided, all services will ensure a match when the upload has completed or raise an ActiveStorage::IntegrityError.

Source code GitHub
# File activestorage/lib/active_storage/service/mirror_service.rb, line 38
def upload(key, io, checksum: nil, **options)
  io.rewind
  primary.upload key, io, checksum: checksum, **options
  mirror_later key, checksum: checksum
end

Definition files