class ActiveStorage::Service::MirrorService
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
Attributes
mirrors[R]
primary[R]
Public Class Methods
new(primary:, mirrors:)
click to toggle source
# File lib/active_storage/service/mirror_service.rb, line 28 def initialize(primary:, mirrors:) @primary, @mirrors = primary, mirrors end
Public Instance Methods
delete(key)
click to toggle source
Delete the file at the key
on all services.
# File lib/active_storage/service/mirror_service.rb, line 42 def delete(key) perform_across_services :delete, key end
delete_prefixed(prefix)
click to toggle source
Delete files at keys starting with the prefix
on all services.
# File lib/active_storage/service/mirror_service.rb, line 47 def delete_prefixed(prefix) perform_across_services :delete_prefixed, prefix end
mirror(key, checksum:)
click to toggle source
Copy the file at the key
from the primary service to each of
the mirrors where it doesn't already exist.
# File lib/active_storage/service/mirror_service.rb, line 53 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(key, io, checksum: nil, **options)
click to toggle source
Upload the io
to the key
specified to all
services. If a checksum
is provided, all services will ensure
a match when the upload has completed or raise an ActiveStorage::IntegrityError.
# File lib/active_storage/service/mirror_service.rb, line 34 def upload(key, io, checksum: nil, **options) each_service.collect do |service| io.rewind service.upload key, io, checksum: checksum, **options end end
Private Instance Methods
each_service(&block)
click to toggle source
# File lib/active_storage/service/mirror_service.rb, line 67 def each_service(&block) [ primary, *mirrors ].each(&block) end
perform_across_services(method, *args)
click to toggle source
# File lib/active_storage/service/mirror_service.rb, line 71 def perform_across_services(method, *args) # FIXME: Convert to be threaded each_service.collect do |service| service.public_send method, *args end end