我正在使用delayed_job_active_record
安排我的应用程序的引用,以便在FB上发布和分享。
创建的报价可以有3种状态:
这是我的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 :schedule
和before_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 for dev和prod服务器。