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”字段中。
上面的代码有效,作业会在正确的日期执行。
但是在某些情况下,我首先创建Radar对象并将一些作业发送到延迟作业队列中。之后我更新此对象并发送另一个作业到延迟作业。
这是错误的行为,因为我只想在正确的时间执行一次作业。在这种实现中,将有两个作业被执行。我如何删除以前的作业,以便只执行更新后的作业?
Rails 3.0.7
Delayed Job => 2.1.4 https://github.com/collectiveidea/delayed_job
ps:对不起我的英语不好,我会尽力的
after_save
回调中创建的,会怎样呢? - Rohit