FIX: Don't enqueue topic status update job if topic is deleted.

This commit is contained in:
Guo Xiang Tan 2017-04-13 12:02:35 +08:00
parent ee449b0dd5
commit bda20cc44a
2 changed files with 11 additions and 2 deletions

View File

@ -23,7 +23,7 @@ class TopicStatusUpdate < ActiveRecord::Base
end end
after_save do after_save do
if (execute_at_changed? || user_id_changed?) && topic if (execute_at_changed? || user_id_changed?)
now = Time.zone.now now = Time.zone.now
time = execute_at < now ? now : execute_at time = execute_at < now ? now : execute_at
@ -40,7 +40,9 @@ class TopicStatusUpdate < ActiveRecord::Base
end end
def self.ensure_consistency! def self.ensure_consistency!
TopicStatusUpdate.where("execute_at < ?", Time.zone.now).find_each do |topic_status_update| TopicStatusUpdate.where("topic_status_updates.execute_at < ?", Time.zone.now)
.find_each do |topic_status_update|
topic_status_update.send( topic_status_update.send(
"schedule_auto_#{self.types[topic_status_update.status_type]}_job", "schedule_auto_#{self.types[topic_status_update.status_type]}_job",
topic_status_update.execute_at topic_status_update.execute_at
@ -76,6 +78,7 @@ class TopicStatusUpdate < ActiveRecord::Base
end end
def schedule_auto_open_job(time) def schedule_auto_open_job(time)
return unless topic
topic.update_status('closed', true, user) if !topic.closed topic.update_status('closed', true, user) if !topic.closed
Jobs.enqueue_at(time, :toggle_topic_closed, Jobs.enqueue_at(time, :toggle_topic_closed,
@ -85,6 +88,7 @@ class TopicStatusUpdate < ActiveRecord::Base
end end
def schedule_auto_close_job(time) def schedule_auto_close_job(time)
return unless topic
topic.update_status('closed', false, user) if topic.closed topic.update_status('closed', false, user) if topic.closed
Jobs.enqueue_at(time, :toggle_topic_closed, Jobs.enqueue_at(time, :toggle_topic_closed,

View File

@ -226,6 +226,11 @@ RSpec.describe TopicStatusUpdate, type: :model do
Fabricate(:topic_status_update) Fabricate(:topic_status_update)
Fabricate(:topic_status_update,
execute_at: Time.zone.now - 1.hour,
created_at: Time.zone.now - 2.hour
).topic.trash!
expect { described_class.ensure_consistency! } expect { described_class.ensure_consistency! }
.to change { Jobs::ToggleTopicClosed.jobs.count }.by(2) .to change { Jobs::ToggleTopicClosed.jobs.count }.by(2)