class Radar
include Mongoid::Document
after_save :post_on_facebook
private
def post_on_facebook
if self.user.settings.post_facebook
Delayed::Job.enqueue(::FacebookJob.new(self.user,self.body,url,self.title),0,self.active_from)
end
end
end
class FacebookJob < Struct.new(:user,:body,:url,:title)
include SocialPluginsHelper
def perform
facebook_client(user).publish_feed('', :message => body, :link => url, :name => title)
end
end
我想在特定日期執行post_on_facebook方法。我將此日期存儲在「active_from」字段中。delayed_job。從隊列中刪除作業
上面的代碼正在工作,工作在正確的日期執行。
但在某些情況下,我首先創建Radar對象並將一些作業發送到延遲作業隊列。之後,我更新這個對象併發送另一個作業到延遲作業。
這是錯誤的行爲,因爲我不會在正確的時間只執行一次作業。在這個實現中,我將有2個將被執行的工作。我如何刪除以前的作業,只更新一個作業?
Rails 3.0.7
Delayed Job => 2.1.4 https://github.com/collectiveidea/delayed_job
PS:對不起,我的英語我試着盡我所能
@Unixmonkey ......如果什麼工作正在'after_save'回調中創建。 – Rohit 2011-12-16 06:38:44
@Rohit after_save將運行此方法,並且delayed_job_id不會在記錄上設置(因爲您無法在after_save中調用保存而沒有進入無限循環) – Unixmonkey 2011-12-19 17:52:39