我实际上是在 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/