FIX: Clean up topic_timers when no longer valid.

This was causing a bug where the jobs for the topic_timers will keep
getting enqueued over and over again.
This commit is contained in:
Guo Xiang Tan 2020-08-26 12:18:33 +08:00
parent 1e8f216e17
commit 5bca1aec48
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
2 changed files with 29 additions and 11 deletions

View File

@ -6,11 +6,12 @@ module Jobs
topic_timer = TopicTimer.find_by(id: args[:topic_timer_id] || args[:topic_status_update_id])
state = !!args[:state]
if topic_timer.blank? ||
topic_timer.execute_at > Time.zone.now ||
(topic = topic_timer.topic).blank? ||
topic.closed == state
if topic_timer.blank? || topic_timer.execute_at > Time.zone.now
return
end
if (topic = topic_timer.topic).blank? || topic.closed == state
topic_timer.destroy!
return
end

View File

@ -72,16 +72,33 @@ describe Jobs::ToggleTopicClosed do
end
end
describe 'when trying to close a topic that has already been closed' do
it 'should delete the topic timer' do
freeze_time(topic.public_topic_timer.execute_at + 1.minute)
topic.update!(closed: true)
expect do
described_class.new.execute(
topic_timer_id: topic.public_topic_timer.id,
state: true
)
end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)
end
end
describe 'when trying to close a topic that has been deleted' do
it 'should not do anything' do
it 'should delete the topic timer' do
freeze_time(topic.public_topic_timer.execute_at + 1.minute)
topic.trash!
Topic.any_instance.expects(:update_status).never
described_class.new.execute(
topic_timer_id: topic.public_topic_timer.id,
state: true
)
expect do
described_class.new.execute(
topic_timer_id: topic.public_topic_timer.id,
state: true
)
end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)
end
end