carrierwave - Sidekiq 一遍又一遍地重复同样的工作

我实际上是在 Railscast 383 中编写项目- 第二部分,当照片直接上传到 AWS S3 时,然后由 Sidekiq 在后台处理照片以创建照片的缩略图版本。我在 Rails 4 上。

我的问题是,Sidekiq 作业在成功完成后,不断重复,而不是停止。

我哪里错了?我看不出我的代码和 Railscast 的代码有什么区别,除了我在 Rails 4 上(如此强大的参数而不是 attr_accessible)

照片类:

class Photo < ActiveRecord::Base
  mount_uploader :image, ImageUploader

  default_scope order('updated_at DESC')

  after_save :enqueue_image

  def image_name
    File.basename(image.path || image.filename) if image
  end

  def enqueue_image
    ImageWorker.perform_async(id, key) if key.present?
  end
end

图像 worker :

class ImageWorker
  include Sidekiq::Worker

  sidekiq_options retry: false
  # sidekiq_options retry: 3

  def perform(id, key)
    photo = Photo.find(id)
    photo.key = key
    photo.remote_image_url = photo.image.direct_fog_url(with_path: true)
    photo.save!
    photo.update_column(:image_processed, true)
  end
end

上传者:

class ImageUploader < CarrierWave::Uploader::Base

  include CarrierWaveDirect::Uploader
  include CarrierWave::RMagick

  # storage :fog
  #### storage defaults to fog when CarrierWaveDirect::Uploader is included ####

  include CarrierWave::MimeTypes
  process :set_content_type

  version :thumb do
    process :resize_to_limit => [200, 200]
  end

  version :medium do
    process :resize_to_limit => [400, 400]
  end

end

最佳答案

一次又一次调用 sidekiq worker 的一个原因是因为每次保存照片对象时都会调用 perform_async,这发生在 sidekiq worker 本身。 因此,每次调用 ImageWorker 时,它都会保存照片,再次调用 ImageWorker,从而创建您正在经历的循环。

在再次调用 ImageWorker 之前,您确定没有遗漏检查 :image_processed 标签是否为真。 试试这个:

def enqueue_image
    ImageWorker.perform_async(id, key) if key.present? && !image_processed
end

这将检查图像之前是否被处理过一次。我认为这可能意味着要在 rails cast 中设置,但作者忘记了它,否则 image_processed 标志已过时。

https://stackoverflow.com/questions/17946320/

相关文章:

python - 如何在没有用户交互的情况下在在线服务器上启动后连续运行 python 脚本?

asp.net-mvc-4 - 使用 ADFS 登录创建角色成员资格

sql - 带有 OFFSET 的总行数 .. FETCH 问题

Magento - 如何为处理订单启用取消按钮(已创建发票)

bash - 使用 sendmail 强制内容类型

perl - 跳过(不执行该行)perl 调试器中的一行

canvas - 如何调试 CORS 错误

uiview - UIView 标签属性的最大值

python - 带有滚动条的 tkinter 表格显示

sql-server - 在存储过程中批处理多个合并是个好主意吗