Delayed_Paperclip + Sidekick + Mongoid-回形针

Delayed_Paperclip + Sidekiq + Mongoid-Paperclip

我正在开发一个 Rails 4.1 引擎来处理用户上传的照片和视频。我正在使用 Mongoid-Paperclip 处理上传,并使用 Paperclip-av-transcoder 将视频编码为多种格式。所有文件都存储在 S3。所有这些工作正常,但正如您所料,对视频进行编码可能需要相当长的时间,因此下一步是在后台进行编码。我做了一些谷歌搜索,发现 Delayed_Paperclip that seems to do what I need. After that it seemed that 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 已启动并 运行ning。我正在使用 Mongoid,而不是活动记录。

我做错了什么?有没有人成功使用过这个设置?我应该尝试其他宝石组合吗?

附加信息:

我一直在研究 delayed_paperclip 的代码,它肯定与 ActiveRecord 相关,因此与 Mongoid 不兼容。我尝试了 mongoid_paperclip_queue,但 gem 已经 4 年没有更新了,而且据我所知似乎不适用于 rails/mongoid/paperclip 的当前版本。

因此,我决定解决我的问题的最佳方法是覆盖 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

希望这会为其他人省去所有麻烦。