Class: ActiveStorage::Service::MirrorService
- Inherits:
-
ActiveStorage::Service
- Object
- ActiveStorage::Service
- ActiveStorage::Service::MirrorService
- Defined in:
- activestorage/lib/active_storage/service/mirror_service.rb
Overview
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
Instance Attribute Summary collapse
-
#mirrors ⇒ Object
readonly
Returns the value of attribute mirrors.
-
#primary ⇒ Object
readonly
Returns the value of attribute primary.
Attributes inherited from ActiveStorage::Service
Class Method Summary collapse
-
.build(primary:, mirrors:, name:, configurator:, **options) ⇒ Object
Stitch together from named services.
Instance Method Summary collapse
-
#delete(key) ⇒ Object
Delete the file at the
key
on all services. -
#delete_prefixed(prefix) ⇒ Object
Delete files at keys starting with the
prefix
on all services. -
#initialize(primary:, mirrors:) ⇒ MirrorService
constructor
A new instance of MirrorService.
-
#mirror(key, checksum:) ⇒ Object
Copy the file at the
key
from the primary service to each of the mirrors where it doesn’t already exist. -
#upload(key, io, checksum: nil, **options) ⇒ Object
Upload the
io
to thekey
specified to all services.
Methods inherited from ActiveStorage::Service
configure, #download, #download_chunk, #exist?, #headers_for_direct_upload, #open, #public?, #update_metadata, #url, #url_for_direct_upload
Methods included from ActiveSupport::Autoload
#autoload, #autoload_at, #autoload_under, #autoloads, #eager_autoload, #eager_load!, extended
Constructor Details
#initialize(primary:, mirrors:) ⇒ MirrorService
Returns a new instance of MirrorService.
29 30 31 |
# File 'activestorage/lib/active_storage/service/mirror_service.rb', line 29 def initialize(primary:, mirrors:) @primary, @mirrors = primary, mirrors end |
Instance Attribute Details
#mirrors ⇒ Object (readonly)
Returns the value of attribute mirrors
14 15 16 |
# File 'activestorage/lib/active_storage/service/mirror_service.rb', line 14 def mirrors @mirrors end |
#primary ⇒ Object (readonly)
Returns the value of attribute primary
14 15 16 |
# File 'activestorage/lib/active_storage/service/mirror_service.rb', line 14 def primary @primary end |
Class Method Details
.build(primary:, mirrors:, name:, configurator:, **options) ⇒ Object
Stitch together from named services.
20 21 22 23 24 25 26 27 |
# File 'activestorage/lib/active_storage/service/mirror_service.rb', line 20 def self.build(primary:, mirrors:, name:, configurator:, **) #:nodoc: new( primary: configurator.build(primary), mirrors: mirrors.collect { |mirror_name| configurator.build mirror_name } ).tap do |service_instance| service_instance.name = name end end |
Instance Method Details
#delete(key) ⇒ Object
Delete the file at the key
on all services.
43 44 45 |
# File 'activestorage/lib/active_storage/service/mirror_service.rb', line 43 def delete(key) perform_across_services :delete, key end |
#delete_prefixed(prefix) ⇒ Object
Delete files at keys starting with the prefix
on all services.
48 49 50 |
# File 'activestorage/lib/active_storage/service/mirror_service.rb', line 48 def delete_prefixed(prefix) perform_across_services :delete_prefixed, prefix end |
#mirror(key, checksum:) ⇒ Object
Copy the file at the key
from the primary service to each of the mirrors where it doesn’t already exist.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'activestorage/lib/active_storage/service/mirror_service.rb', line 54 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) ⇒ Object
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.
35 36 37 38 39 40 |
# File 'activestorage/lib/active_storage/service/mirror_service.rb', line 35 def upload(key, io, checksum: nil, **) each_service.collect do |service| io.rewind service.upload key, io, checksum: checksum, ** end end |