From bda20cc44a52a4c80d169cc6c8b3b71dbb98aaf3 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 13 Apr 2017 12:02:35 +0800 Subject: [PATCH] FIX: Don't enqueue topic status update job if topic is deleted. --- app/models/topic_status_update.rb | 8 ++++++-- spec/models/topic_status_update_spec.rb | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/models/topic_status_update.rb b/app/models/topic_status_update.rb index c8461bd2b77..d5e59c67373 100644 --- a/app/models/topic_status_update.rb +++ b/app/models/topic_status_update.rb @@ -23,7 +23,7 @@ class TopicStatusUpdate < ActiveRecord::Base end after_save do - if (execute_at_changed? || user_id_changed?) && topic + if (execute_at_changed? || user_id_changed?) now = Time.zone.now time = execute_at < now ? now : execute_at @@ -40,7 +40,9 @@ class TopicStatusUpdate < ActiveRecord::Base end 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( "schedule_auto_#{self.types[topic_status_update.status_type]}_job", topic_status_update.execute_at @@ -76,6 +78,7 @@ class TopicStatusUpdate < ActiveRecord::Base end def schedule_auto_open_job(time) + return unless topic topic.update_status('closed', true, user) if !topic.closed Jobs.enqueue_at(time, :toggle_topic_closed, @@ -85,6 +88,7 @@ class TopicStatusUpdate < ActiveRecord::Base end def schedule_auto_close_job(time) + return unless topic topic.update_status('closed', false, user) if topic.closed Jobs.enqueue_at(time, :toggle_topic_closed, diff --git a/spec/models/topic_status_update_spec.rb b/spec/models/topic_status_update_spec.rb index 6880ddc2ec9..7b246b88196 100644 --- a/spec/models/topic_status_update_spec.rb +++ b/spec/models/topic_status_update_spec.rb @@ -226,6 +226,11 @@ RSpec.describe TopicStatusUpdate, type: :model do 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! } .to change { Jobs::ToggleTopicClosed.jobs.count }.by(2)