Rails 5 ActiveStorage How to wait for all threads to finish

与世无争的帅哥 提交于 2019-12-25 00:34:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!