Attachments not stored, stuck in cache

I am converting a large CarrierWave storage to Shrine and am trying to track down a random issue.

My conversion code looks like this…
I have an Attachment class with a Carrierwave uploader and Shrine attacher on the file and file_data attributes.

# pull the carrierwave file from storage to cache
cw = a.file
cw.cache_stored_file!
# upload to Shrine
a.file_attacher.assign File.open(cw.tmp_cache_path, "rb")
a.save

This code works great, however as a quality check, I am scanning all records to ensure that the files exist, are the correct size, and are stored in the storage. I am finding some random cases…

Attachment.find_each do |a|
  if a.cached? and !a.stored?
     raise "attachment is cached but not stored"
  end
end

In some random cases, a.cached? returns true while a.stored? returns false. The file does not get processed into storage.

Question - why does the …file_attacher.assign(…) not return some error status if the file does not make it all of the way to storage? Or maybe it does - does anyone have suggestions about how to handle this error scenario? Thanks!

Question - why does the …file_attacher.assign(…) not return some error status if the file does not make it all of the way to storage? Or maybe it does - does anyone have suggestions about how to handle this error scenario? Thanks!

Shrine will raise a failure if there are any problems with the upload itself. However, if the model instance doesn’t get saved due to validation errors (be it model or file), then that won’t be reported anywhere if you’re using something like save! which will raise an error if validations failed. I don’t know what is the reason for some attachments not ending up in permanent storage in your case.

For scripts I recommend using Attacher#attach, which uploads the file directly to permanent storage (you generally only need temporary storage for direct uploads and handling validation errors).