Skip to content

Commit 3a27dee

Browse files
committed
fix: dont retrieve from store until accessing file
1 parent 8815592 commit 3a27dee

File tree

7 files changed

+39
-9
lines changed

7 files changed

+39
-9
lines changed

lib/carrierwave/mounter.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ def read_identifiers
6666
def uploaders
6767
@uploaders ||= read_identifiers.map do |identifier|
6868
uploader = blank_uploader
69-
uploader.retrieve_from_store!(identifier)
69+
70+
# Assign the identifier to the uploader here,
71+
# instead of forcing the uploader to make a round-trip
72+
# to the store if we already have the identifier.
73+
uploader.identifier = identifier
7074
uploader
7175
end
7276
end

lib/carrierwave/uploader.rb

-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ module Uploader
4242
# be trivial.
4343
#
4444
class Base
45-
attr_reader :file
46-
4745
include CarrierWave::Uploader::Configuration
4846
include CarrierWave::Uploader::Callbacks
4947
include CarrierWave::Uploader::Proxy

lib/carrierwave/uploader/configuration.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def reset_config
214214
config.base_path = CarrierWave.base_path
215215
config.enable_processing = true
216216
config.ensure_multipart_form = true
217-
config.download_retry_count = 0
217+
config.download_retry_count = 3
218218
config.download_retry_wait_time = 5
219219
end
220220
end

lib/carrierwave/uploader/mountable.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Uploader
33
module Mountable
44

55
attr_reader :model, :mounted_as
6+
attr_writer :identifier
67

78
##
89
# If a model is given as the first parameter, it will be stored in the

lib/carrierwave/uploader/remove.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Remove
1010
#
1111
def remove!
1212
with_callbacks(:remove) do
13-
@file.delete if @file
13+
@file.delete if file
1414
@file = nil
1515
@cache_id = nil
1616
end

lib/carrierwave/uploader/store.rb

+30-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module Store
88
include CarrierWave::Uploader::Cache
99

1010
included do
11+
attr_accessor :retrieval_retry_count
12+
1113
prepend Module.new {
1214
def initialize(*)
1315
super
@@ -93,17 +95,41 @@ def store!(new_file=nil)
9395
end
9496
end
9597

98+
# Attempt to retrieve the file from the store
99+
# if the file is nil, but to avoid calling this
100+
# over and over if retrieving it returns nil,
101+
# we keep track of the number of times we've
102+
# attempted to retrieve it and just return nil
103+
# if we exceed the maximum number of retries
104+
#
105+
# === Returns
106+
#
107+
# [CarrierWave::SanitizedFile, nil] the stored file or nil
108+
def file
109+
return @file unless @identifier
110+
retrieve_from_store!(@identifier) unless @file
111+
@file
112+
end
113+
96114
##
97115
# Retrieves the file from the storage.
98116
#
99117
# === Parameters
100118
#
101119
# [identifier (String)] uniquely identifies the file to retrieve
102120
#
103-
def retrieve_from_store!(identifier)
104-
with_callbacks(:retrieve_from_store, identifier) do
105-
@file = storage.retrieve!(identifier)
106-
@identifier = identifier
121+
def retrieve_from_store!(file_identifier)
122+
self.retrieval_retry_count ||= 0
123+
return if self.retrieval_retry_count > download_retry_count
124+
self.retrieval_retry_count += 1
125+
with_callbacks(:retrieve_from_store, file_identifier) do
126+
# We can't retrieve the file if we have no identifier
127+
# to retrieve it with. Identifier should be assigned
128+
# when setting up the uploader, or when caching,
129+
# or storing a new file
130+
next unless file_identifier
131+
@file = storage.retrieve!(file_identifier)
132+
@identifier = file_identifier
107133
end
108134
end
109135

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ def stub_request(method, uri)
162162
config.around :each, :with_retry do |example|
163163
example.run_with_retry retry: 2
164164
end
165+
config.example_status_persistence_file_path = "tmp/failures.txt"
165166
config.retry_callback = proc do |example|
166167
sleep 1
167168
end

0 commit comments

Comments
 (0)