add logic that auto closes any topics that missed the queued job (due to redis flush or whatever)

This commit is contained in:
Sam 2013-11-11 10:52:44 +11:00
parent a9c5d843f7
commit d1d661f6e1
4 changed files with 30 additions and 6 deletions

View file

@ -2,12 +2,9 @@ module Jobs
class CloseTopic < Jobs::Base class CloseTopic < Jobs::Base
def execute(args) def execute(args)
topic = Topic.where(id: args[:topic_id]).first if topic = Topic.where(id: args[:topic_id]).first
if topic and topic.auto_close_at and !topic.closed? and !topic.deleted_at
closer = User.where(id: args[:user_id]).first closer = User.where(id: args[:user_id]).first
if Guardian.new(closer).can_moderate?(topic) topic.auto_close(closer)
topic.update_status('autoclosed', true, closer)
end
end end
end end

View file

@ -25,6 +25,8 @@ module Jobs
# Refresh Hot Topics # Refresh Hot Topics
HotTopic.refresh! HotTopic.refresh!
# Automatically close stuff that we missed
Topic.auto_close
end end
end end

View file

@ -599,6 +599,21 @@ class Topic < ActiveRecord::Base
set_auto_close(num_days) set_auto_close(num_days)
end end
def self.auto_close
Topic.where("NOT closed AND auto_close_at < ? AND auto_close_user_id IS NOT NULL", 5.minutes.from_now).each do |t|
t.auto_close
end
end
def auto_close(closer = nil)
if auto_close_at && !closed? && !deleted_at && auto_close_at < 5.minutes.from_now
closer ||= auto_close_user
if Guardian.new(closer).can_moderate?(self)
update_status('autoclosed', true, closer)
end
end
end
def set_auto_close(num_days, by_user=nil) def set_auto_close(num_days, by_user=nil)
num_days = num_days.to_i num_days = num_days.to_i
self.auto_close_at = (num_days > 0 ? num_days.days.from_now : nil) self.auto_close_at = (num_days > 0 ? num_days.days.from_now : nil)

View file

@ -1051,7 +1051,17 @@ describe Topic do
topic = Fabricate(:topic, category: Fabricate(:category, auto_close_days: 14), user: mod) topic = Fabricate(:topic, category: Fabricate(:category, auto_close_days: 14), user: mod)
Jobs.expects(:enqueue_at).with(12.hours.from_now, :close_topic, has_entries(topic_id: topic.id, user_id: topic.user_id)) Jobs.expects(:enqueue_at).with(12.hours.from_now, :close_topic, has_entries(topic_id: topic.id, user_id: topic.user_id))
topic.auto_close_at = 12.hours.from_now topic.auto_close_at = 12.hours.from_now
topic.save.should be_true topic.save
topic.reload
topic.closed.should == false
Timecop.freeze(24.hours.from_now) do
Topic.auto_close
topic.reload
topic.closed.should == true
end
end end
end end
end end