maintenance: break maintenance tasks into individual jobs.
Break the hourly/daily/weekly/monthly maintenance tasks down into individual delayed jobs. This way if one task fails, it won't prevent other tasks from running. Also, jobs can be run in parallel, and can be individually retried if they fail.
This commit is contained in:
18
test/jobs/prune_bans_job_test.rb
Normal file
18
test/jobs/prune_bans_job_test.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PruneBansJobTest < ActiveJob::TestCase
|
||||
context "PruneBansJob" do
|
||||
should "prune all expired bans" do
|
||||
@expired_ban = travel_to(1.month.ago) { create(:ban, duration: 1.week) }
|
||||
@unexpired_ban = create(:ban, duration: 1.week)
|
||||
|
||||
assert_equal(true, @expired_ban.user.is_banned?)
|
||||
assert_equal(true, @unexpired_ban.user.is_banned?)
|
||||
|
||||
PruneBansJob.perform_now
|
||||
|
||||
assert_equal(false, @expired_ban.user.reload.is_banned?)
|
||||
assert_equal(true, @unexpired_ban.user.reload.is_banned?)
|
||||
end
|
||||
end
|
||||
end
|
||||
22
test/jobs/prune_posts_job_test.rb
Normal file
22
test/jobs/prune_posts_job_test.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PrunePostsJobTest < ActiveJob::TestCase
|
||||
context "PrunePostsJob" do
|
||||
should "prune expired posts" do
|
||||
@pending = create(:post, is_pending: true, created_at: 5.days.ago)
|
||||
@flagged = create(:post, is_flagged: true, created_at: 5.days.ago)
|
||||
@appealed = create(:post, is_deleted: true, created_at: 5.days.ago)
|
||||
|
||||
@flag = create(:post_flag, post: @flagged, created_at: 4.days.ago)
|
||||
@appeal = create(:post_appeal, post: @appealed, created_at: 4.days.ago)
|
||||
|
||||
PrunePostsJob.perform_now
|
||||
|
||||
assert_equal(true, @pending.reload.is_deleted?)
|
||||
assert_equal(true, @flagged.reload.is_deleted?)
|
||||
assert_equal(true, @appealed.reload.is_deleted?)
|
||||
assert_equal(true, @flag.reload.succeeded?)
|
||||
assert_equal(true, @appeal.reload.rejected?)
|
||||
end
|
||||
end
|
||||
end
|
||||
13
test/jobs/prune_rate_limits_job_test.rb
Normal file
13
test/jobs/prune_rate_limits_job_test.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PruneRateLimitsJobTest < ActiveJob::TestCase
|
||||
context "PruneRateLimitsJob" do
|
||||
should "prune all stale rate limits" do
|
||||
travel_to(2.hours.ago) { create(:rate_limit) }
|
||||
|
||||
assert_equal(1, RateLimit.count)
|
||||
PruneRateLimitsJob.perform_now
|
||||
assert_equal(0, RateLimit.count)
|
||||
end
|
||||
end
|
||||
end
|
||||
19
test/jobs/prune_uploads_job_test.rb
Normal file
19
test/jobs/prune_uploads_job_test.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PruneUploadsJobTest < ActiveJob::TestCase
|
||||
context "PruneUploadsJob" do
|
||||
should "prune all old uploads" do
|
||||
@uploader = create(:user)
|
||||
|
||||
as(@uploader) do
|
||||
@completed_upload = travel_to(2.hours.ago) { create(:upload, uploader: @uploader, status: "completed") }
|
||||
@stale_upload = travel_to(2.days.ago) { create(:upload, uploader: @uploader, status: "preprocessed") }
|
||||
@failed_upload = travel_to(4.days.ago) { create(:upload, uploader: @uploader, status: "error") }
|
||||
end
|
||||
|
||||
assert_equal(3, Upload.count)
|
||||
PruneUploadsJob.perform_now
|
||||
assert_equal(0, Upload.count)
|
||||
end
|
||||
end
|
||||
end
|
||||
18
test/jobs/regenerate_post_counts_job_test.rb
Normal file
18
test/jobs/regenerate_post_counts_job_test.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
require 'test_helper'
|
||||
|
||||
class RegeneratePostCountsJobTest < ActiveJob::TestCase
|
||||
context "RegeneratePostCountsJob" do
|
||||
should "regenerate all incorrect tag post counts" do
|
||||
tag1 = create(:tag, name: "touhou", post_count: -10)
|
||||
tag2 = create(:tag, name: "bkub", post_count: 10)
|
||||
tag3 = create(:tag, name: "chen", post_count: 10)
|
||||
post = create(:post, tag_string: "touhou bkub")
|
||||
|
||||
RegeneratePostCountsJob.perform_now
|
||||
|
||||
assert_equal(1, Tag.find_by_name!("touhou").post_count)
|
||||
assert_equal(1, Tag.find_by_name!("bkub").post_count)
|
||||
assert_equal(0, Tag.find_by_name!("chen").post_count)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user