mirror of
https://github.com/discourse/discourse.git
synced 2024-11-24 06:15:28 +08:00
0034cbda8a
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.
51 lines
1.9 KiB
Ruby
51 lines
1.9 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'rails_helper'
|
|
|
|
RSpec.describe Jobs::TopicTimerEnqueuer do
|
|
subject { described_class.new }
|
|
|
|
fab!(:timer1) do
|
|
Fabricate(:topic_timer, execute_at: 1.minute.ago, created_at: 1.hour.ago, status_type: TopicTimer.types[:close])
|
|
end
|
|
fab!(:timer2) do
|
|
Fabricate(:topic_timer, execute_at: 1.minute.ago, created_at: 1.hour.ago, status_type: TopicTimer.types[:open])
|
|
end
|
|
fab!(:future_timer) do
|
|
Fabricate(:topic_timer, execute_at: 1.hours.from_now, created_at: 1.hour.ago, status_type: TopicTimer.types[:close])
|
|
end
|
|
fab!(:deleted_timer) do
|
|
Fabricate(:topic_timer, execute_at: 1.minute.ago, created_at: 1.hour.ago, status_type: TopicTimer.types[:close])
|
|
end
|
|
|
|
before do
|
|
deleted_timer.trash!
|
|
end
|
|
|
|
it "does not enqueue deleted timers" do
|
|
expect_not_enqueued_with(job: :close_topic, args: { topic_timer_id: deleted_timer.id })
|
|
subject.execute
|
|
expect(deleted_timer.topic.reload.closed?).to eq(false)
|
|
end
|
|
|
|
it "does not enqueue future timers" do
|
|
expect_not_enqueued_with(job: :close_topic, args: { topic_timer_id: future_timer.id })
|
|
subject.execute
|
|
expect(future_timer.topic.reload.closed?).to eq(false)
|
|
end
|
|
|
|
it "enqueues the related job" do
|
|
expect_not_enqueued_with(job: :close_topic, args: { topic_timer_id: deleted_timer.id })
|
|
expect_not_enqueued_with(job: :close_topic, args: { topic_timer_id: future_timer.id })
|
|
subject.execute
|
|
expect_job_enqueued(job: :close_topic, args: { topic_timer_id: timer1.id })
|
|
expect_job_enqueued(job: :open_topic, args: { topic_timer_id: timer2.id })
|
|
end
|
|
|
|
it "does not re-enqueue a job that has already been scheduled ahead of time in sidekiq (legacy topic timers)" do
|
|
expect_not_enqueued_with(job: :close_topic, args: { topic_timer_id: timer1.id })
|
|
Jobs.enqueue_at(1.hours.from_now, :close_topic, topic_timer_id: timer1.id)
|
|
subject.execute
|
|
end
|
|
end
|