2013-07-30 124 views
2

我基本上是在Railscast 383中編寫項目 - 第二部分,當照片直接上傳到AWS S3時,然後由Sidekiq在後臺處理照片以創建縮略圖版本的照片。我在Rails 4上。Sidekiq一遍又一遍地重複同一個作業

我的問題是,Sidekiq作業成功完成後,一再重複,而不是停止。

我哪裏錯了?我看不到我的Railscast的代碼和任何區別,除了我在軌道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 

ImageWorker:

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 

回答

1

的原因之一sidekiq工人是一次又一次地被調用是因爲每次保存照片對象時都會調用perform_async,這發生在sidekiq工作人員本身中。 因此每次調用ImageWorker時,都會保存照片,再次調用ImageWorker,創建您正在經歷的循環。

您確定在再次致電ImageWorker之前,您沒有錯過對:image_processed標記的檢查爲真。 試試這個:

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

這將檢查圖像之前就有過一次處理。我認爲這可能意味着在軌道投射中設置,但作者忘了它,否則image_processed標誌已過時。

相關問題