【问题标题】:Imagemagick convert works from console but not in SidekiqImagemagick convert 可以从控制台工作,但不能在 Sidekiq 中工作
【发布时间】:2016-06-15 16:23:18
【问题描述】:

我有一个带有回形针附件(图像或 pdf 文件)的模型。我正在使用 Sidekiq 在后台进程中处理此模型。在后台过程中,我使用 imagemagick convert 函数将 pdf 文件转换为 pdf 文件字符串,以消除文件格式中的任何密码保护或垃圾。

此转换在我的开发环境的 sidekiq 和暂存服务器的 sidekiq 上运行良好,但在生产服务器的 sidekiq 上不起作用。在我的生产服务器上,如果我从 Rails 控制台运行转换方法,它就可以工作。当它不工作时,dst 文件为空,返回的字符串为''

模型是这样的

class Receipt < ActiveRecord::Base

  has_attached_file :image, styles: { original: {} }, path: ':rails_root/attachments/:class/:attachment/:id_partition/:style/:filename'

  validates_attachment_content_type :image, :content_type => IMAGE_MIME_TYPES

  def convert_pdf_to_pdf_string
    if %w(application/pdf application/octet-stream).include?(image.content_type)
      begin
        dst = Tempfile.new([File.basename(image.path, File.extname(image.path)), '.pdf'], :encoding => 'ascii-8bit')
        dst.flush
        `convert -density 200 #{image.path.shellescape} #{dst.path.shellescape}`
        dst
        str = File.open(dst.path, 'rb') {|f| f.read}.to_s
      ensure
        dst.close
        dst.unlink
      end
    end
  end
end

我不知道如何调试这个问题。

没有引发异常。我还尝试记录反引号调用的返回值,但没有返回任何内容。

请提出我应该检查的任何想法。

【问题讨论】:

  • 你在使用 Heroku 吗(生产服务器)?
  • 不,它是 Debian 服务器。暂存服务器和生产服务器的配置方式应相同。
  • 使用 pry-remote 之类的东西,设置断点,然后检查发生了什么。

标签: ruby-on-rails imagemagick sidekiq


【解决方案1】:

我开始想办法让convert 产生一些日志记录。在我将反引号调用更改为

之后
`convert -density 200 #{image.path.shellescape} #{dst.path.shellescape} 2>log/magick_error.log 1>log/magick.log`

代码开始工作。进入它产生的magick_error.log

    **** Warning: File has some garbage before %PDF- .

所以我猜这个到 STDERR 的输出是在我的生产环境中搞砸了一些事情。暂存和开发环境可以容忍这种情况。

【讨论】:

    猜你喜欢
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多