问题
First, let me describe the problem. I'm using RSpec to run a script to generate some test data.
RAILS_ENV=development rspec spec/dev/food/food_upload.rb
The script looks like this.
image = fixture_file_upload(Rails.root.join('spec', 'resources', 'test2.png'), 'image/png')
(0..200).each {
food = FactoryBot.build :food
p "Loading #{food.name}"
expect {
post :upload, params: {
"foodUpload_foodName"=> food.name,
"foodUpload_image"=> image
}
}.to change(Food, :count).by(1)
.and change(Image, :count).by(1)
This script will send data to FoodController which then add more food items. Every food item uses ActiveStorage to save an image.
The problem is this script finishes so quickly and as I found out, ActiveStorage spawns new threads to process images. As the result, I have many unfinished images and it shows 'minimagick::invalid error with message improper image header' when trying to load the images.
At the moment, I'm using sleep(5.minutes) to handle this problem. Just want to know if there is another way to do it properly.
回答1:
The answer is to move image = fixture_file_upload()
into the (0..200).each {} loop. Took me a day
来源:https://stackoverflow.com/questions/51236616/rails-5-activestorage-how-to-wait-for-all-threads-to-finish