Resque Scheduler未按预期更新模型

时间:2015-11-09 01:39:40

标签: ruby-on-rails ruby-on-rails-4 activerecord resque resque-scheduler

我正在使用resque-scheduler来安排Twitter API的帖子。在我的数据库中,我有一个SocialMediaPost模型,其中post_tweet方法查询API,然后使用推文的ID更新记录。当我在resque-scheduler之外运行此代码时,它按预期工作。当我排队一个帖子时,if/else块中的所有内容似乎都被跳过了。如果我转到我的Twitter帐户,我可以看到帖子是成功的,但是当我检查数据库条目时,条目没有twitter_idtwitter_uidtwitter_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,但resque会跳过绑定。请注意,我使用update_columns方法来绕过创建无限循环的this bug

0 个答案:

没有答案