我正在使用Rails 4.1引擎来处理用户上传的照片和视频。我正在使用蒙哥德-剪纸剪辑处理上传和纸剪辑-av-转码器来编码视频的几种格式。所有文件都存储在S3中。所有这些都很好,但正如您所预期的那样,编码视频可能需要相当长的时间,所以下一步是在后台实现这一点。我搜索了一下,发现回形针似乎能满足我的需要。此后,塞迪基克似乎是处理后台处理的最佳选择。
现在的问题是,我不能让所有这些一起工作。运行我的单元测试,我得到了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已经启动并正在运行。我用的是蒙哥德,不是活动记录。
我做错什么了?有人成功地使用了这个设置吗?还有别的宝石我应该试试吗?
相关信息:
发布于 2015-09-14 09:11:42
我一直在深入研究delayed_paperclip的代码,它肯定与ActiveRecord有关联,因此与Mongoid不兼容。我尝试了一下队列,但gem已经4年没有更新了,据我所知,它似乎无法与rails/mongoid/回形针的当前版本一起工作。
因此,我决定解决我的问题的最好方法是重写与delayed_paperclip集成的ActiveRecord代码,并让它与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
希望这能省去其他人的麻烦。
https://stackoverflow.com/questions/32364454
复制相似问题