我正在使用 resque-scheduler 将帖子安排到 Twitter API。在我的数据库中,我有一个SocialMediaPost
带有查询 API 的方法的模型,post_tweet
然后使用推文的 id 更新记录。当我在 resque-scheduler 之外运行此代码时,它按预期工作。但是,当我排队发帖时,我的if/else
块中的所有内容似乎都被跳过了。如果我转到我的 Twitter 帐户,我可以看到发布成功,但是当我检查数据库条目时,没有twitter_id
, twitter_uid
, 或twitter_link
条目。
这是我的SocialMediaPost
模型:
class SocialMediaPost < ActiveRecord::Base
require 'open-uri'
require 'postman'
belongs_to :company
belongs_to :employer
belongs_to :identity
validates :post, length: {in: 0..140}, if: Proc.new { |t| t.twitter }
validates :post, length: {in: 0..256}, if: Proc.new { |t| t.linkedin }
after_commit :enqueue
def enqueue
time = scheduled_time.in_time_zone("Eastern Time (US & Canada)")
options = {
id: self.id,
services: JSON.parse(self.services),
posts: JSON.parse(self.posts),
attachment_url: self.attachment_url,
media_url: self.media_url,
time: scheduled_time.in_time_zone("Eastern Time (US & Canada)")
}
Resque.enqueue_at(time, Postman, options)
end
def post_tweet
tweet = company.twitter_client.update(JSON.parse(posts)['twitter'])
if tweet.created?
uid = self.company.identities.find_by(provider: :twitter).uid
self.update_columns(twitter_id: tweet.id, twitter_uid: uid, twitter_link: "https://twitter.com/#{uid}/status/#{self.twitter_id}")
end
tweet.created?
end
end
这是我的resque工人Postman
:
class Postman
@queue = :social_media_posts
def self.perform(options)
post = SocialMediaPost.find(options['id'])
post.post_tweet if options['services']['twitter'] && options['attachment_url'].blank?
post.post_tweet_with_media if options['services']['twitter'] && options['attachment_url'].present?
post.post_linkedin_status if options['services']['linkedin']
end
end
我尝试binding.pry
在几个位置插入 a ,但 resque 跳过了绑定。请注意,我使用该update_columns
方法来规避创建无限循环的错误。