mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
a8e849bb0d
The mirror service exists for the purpose of migration, where all blobs exist in the primary subservice and a subset of blobs exist in the secondary subservice. Since the primary subservice is the source of truth until a migration is completed, operations like existence checks need not be performed against the secondary subservices.
34 lines
720 B
Ruby
34 lines
720 B
Ruby
require "active_support/core_ext/module/delegation"
|
|
|
|
class ActiveStorage::Service::MirrorService < ActiveStorage::Service
|
|
attr_reader :services
|
|
|
|
delegate :download, :exist?, :url, :byte_size, :checksum, to: :primary_service
|
|
|
|
def initialize(services:)
|
|
@services = services
|
|
end
|
|
|
|
def upload(key, io)
|
|
services.collect do |service|
|
|
service.upload key, io
|
|
io.rewind
|
|
end
|
|
end
|
|
|
|
def delete(key)
|
|
perform_across_services :delete, key
|
|
end
|
|
|
|
private
|
|
def primary_service
|
|
services.first
|
|
end
|
|
|
|
def perform_across_services(method, *args)
|
|
# FIXME: Convert to be threaded
|
|
services.collect do |service|
|
|
service.public_send method, *args
|
|
end
|
|
end
|
|
end
|