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:
evazion
2021-09-26 20:05:39 -05:00
parent 7d3e491dc6
commit 52bf4a3a6b
21 changed files with 227 additions and 70 deletions

View File

@@ -0,0 +1,6 @@
# A job that runs daily to export all tables to BigQuery. Spawned by {DanbooruMaintenance}.
class BigqueryExportAllJob < ApplicationJob
def perform
BigqueryExportService.async_export_all!
end
end

View File

@@ -0,0 +1,7 @@
# A job that runs weekly to warn inactive approvers before they're demoted.
# Spawned by {DanbooruMaintenance}.
class DmailInactiveApproversJob < ApplicationJob
def perform
ApproverPruner.dmail_inactive_approvers!
end
end

View File

@@ -0,0 +1,7 @@
# A job that runs monthly to demote all inactive approvers. Spawned by
# {DanbooruMaintenance}.
class PruneApproversJob < ApplicationJob
def perform
ApproverPruner.prune!
end
end

View File

@@ -0,0 +1,7 @@
# A job that runs daily to remove expired bans. Spawned by
# {DanbooruMaintenance}.
class PruneBansJob < ApplicationJob
def perform
Ban.prune!
end
end

View File

@@ -0,0 +1,8 @@
# A job that runs daily to reject expired bulk update requests. Spawned by
# {DanbooruMaintenance}.
class PruneBulkUpdateRequestsJob < ApplicationJob
def perform
BulkUpdateRequestPruner.warn_old
BulkUpdateRequestPruner.reject_expired
end
end

View File

@@ -0,0 +1,7 @@
# A job that runs daily to delete all stale delayed jobs. Spawned by
# {DanbooruMaintenance}.
class PruneDelayedJobsJob < ApplicationJob
def perform
Delayed::Job.where("created_at < ?", 45.days.ago).delete_all
end
end

View File

@@ -0,0 +1,7 @@
# A job that runs daily to remove old post disapprovals. Spawned by
# {DanbooruMaintenance}.
class PrunePostDisapprovalsJob < ApplicationJob
def perform
PostDisapproval.prune!
end
end

View File

@@ -0,0 +1,7 @@
# A job that runs hourly to delete all expired pending, flagged, and appealed
# posts. Spawned by {DanbooruMaintenance}.
class PrunePostsJob < ApplicationJob
def perform
PostPruner.prune!
end
end

View File

@@ -0,0 +1,7 @@
# A job that runs hourly to delete all state rate limit objects from the
# database. Spawned by {DanbooruMaintenance}.
class PruneRateLimitsJob < ApplicationJob
def perform
RateLimit.prune!
end
end

View File

@@ -0,0 +1,7 @@
# A job that runs hourly to delete all completed, stale, or failed uploads.
# Spawned by {DanbooruMaintenance}.
class PruneUploadsJob < ApplicationJob
def perform
Upload.prune!
end
end

View File

@@ -0,0 +1,10 @@
# A job that runs hourly to fix all incorrect tag counts.
# Spawned by {DanbooruMaintenance}.
class RegeneratePostCountsJob < ApplicationJob
def perform
updated_tags = Tag.regenerate_post_counts!
updated_tags.each do |tag|
DanbooruLogger.info("Updated tag count", tag.attributes)
end
end
end

View File

@@ -0,0 +1,7 @@
# A job that runs weekly to retire inactive aliases and implications. Spawned
# by {DanbooruMaintenance}.
class RetireTagRelationshipsJob < ApplicationJob
def perform
TagRelationshipRetirementService.find_and_retire!
end
end

View File

@@ -0,0 +1,8 @@
# A job that runs daily to vacuum the database. Spawned by {DanbooruMaintenance}.
class VacuumDatabaseJob < ApplicationJob
def perform
# We can't perform vacuum inside a transaction. This happens during tests.
return if ApplicationRecord.connection.transaction_open?
ApplicationRecord.connection.execute("vacuum analyze")
end
end

View File

@@ -2,45 +2,34 @@ module DanbooruMaintenance
module_function
def hourly
safely { Upload.prune! }
safely { PostPruner.prune! }
safely { RateLimit.prune! }
safely { regenerate_post_counts! }
queue PruneUploadsJob
queue PrunePostsJob
queue PruneRateLimitsJob
queue RegeneratePostCountsJob
end
def daily
safely { Delayed::Job.where('created_at < ?', 45.days.ago).delete_all }
safely { PostDisapproval.prune! }
safely { BulkUpdateRequestPruner.warn_old }
safely { BulkUpdateRequestPruner.reject_expired }
safely { Ban.prune! }
safely { BigqueryExportService.async_export_all! }
safely { ActiveRecord::Base.connection.execute("vacuum analyze") unless Rails.env.test? }
queue PruneDelayedJobsJob
queue PrunePostDisapprovalsJob
queue PruneBulkUpdateRequestsJob
queue PruneBansJob
queue BigqueryExportAllJob
queue VacuumDatabaseJob
end
def weekly
safely { TagRelationshipRetirementService.find_and_retire! }
safely { ApproverPruner.dmail_inactive_approvers! }
queue RetireTagRelationshipsJob
queue DmailInactiveApproversJob
end
def monthly
safely { ApproverPruner.prune! }
queue PruneApproversJob
end
def regenerate_post_counts!
updated_tags = Tag.regenerate_post_counts!
updated_tags.each do |tag|
DanbooruLogger.info("Updated tag count", tag.attributes)
end
end
def safely(&block)
ActiveRecord::Base.connection.execute("set statement_timeout = 0")
CurrentUser.scoped(User.system, "127.0.0.1") do
yield
end
rescue StandardError => exception
def queue(job)
DanbooruLogger.info("Queueing #{job.name}")
job.perform_later
rescue Exception # rubocop:disable Lint/RescueException
DanbooruLogger.log(exception)
raise exception if Rails.env.test?
end