Rails 5 ActiveStorage 如何等待所有线程完成 [英] Rails 5 ActiveStorage How to wait for all threads to finish

查看:57
本文介绍了Rails 5 ActiveStorage 如何等待所有线程完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,让我描述一下问题.我正在使用 RSpec 运行脚本来生成一些测试数据.

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

脚本看起来像这样.

    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)

此脚本将向 FoodController 发送数据,然后添加更多食品.每个食品都使用 ActiveStorage 来保存图像.

This script will send data to FoodController which then add more food items. Every food item uses ActiveStorage to save an image.

问题是这个脚本完成得如此之快,正如我发现的那样,ActiveStorage 会产生新的线程来处理图像.结果,我有许多未完成的图像,并且在尝试加载图像时显示minimagick::invalid error with message wrong image header".

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.

目前,我正在使用 sleep(5.minutes) 来处理这个问题.只是想知道是否有另一种方法可以正确地做到这一点.

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.

推荐答案

答案是将 image = fixture_file_upload() 移动到 (0..200).each {} 循环中.花了我一天

The answer is to move image = fixture_file_upload() into the (0..200).each {} loop. Took me a day

这篇关于Rails 5 ActiveStorage 如何等待所有线程完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆