首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >上传不正常NGINX + Passenger + Carrierwave + Carrierwave_backgrounder

上传不正常NGINX + Passenger + Carrierwave + Carrierwave_backgrounder
EN

Stack Overflow用户
提问于 2014-09-17 22:20:37
回答 1查看 415关注 0票数 0

我有一个Rails 4.0.0应用程序,安装了一个名为episode的模型,它安装了一个名为file_uploader的carrierwave上传程序来上传mp3s。我使用carrierwave_backgrounder和resque设置我的应用程序,以后台处理上传的文件,这些文件使用carrierwave-ftp gem保存到sftp服务器。在我的本地机器上,它工作得很好。同样,在我的vps (CentOS 6)上,当我使用rails s甚至rails s -e production启动应用程序时,它工作得很好。然而,当我切换到nginx + passenger时,它不再像预期的那样工作。

这些文件被上传到/public/uploads/tmp目录,在那里它们应该是临时存储的,但它们从来没有移动到我指定的上传目录中,并且没有完成任何其他后处理工作,比如设置内容类型、删除缓存目录、设置文件大小和长度等。

所以,昨天,我从使用carrierwave_backgrounder命令save_in_background切换到process_in_background,现在它可以很好地用于本地存储的文件,但是,当我使用carrierwave-ftp gem切换到sftp存储时,文件会得到处理,即,它们被传输到我的sftp服务器,路径存储在我的模型中,但随后作业在Resque队列中挂起。

未执行的相关代码为:

代码语言:javascript
复制
process :set_content_type
process :save_content_type_duration_and_size_in_model

有没有人知道为什么使用开发模式甚至生产模式可以很好地工作,而不是使用nginx + passenger?

下面是所有相关代码:

episode.rb:

代码语言:javascript
复制
class Episode < ActiveRecord::Base
    require 'carrierwave/orm/activerecord'
    # require 'mp3info'

    mount_uploader :file, FileUploader
    process_in_background :file
    belongs_to :podcast

  validates :name, :podcast, :file, presence: true

  default_scope { order("created_at DESC") }

  scope :most_recent, ->(max = 5) { limit(max) }
end

file_uploader.rb:

代码语言:javascript
复制
# encoding: utf-8

class FileUploader < CarrierWave::Uploader::Base
  include CarrierWave::MimeTypes
  include ::CarrierWave::Backgrounder::Delay

  storage :sftp

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "#{model.podcast.name.to_s.downcase.parameterize}"
  end

  before :store, :remember_cache_id
  after :store, :delete_tmp_dir

  # This is the relevant code that is not getting executed
  process :set_content_type
  process :save_content_type_duration_and_size_in_model

  def save_content_type_duration_and_size_in_model
    model.content_type = file.content_type if file.content_type
    model.file_size = file.size
    Mp3Info.open(model.file.current_path) do |media|
      model.duration = media.length
    end
  end

  # store! nil's the cache_id after it finishes so we need to remember it for deletion
  def remember_cache_id(new_file)
    @cache_id_was = cache_id
  end

  def delete_tmp_dir(new_file)
    # make sure we don't delete other things accidentally by checking the name pattern
    if @cache_id_was.present? && @cache_id_was =~ /\A[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}\z/
      FileUtils.rm_rf(File.join(root, cache_dir, @cache_id_was))
    end
  end
end

config/initializers/carrierwave_backgrounder.rb:

代码语言:javascript
复制
CarrierWave::Backgrounder.configure do |c|
  c.backend :resque, queue: :carrierwave
end

代码语言:javascript
复制
CarrierWave.configure do |config|
  config.sftp_host = "ftphost.com"
  config.sftp_user = "ftp_user"
  config.sftp_folder = "ftp_password"
  config.sftp_url = "http://url.com"
  config.sftp_options = {
    :password => "ftp_password",
    :port     => 22
  }
end

如果你需要更多信息,尽管问。任何帮助都将不胜感激。

更新:嗯,奇怪的是,就像经常发生的情况一样,它现在正在神奇地工作。我不确定到底是什么在起作用,所以我担心这篇文章对其他在这一页上遇到麻烦的人没有任何帮助。

EN

回答 1

Stack Overflow用户

发布于 2014-12-17 07:07:04

我也有同样的问题。我的进程块在开发中运行(rails s),但不在apache2/passenger下运行。它并不美观,但我解决它的方法是将我的进程代码移到after :cache回调中。进程块是在缓存回调之前和之后之间调用的,所以这在我看来是合理的。

这里是超级奇怪的部分:我不是指调用函数,我是指将代码从进程块(或函数)复制出来,并直接粘贴到after_cache回调中。

我知道我做错了什么,导致了这种情况,但我想不通。希望这对你有帮助。

代码语言:javascript
复制
version :office_preview
  # comment out the following since it does nothing under Passenger
  #process :office_to_img

end

def office_to_img
 this won't be called under passenger :(
end

after :cache, :after_cache

def after_cache(file)
  #for some reason, calling it here doesn't do anything 
  #office_to_img
  code copied&pasted here from office_to_img
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25893166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档