From 950bc608c2380702d61c5752f9a032cbec0a297f Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 6 Oct 2021 04:14:23 -0500 Subject: [PATCH] maintenance: add job to check for database corruption. Add a job to run pg_amcheck hourly to check for corrupt database indexes. https://www.postgresql.org/docs/14/app-pgamcheck.html --- app/jobs/amcheck_database_job.rb | 18 ++++++++++++++++++ app/logical/danbooru_maintenance.rb | 1 + test/jobs/amcheck_database_job_test.rb | 9 +++++++++ 3 files changed, 28 insertions(+) create mode 100644 app/jobs/amcheck_database_job.rb create mode 100644 test/jobs/amcheck_database_job_test.rb diff --git a/app/jobs/amcheck_database_job.rb b/app/jobs/amcheck_database_job.rb new file mode 100644 index 000000000..184b82ff3 --- /dev/null +++ b/app/jobs/amcheck_database_job.rb @@ -0,0 +1,18 @@ +# A job that runs hourly to check the database for corruption. Spawned by {DanbooruMaintenance}. +# Requires at least PostgreSQL 14.0 to be installed for pg_amcheck to be available. +# +# https://www.postgresql.org/docs/14/app-pgamcheck.html +class AmcheckDatabaseJob < ApplicationJob + def perform(options: "--verbose --install-missing --heapallindexed --parent-check") + return unless system("pg_amcheck --version") + + connection_url = ApplicationRecord.connection_db_config.url + output = %x(PGDATABASE="#{connection_url}" pg_amcheck #{options}) + notify(output) unless $?.success? + end + + def notify(output) + DanbooruLogger.info(output) + Dmail.create_automated(to: User.owner, title: "pg_amcheck failed", body: output) + end +end diff --git a/app/logical/danbooru_maintenance.rb b/app/logical/danbooru_maintenance.rb index d77fd9862..6bd12a5a2 100644 --- a/app/logical/danbooru_maintenance.rb +++ b/app/logical/danbooru_maintenance.rb @@ -6,6 +6,7 @@ module DanbooruMaintenance queue PrunePostsJob queue PruneRateLimitsJob queue RegeneratePostCountsJob + queue AmcheckDatabaseJob end def daily diff --git a/test/jobs/amcheck_database_job_test.rb b/test/jobs/amcheck_database_job_test.rb new file mode 100644 index 000000000..b806d8e22 --- /dev/null +++ b/test/jobs/amcheck_database_job_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class AmcheckDatabaseJobTest < ActiveJob::TestCase + context "AmcheckDatabaseJob" do + should "work" do + AmcheckDatabaseJob.perform_now + end + end +end