discourse/app/jobs/regular/open_topic.rb
Martin Brennan 0034cbda8a
DEV: Change Topic Timer from enqueue_at scheduled jobs to incrementally executed jobs (#11698)
Moves the topic timer jobs from being scheduled ahead of time with enqueue_at to a 5 minute scheduled run like bookmark reminders, in a new job called Jobs::EnqueueTopicTimers. Backwards compatibility is maintained by checking if an existing topic timer job is enqueued in sidekiq for the timer, and if it is not running it inside the new job.

The functionality to close/open a topic if it is in the opposite state still remains in the after_save block of TopicTimer, with further commentary, which is used for Open/Close Temporarily.

This also removes the ensure_consistency! functionality of topic timers as it is no longer needed; the new job will always pick up the timers because they are not stored in a fragile state of sidekiq.
2021-01-19 13:30:58 +10:00

37 lines
1.1 KiB
Ruby

# frozen_string_literal: true
module Jobs
class OpenTopic < ::Jobs::TopicTimerBase
def execute_timer_action(topic_timer, topic)
user = topic_timer.user
if !Guardian.new(user).can_open_topic?(topic) || topic.open?
topic_timer.destroy!
topic.reload
topic.inherit_auto_close_from_category(timer_type: :close)
return
end
# guards against reopening a topic too early if the topic has
# been auto closed because of reviewables/reports, this will
# just update the existing topic timer and push it down the line
if topic.auto_close_threshold_reached?
topic.set_or_create_timer(
TopicTimer.types[:open],
SiteSetting.num_hours_to_close_topic,
by_user: Discourse.system_user
)
else
# autoclosed, false is just another way of saying open.
# this handles deleting the topic timer as wel, see TopicStatusUpdater
topic.update_status('autoclosed', false, user)
end
topic.inherit_auto_close_from_category(timer_type: :close)
end
end
end