我正在开发一个处理用户上传照片和视频的Rails 4.1引擎。我正在使用Mongoid-Paperclip处理上传和Paperclip-av-transcoder将视频编码为多种格式。所有文件都存储在S3中。所有这些都可以正常工作,但正如您所料,对视频进行编码可能需要相当长的时间,因此下一步是在后台进行。我做了一些谷歌搜索,发现Delayed_Paperclip似乎做了我需要的。之后,似乎Sidekiq是处理后台处理的最佳选择。
现在的问题是,我无法让所有这些工作在一起。运行我的单元测试我得到NoMethodError: undefined method 'process_in_background'
所以看起来问题出在Delayed_Paperclip上,虽然它没有特殊的设置。
这是解决问题的模型
module MyEngine
class Video
include Mongoid::Document
include Mongoid::Paperclip
has_mongoid_attached_file :file,
:path => ':hash.:extension',
:hash_secret => "the-secret",
:storage => :s3,
:url => ':s3_domain_url',
:s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
:bucket => "my-bucket-#{Rails.env}",
:styles => {
:mp4 => { :format => 'mp4', :convert_options => { :output => { :vcodec => 'libx264', :acodec => 'copy' } } },
:ogg => { :format => 'ogg', :auto_rotate => true },
:webm => { :format => 'webm', :auto_rotate => true },
:thumb => { :geometry => "250x187#", :format => 'jpg', :time => 10, :auto_rotate => true }
},
:processors => [:transcoder]
validates_attachment :file, :content_type => { :content_type => ["video/x-flv", "video/mp4", "video/ogg", "video/webm", "video/x-ms-wmv", "video/x-msvideo", "video/quicktime", "video/3gpp"] }
process_in_background :file
end
end
我尝试将require "delayed_paperclip"
添加到lib/myengine/myengine.rb
文件中,但这没有帮助。
关于Sidekiq,我已将test_helper.rb
添加到以下内容中:
require 'sidekiq/testing'
Sidekiq::Testing.inline!
请注意,我没有忘记运行bundle install
并且Redis已启动并正在运行。我正在使用Mongoid,而不是活跃的记录。
我做错了什么?有没有人成功使用过此设置?我应该尝试另一种宝石组合吗?
有条件的信息:
答案 0 :(得分:1)
我一直在挖掘delayed_paperclip的代码,它肯定与ActiveRecord绑定,因此与Mongoid不兼容。我尝试了chrome.tabs.update但是这个宝石在4年内没有更新过,而且就我所知,它似乎不适用于当前版本的rails / mongoid / paperclip。
因此,我决定解决我的问题的最佳方法是覆盖与ActiveRecord集成的delayed_paperclip的代码,并使其与Mongoid一起使用。
这就是我最终做的事情,到目前为止似乎工作正常:
lib/myengine.rb
require "mongoid_paperclip"
require "paperclip/av/transcoder"
require "delayed_paperclip"
require "myengine/engine"
module Myengine
end
DelayedPaperclip::Railtie.class_eval do
initializer 'delayed_paperclip.insert_into_mongoid' do |app|
ActiveSupport.on_load :mongoid do
DelayedPaperclip::Railtie.insert
end
if app.config.respond_to?(:delayed_paperclip_defaults)
DelayedPaperclip.options.merge!(app.config.delayed_paperclip_defaults)
end
end
# Attachment and URL Generator extends Paperclip
def self.insert
Paperclip::Attachment.send(:include, DelayedPaperclip::Attachment)
Paperclip::UrlGenerator.send(:include, DelayedPaperclip::UrlGenerator)
end
end
DelayedPaperclip::InstanceMethods.class_eval do
def enqueue_post_processing_for name
DelayedPaperclip.enqueue(self.class.name, read_attribute(:id).to_s, name.to_sym)
end
end
然后您只需要将delayed_paperclip粘合剂包含在模型中:
module Myengine
class Video
include Mongoid::Document
include Mongoid::Paperclip
include DelayedPaperclip::Glue # <---- Include this
has_mongoid_attached_file :file,
:path => ':hash.:extension',
:hash_secret => "the-secret",
:storage => :s3,
:url => ':s3_domain_url',
:s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
:bucket => "my-bucket-#{Rails.env}",
:styles => {
:mp4 => { :format => 'mp4', :convert_options => { :output => { :vcodec => 'libx264', :acodec => 'copy' } } },
:ogg => { :format => 'ogg', :auto_rotate => true },
:webm => { :format => 'webm', :auto_rotate => true },
:thumb => { :geometry => "250x187#", :format => 'jpg', :time => 10, :auto_rotate => true }
},
:processors => [:transcoder]
validates_attachment :file, :content_type => { :content_type => ["video/x-flv", "video/mp4", "video/ogg", "video/webm", "video/x-ms-wmv", "video/x-msvideo", "video/quicktime", "video/3gpp"] }
process_in_background :file
end
end
希望这会为别人省下所有麻烦。