From 5201954413425b23080501bbf588fb027d0be53b Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 7 Oct 2018 22:22:45 -0500 Subject: [PATCH 1/4] maintenance: consolidate maintenance tasks in logical/maintenance.rb. --- .../{daily_maintenance.rb => maintenance.rb} | 18 ++++++++++++++++-- app/logical/monthly_maintenance.rb | 5 ----- app/logical/weekly_maintenance.rb | 8 -------- config/schedule.rb | 15 +++------------ ...maintenance_test.rb => maintenance_test.rb} | 8 ++++---- 5 files changed, 23 insertions(+), 31 deletions(-) rename app/logical/{daily_maintenance.rb => maintenance.rb} (64%) delete mode 100644 app/logical/monthly_maintenance.rb rename test/unit/{daily_maintenance_test.rb => maintenance_test.rb} (86%) diff --git a/app/logical/daily_maintenance.rb b/app/logical/maintenance.rb similarity index 64% rename from app/logical/daily_maintenance.rb rename to app/logical/maintenance.rb index cc6f24090..98ef367e1 100644 --- a/app/logical/daily_maintenance.rb +++ b/app/logical/maintenance.rb @@ -1,5 +1,12 @@ -class DailyMaintenance - def run +module Maintenance + module_function + + def hourly + UploadErrorChecker.new.check! + DelayedJobErrorChecker.new.check! + end + + def daily ActiveRecord::Base.connection.execute("set statement_timeout = 0") PostPruner.new.prune! Upload.where('created_at < ?', 1.day.ago).delete_all @@ -18,4 +25,11 @@ class DailyMaintenance TagChangeRequestPruner.reject_all Ban.prune! end + + def weekly + ActiveRecord::Base.connection.execute("set statement_timeout = 0") + UserPasswordResetNonce.prune! + ApproverPruner.prune! + TagRelationshipRetirementService.find_and_retire! + end end diff --git a/app/logical/monthly_maintenance.rb b/app/logical/monthly_maintenance.rb deleted file mode 100644 index bbdf76408..000000000 --- a/app/logical/monthly_maintenance.rb +++ /dev/null @@ -1,5 +0,0 @@ -class MonthlyMaintenance - def run - ActiveRecord::Base.connection.execute("set statement_timeout = 0") - end -end diff --git a/app/logical/weekly_maintenance.rb b/app/logical/weekly_maintenance.rb index db19d0077..e69de29bb 100644 --- a/app/logical/weekly_maintenance.rb +++ b/app/logical/weekly_maintenance.rb @@ -1,8 +0,0 @@ -class WeeklyMaintenance - def run - ActiveRecord::Base.connection.execute("set statement_timeout = 0") - UserPasswordResetNonce.prune! - ApproverPruner.prune! - # JanitorPruner.new.prune! - end -end diff --git a/config/schedule.rb b/config/schedule.rb index 9bd6be94b..903d6b558 100644 --- a/config/schedule.rb +++ b/config/schedule.rb @@ -2,15 +2,11 @@ set :output, "/var/log/whenever.log" #env "MAILTO", "webmaster@danbooru.donmai.us" every 1.hour do - runner "UploadErrorChecker.new.check!" -end - -every 1.hour do - runner "DelayedJobErrorChecker.new.check!" + runner "Maintenance.hourly" end every 1.day do - runner "DailyMaintenance.new.run" + runner "Maintenance.daily" end every 1.day, :at => "1:00 am" do @@ -18,10 +14,5 @@ every 1.day, :at => "1:00 am" do end every 1.week, :at => "1:30 am" do - runner "WeeklyMaintenance.new.run" - runner "TagRelationshipRetirementService.find_and_retire!" -end - -every 1.month, :at => "2:00 am" do - runner "MonthlyMaintenance.new.run" + runner "Maintenance.weekly" end diff --git a/test/unit/daily_maintenance_test.rb b/test/unit/maintenance_test.rb similarity index 86% rename from test/unit/daily_maintenance_test.rb rename to test/unit/maintenance_test.rb index a0034f0c4..745ca787e 100644 --- a/test/unit/daily_maintenance_test.rb +++ b/test/unit/maintenance_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class DailyMaintenanceTest < ActiveSupport::TestCase +class MaintenanceTest < ActiveSupport::TestCase context "daily maintenance" do setup do # have ApiCacheGenerator save files to a temp dir. @@ -17,14 +17,14 @@ class DailyMaintenanceTest < ActiveSupport::TestCase end should "work" do - assert_nothing_raised { DailyMaintenance.new.run } + assert_nothing_raised { Maintenance.daily } end should "prune expired posts" do @pending = FactoryBot.create(:post, is_pending: true, created_at: 4.days.ago) @flagged = FactoryBot.create(:post, is_flagged: true, created_at: 4.days.ago) - DailyMaintenance.new.run + Maintenance.daily assert(true, @pending.reload.is_deleted) assert(true, @flagged.reload.is_deleted) @@ -38,7 +38,7 @@ class DailyMaintenanceTest < ActiveSupport::TestCase CurrentUser.as(banner) { FactoryBot.create(:ban, user: user, banner: banner, duration: 1) } assert_equal(true, user.reload.is_banned) - travel_to(2.days.from_now) { DailyMaintenance.new.run } + travel_to(2.days.from_now) { Maintenance.daily } assert_equal(false, user.reload.is_banned) end end From 6e54c37b04db683b86de078674012f3312eca1a5 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 7 Oct 2018 22:22:45 -0500 Subject: [PATCH 2/4] maintenance: add rake tasks for maintenance jobs. --- config/schedule.rb | 6 +++--- lib/tasks/maintenance.rake | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 lib/tasks/maintenance.rake diff --git a/config/schedule.rb b/config/schedule.rb index 903d6b558..23660acb9 100644 --- a/config/schedule.rb +++ b/config/schedule.rb @@ -2,11 +2,11 @@ set :output, "/var/log/whenever.log" #env "MAILTO", "webmaster@danbooru.donmai.us" every 1.hour do - runner "Maintenance.hourly" + rake "maintenance:hourly" end every 1.day do - runner "Maintenance.daily" + rake "maintenance:daily" end every 1.day, :at => "1:00 am" do @@ -14,5 +14,5 @@ every 1.day, :at => "1:00 am" do end every 1.week, :at => "1:30 am" do - runner "Maintenance.weekly" + rake "maintenance:weekly" end diff --git a/lib/tasks/maintenance.rake b/lib/tasks/maintenance.rake new file mode 100644 index 000000000..812103d4e --- /dev/null +++ b/lib/tasks/maintenance.rake @@ -0,0 +1,18 @@ +require "tasks/newrelic" if defined?(NewRelic) + +namespace :maintenance do + desc "Run hourly maintenance jobs" + task hourly: :environment do + Maintenance.hourly + end + + desc "Run daily maintenance jobs" + task daily: :environment do + Maintenance.daily + end + + desc "Run weekly maintenance jobs" + task weekly: :environment do + Maintenance.weekly + end +end From 5e57cccd18f3a3c4d896a8a301582e93d8dd9a18 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 7 Oct 2018 22:22:45 -0500 Subject: [PATCH 3/4] maintenance: log errors to rails log and newrelic. --- app/logical/maintenance.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/logical/maintenance.rb b/app/logical/maintenance.rb index 98ef367e1..38382bd17 100644 --- a/app/logical/maintenance.rb +++ b/app/logical/maintenance.rb @@ -4,6 +4,8 @@ module Maintenance def hourly UploadErrorChecker.new.check! DelayedJobErrorChecker.new.check! + rescue Exception => exception + rescue_exception(exception) end def daily @@ -24,6 +26,8 @@ module Maintenance TagChangeRequestPruner.warn_all TagChangeRequestPruner.reject_all Ban.prune! + rescue Exception => exception + rescue_exception(exception) end def weekly @@ -31,5 +35,18 @@ module Maintenance UserPasswordResetNonce.prune! ApproverPruner.prune! TagRelationshipRetirementService.find_and_retire! + rescue Exception => exception + rescue_exception(exception) + end + + def rescue_exception(exception) + backtrace = Rails.backtrace_cleaner.clean(exception.backtrace).join("\n") + Rails.logger.error("#{exception.class}: #{exception.message}\n#{backtrace}") + + if defined?(NewRelic::Agent) + NewRelic::Agent.notice_error(exception, custom_params: { backtrace: backtrace }) + end + + raise exception end end From 60ea46e1e720d6df30531c5cb16174667a51831f Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 7 Oct 2018 22:32:52 -0500 Subject: [PATCH 4/4] Add sample newrelic config. --- script/install/newrelic.yml | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 script/install/newrelic.yml diff --git a/script/install/newrelic.yml b/script/install/newrelic.yml new file mode 100644 index 000000000..9166634ce --- /dev/null +++ b/script/install/newrelic.yml @@ -0,0 +1,46 @@ +# This file configures the New Relic Agent. New Relic monitors Ruby, Java, +# .NET, PHP, Python, Node, and Go applications with deep visibility and low +# overhead. For more information, visit www.newrelic.com. +# +# For full documentation of agent configuration options, please refer to +# https://docs.newrelic.com/docs/agents/ruby-agent/installation-configuration/ruby-agent-configuration + +common: &default_settings + # Required license key associated with your New Relic account. + license_key: YOUR_LICENCE_KEY_HERE + + # Your application name. Renaming here affects where data displays in New + # Relic. For more details, see https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/renaming-applications + app_name: Danbooru + + # To disable the agent regardless of other settings, uncomment the following: + # agent_enabled: false + + # Logging level for log/newrelic_agent.log + log_level: info + + # https://docs.newrelic.com/docs/agents/ruby-agent/background-jobs/rake-instrumentation + rake: + tasks: ["maintenance:.*"] + +# Environment-specific settings are in this section. +# RAILS_ENV or RACK_ENV (as appropriate) is used to determine the environment. +# If your application has other named environments, configure them here. +development: + <<: *default_settings + app_name: Danbooru (Development) + + # New Relic is disabled by default in the development environment. Uncomment to enable. + # monitor_mode: true + +test: + <<: *default_settings + # It doesn't make sense to report to New Relic from automated test runs. + monitor_mode: false + +staging: + <<: *default_settings + app_name: Danbooru (Staging) + +production: + <<: *default_settings