1

delayed_job_active_record用来安排我的应用程序的报价在 FB 上发布和分享。创建的报价可以有 3 种状态:

  • 草稿只保存在哪里
  • 已发布以直接在我的应用程序和 twitter 和 facebook 上发布
  • 计划由delayed_job 接收并在我在表格中定义的稍后时间和日期发布。

这是我quote.rb使用当前逻辑的模型:

  # == Schema Information
  #
  # Table name: quotes
  #
  #  id           :integer          not null, primary key
  #  content      :text
  #  author       :string
  #  created_at   :datetime         not null
  #  updated_at   :datetime         not null
  #  published_at :datetime
  #  status       :string
  #  facebook     :boolean
  #  twitter      :boolean
  #  user_id      :integer
  #  error        :text
  #

  class Quote < ActiveRecord::Base

    belongs_to :user
    validates :content, presence: true
    validates :author, presence: true
    # validation using validates_timeliness gem
    # validates_datetime :published_at, :on => :create, :on_or_after => Time.zone.now
    # validates_datetime :published_at, :on => :update, :on_or_after => Time.zone.now
    after_save :schedule

    scope :draft,     ->{ where(status: "Draft") }
    scope :published, ->{ where(status: "Published") }
    scope :scheduled, ->{ where(status: "Scheduled") }

    before_validation :clean_up_status

    def clean_up_status
      self.published_at = case status
                          when "Draft"
                            nil
                          when "Published"
                            Time.zone.now
                          else
                            published_at
                          end
      true
    end

    def schedule
      puts "!!!!!!!!!!!!!!!#{self.status}!!!!!!!!!!!!!!!"
      if self.status == "Scheduled"
        begin
            ScheduleJob.set(wait_until: published_at).perform_later(self)
        rescue Exception => e
            self.update_attributes(status: "Scheduling-error", error: e.message)
        end
      else
        publish
      end
    end

    def publish
      unless self.status == "Draft"
        begin
            if self.facebook
                to_facebook
            end
            if self.twitter
                to_twitter
            end
          self.update_attributes(status: "Published")
        rescue Exception => e
          self.update_attributes(status: "Publishing-error", error: e.message)
        end
      end
    end

    def to_twitter
      client = Twitter::REST::Client.new do |config|
        config.consumer_key        = ENV['TWITTER_KEY']
        config.consumer_secret     = ENV['TWITTER_SECRET']
        config.access_token        = self.user.twitter.oauth_token
        config.access_token_secret = self.user.twitter.secret
      end
      client.update(self.content)
    end

    def to_facebook
      graph = Koala::Facebook::API.new(self.user.facebook.oauth_token)
      graph.put_connections("me", "feed", message: self.content)
    end
  end

我已经尝试过before_create :schedule,但它们似乎都不起作用。before_update :schedulebefore_save

我的jobs/schedule_job.rb

class ScheduleJob < ActiveJob::Base
 queue_as :default

 def perform(quote)
   quote.publish
 end
end

使用基本配置initializers/delayed_job_config.rb

Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.max_attempts = 1

每当我安排报价时,delayed_job 进入无限循环,最终发布报价并继续循环运行。

这是日志:

rake jobs:work
[Worker(host:Christophes-MacBook-Pro.local pid:8962)] Starting job worker
[Worker(host:Christophes-MacBook-Pro.local pid:8962)] Job ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper (id=7) RUNNING
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Published!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!Publishing-error!!!!!!!!!!!!!!!

知道我会错过什么吗?我将 Puma 用于开发和生产服务器。

4

1 回答 1

0

奇怪的是,没有人为您回答这个问题,但似乎问题正在发生,因为您在回调中触发了相同模型的进一步保存。然后这些保存将再次调用回调,这将再次触发保存等等。

您实际上只能update_column在不会触发回调的回调中使用 etc... 之类的东西,或者在模型中添加一些更复杂的逻辑来检查您是否应该运行某个回调。

于 2021-11-16T00:20:35.597 回答