Ruby on rails 3 rails回调和顺序或事件
我在创建记录时有三个不同的API调用 1) 调用为记录创建位URL。 2) 通过bitly URL呼叫发送到facebook 3) 使用bitly URL调用发布到twitter 我目前的程序如下: 记录创建和更新Ruby on rails 3 rails回调和顺序或事件,ruby-on-rails-3,callback,sidekiq,Ruby On Rails 3,Callback,Sidekiq,我在创建记录时有三个不同的API调用 1) 调用为记录创建位URL。 2) 通过bitly URL呼叫发送到facebook 3) 使用bitly URL调用发布到twitter 我目前的程序如下: 记录创建和更新 respond_to do |format| if @dealer.save call_bitly_api end end 在我的模型中: after_save :social_media_posting def social_media_posting if
respond_to do |format|
if @dealer.save
call_bitly_api
end
end
在我的模型中:
after_save :social_media_posting
def social_media_posting
if (self.bitly_url.present? && self.posted_to_facebook == false)
call_facebook_post
end
if (self.bitly_url.present? && self.posted_to_twitter == false)
call_twitter_post
end
end
我面临的问题是,facebook和twitter帖子在第一次保存时被调用,而bitly_url尚未创建
需要帮助的是,找出如何添加这些仍可能发生但仍按顺序发生的呼叫,并等待bitly_url出现,然后再呼叫facebook和twitter?值得一提的是,我正在使用sidekiq进行呼叫,并将实际呼叫发送给sidekiq工作人员,以便在后台工作。在控制器中:
CallBitlyWorker.perform_async(dealer.id)
在您的工作人员中:
class CallBitlyWorker
include Sidekiq::Worker
def perform(dealer_id)
dealer = Dealer.find(dealer_id)
# make bitly call
dealer.update_attribute(:bitly_url, some_url)
# make the social media calls in parallel
CallFacebook.perform_async(dealer_id)
CallTwitter.perform_async(dealer_id)
end
end
应尽可能避免ActiveRecord回调。他们只是让你的代码更加不透明。如果sidekiq员工因任何原因失败,它会不会一次又一次地调用facebook和twitter帖子?这取决于你如何编写工作。按照你的建议进行操作。到目前为止,这似乎是可行的。谢谢你的帮助。