1

我正在开发一个处理用户上传照片和视频的 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,而不是活动记录。

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

附加信息:

  • Delayed_pa​​perclip 2.9.1
  • Mongoid 4.0.2
  • Mongoid-Paperclip 0.0.9
  • 回形针 4.2.1
  • Paperclip-av-transcoder 0.6.4
  • 导轨 4.1.9
  • Sidekiq 3.5.0
4

1 回答 1

1

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

因此,我决定解决我的问题的最佳方法是覆盖与 ActiveRecord 集成的 delay_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_pa​​perclip 胶水:

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

希望这可以为其他人省去所有的麻烦。

于 2015-09-14T17:11:42.890 回答