【问题标题】:Delayed_Paperclip + Sidekiq + Mongoid-PaperclipDelayed_Paperclip + Sidekiq + Mongoid-Paperclip
【发布时间】:2015-09-02 22:53:59
【问题描述】:

我正在开发一个 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
  • Rails 4.1.9
  • Sidekiq 3.5.0

【问题讨论】:

    标签: ruby-on-rails-4 mongoid paperclip sidekiq delayed-paperclip


    【解决方案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
    

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

    【讨论】:

    • 感谢您回来并如此详细地回答您自己的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多