Skip to content

Commit 76f5655

Browse files
committed
added job for cleaning up duplicate thread followers
1 parent 8f5b894 commit 76f5655

6 files changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class CleanUpThreadFollowersJob < ApplicationJob
2+
queue_as :default
3+
4+
def perform
5+
sql = File.read(Rails.root.join('db/scripts/threads_with_duplicate_followers.sql'))
6+
threads = ActiveRecord::Base.connection.execute(sql).to_a
7+
8+
threads.each do |thread|
9+
user_id, thread_id = thread
10+
11+
followers = ThreadFollower.where(comment_thread_id: thread_id, user_id: user_id)
12+
13+
next unless followers.many?
14+
15+
duplicate = followers.first
16+
result = duplicate.destroy
17+
18+
unless result
19+
puts "failed to destroy thread follower duplicate \"#{duplicate.id}\""
20+
duplicate.errors.each { |e| puts e.full_message }
21+
end
22+
end
23+
end
24+
end

config/schedule.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
runner 'scripts/run_complaints_closure.rb'
2727
end
2828

29+
every 7.days, at: '02:35' do
30+
runner 'scripts/run_thread_followers_cleanup.rb'
31+
end
32+
2933
every 6.hours do
3034
runner 'scripts/recalc_abilities.rb'
3135
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
select
2+
user_id,
3+
comment_thread_id,
4+
count(*) as count
5+
from
6+
thread_followers
7+
where
8+
post_id is null
9+
group by
10+
user_id,
11+
comment_thread_id
12+
having
13+
count > 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CleanUpThreadFollowersJob.perform_later

test/fixtures/comment_threads.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,10 @@ without_activity:
8282
community: sample
8383
created_at: 2020-01-01T00:00:00.000000Z
8484
updated_at: 2020-01-01T00:00:00.000000Z
85+
86+
without_followers:
87+
title: Comment thread without any followers
88+
title: Comment thread without activity
89+
reply_count: 0
90+
post: question_one
91+
community: sample
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'test_helper'
2+
3+
class CleanUpThreadFollowersJobTest < ActiveJob::TestCase
4+
test 'should correctly clean up thread followers' do
5+
thread = comment_threads(:without_followers)
6+
std_usr = users(:standard_user)
7+
8+
ThreadFollower.create(comment_thread: thread, user: std_usr)
9+
ThreadFollower.create(comment_thread: thread, user: std_usr)
10+
assert_equal 2, ThreadFollower.where(comment_thread: thread, user: std_usr).size
11+
12+
perform_enqueued_jobs do
13+
CleanUpThreadFollowersJob.perform_later
14+
end
15+
16+
assert_performed_jobs 1
17+
assert_equal 1, ThreadFollower.where(comment_thread: thread, user: std_usr).size
18+
end
19+
end

0 commit comments

Comments
 (0)