[英]How to upload files in the background using rails carrierwave?
I'm currently using the gem Carrierwave
to upload files to my AWS s3 account. 我目前正在使用gem Carrierwave
将文件上传到我的AWS s3帐户。 But I will be uploading some large files, so I'm worried about the server timing out and the user having to wait. 但我会上传一些大文件,所以我担心服务器超时而用户不得不等待。 Anyone have any suggestions to move this to a background job and how to accomplish that? 任何人都有任何建议将其转移到后台工作以及如何实现这一目标? I've looked at sidekiq
but am lost trying to figure out where to start. 我看过sidekiq
但我想找出从哪里开始。 When I try to implement it the file doens't seem to upload even though it gives a success in Rails
. 当我尝试实现它时,即使它在Rails
取得成功,该文件也似乎无法上传。
there's a gem carrierwave_backgrounder
to help you queue your file uploads in the background 有一个gem carrierwave_backgrounder
可以帮助您在后台排队文件上传
add gem carrierwave_backgrounder
to your Gemfile
and bundle it 将gem carrierwave_backgrounder
添加到您的Gemfile
并捆绑它
Add a initializer file config/initializers/carrierwave_backgrounder.rb
to configure your background processor. 添加初始化文件config/initializers/carrierwave_backgrounder.rb
以配置后台处理器。 Something like this if you're using sidekiq 如果你使用sidekiq,这样的事情
CarrierWave::Backgrounder.configure do |c|
c.backend :sidekiq, queue: :carrierwave
end
Then, use it on your models along with your carrierwave uploader 然后,在您的模型上使用它以及您的carrierwave上传器
class User < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
process_in_background :avatar
end
IMPORTANT! 重要! You should define :carrierwave
queue in config/sidekiq.yml
您应该在config/sidekiq.yml
定义:carrierwave
队列
:queues:
- [carrierwave, 1]
- default
The best way is to use Carrierwave Direct as it bypasses the rails server completely. 最好的方法是使用Carrierwave Direct,因为它完全绕过了rails服务器。 You can then do actual processing as a background task (I recommend Sidekiq, but use whatever you want.) 然后,您可以将实际处理作为后台任务(我推荐Sidekiq,但使用您想要的任何内容。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.