From 6116313da4996ef99dcb45e2b9ac90ef073caabc Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Sat, 8 Jul 2017 15:41:14 -0700 Subject: Mirror: explicit primary service and list of mirrors Pass separate primary service and list of mirrors rather than treating the first of the services list as the primary. Nice fit for keyword args, and something we've long wanted in the equivalent Basecamp file repository. Upload returns the results of the underlying service uploads rather than the io.rewind result. Rewind before uploading rather than afterward, and demonstrate that behavior with a test. Test that more than one mirror works. --- lib/active_storage/service/mirror_service.rb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'lib/active_storage/service') diff --git a/lib/active_storage/service/mirror_service.rb b/lib/active_storage/service/mirror_service.rb index 1ec0930e6c..7ec166aace 100644 --- a/lib/active_storage/service/mirror_service.rb +++ b/lib/active_storage/service/mirror_service.rb @@ -1,18 +1,17 @@ require "active_support/core_ext/module/delegation" class ActiveStorage::Service::MirrorService < ActiveStorage::Service - attr_reader :services + attr_reader :primary, :mirrors - delegate :download, :exist?, :url, to: :primary_service + delegate :download, :exist?, :url, to: :primary - def initialize(services:) - @services = services + def initialize(primary:, mirrors:) + @primary, @mirrors = primary, mirrors end def upload(key, io, checksum: nil) - services.collect do |service| - service.upload key, io, checksum: checksum - io.rewind + each_service.collect do |service| + service.upload key, io.tap(&:rewind), checksum: checksum end end @@ -21,13 +20,13 @@ class ActiveStorage::Service::MirrorService < ActiveStorage::Service end private - def primary_service - services.first + def each_service(&block) + [ primary, *mirrors ].each(&block) end def perform_across_services(method, *args) # FIXME: Convert to be threaded - services.collect do |service| + each_service.collect do |service| service.public_send method, *args end end -- cgit v1.2.3