diff options
author | Jeremy Daer <jeremydaer@gmail.com> | 2017-07-08 15:41:14 -0700 |
---|---|---|
committer | Jeremy Daer <jeremydaer@gmail.com> | 2017-07-08 17:16:52 -0700 |
commit | 6116313da4996ef99dcb45e2b9ac90ef073caabc (patch) | |
tree | 985b02ee94c8b11938ae4a73805577f29861a8ca /lib | |
parent | 8f125d5b7970727064895f67887a59c1cedcf273 (diff) | |
download | rails-6116313da4996ef99dcb45e2b9ac90ef073caabc.tar.gz rails-6116313da4996ef99dcb45e2b9ac90ef073caabc.tar.bz2 rails-6116313da4996ef99dcb45e2b9ac90ef073caabc.zip |
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/active_storage/service/mirror_service.rb | 19 |
1 files changed, 9 insertions, 10 deletions
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 |