From f4b8d39c498c5c817aae3f91ae446b96a46dcf92 Mon Sep 17 00:00:00 2001 From: Toby Privett Date: Fri, 15 Nov 2024 12:19:02 +0000 Subject: [PATCH] MAP-1781 Cleanup Access Logs more than 3 months old (#2408) * Cleanup Access Logs more than 3 months old * Fix rubocop issues * Prefer a cutoff date of 6 months * Add a spec to test the access_logs:cleanup task * Prefer cutoff date of 1 year * `reenable` lets us run a rake task multiple times in a spec --- helm_deploy/values-preprod.yaml | 3 +++ helm_deploy/values-production.yaml | 3 +++ helm_deploy/values-staging.yaml | 3 +++ helm_deploy/values-uat.yaml | 3 +++ lib/tasks/access_logs.rake | 14 ++++++++++++++ spec/factories/access_logs.rb | 1 - spec/lib/tasks/access_logs_spec.rb | 31 ++++++++++++++++++++++++++++++ 7 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 lib/tasks/access_logs.rake create mode 100644 spec/lib/tasks/access_logs_spec.rb diff --git a/helm_deploy/values-preprod.yaml b/helm_deploy/values-preprod.yaml index 93839a684..2dec95c7e 100644 --- a/helm_deploy/values-preprod.yaml +++ b/helm_deploy/values-preprod.yaml @@ -49,6 +49,9 @@ generic-service: REDIS_URL: url cronJobs: + - name: access-log-cleanup + schedule: "0 2 * * *" + command: ["bundle", "exec", "rake", "access_logs:cleanup"] - name: token-cleanup schedule: "0 1 * * 0" command: ["bundle", "exec", "rake", "doorkeeper:db:cleanup"] diff --git a/helm_deploy/values-production.yaml b/helm_deploy/values-production.yaml index beacd90e7..f0ce86a8b 100644 --- a/helm_deploy/values-production.yaml +++ b/helm_deploy/values-production.yaml @@ -77,6 +77,9 @@ sidekiq: GOVUK_NOTIFY_API_KEY: govuk_notify_api_key cronJobs: + - name: access-log-cleanup + schedule: "0 2 * * *" + command: ["bundle", "exec", "rake", "access_logs:cleanup"] - name: token-cleanup schedule: "0 1 * * 0" command: ["bundle", "exec", "rake", "doorkeeper:db:cleanup"] diff --git a/helm_deploy/values-staging.yaml b/helm_deploy/values-staging.yaml index c72b9e94b..f4944e855 100644 --- a/helm_deploy/values-staging.yaml +++ b/helm_deploy/values-staging.yaml @@ -63,6 +63,9 @@ sidekiq: GOVUK_NOTIFY_API_KEY: govuk_notify_api_key cronJobs: + - name: access-log-cleanup + schedule: "0 2 * * *" + command: ["bundle", "exec", "rake", "access_logs:cleanup"] - name: token-cleanup schedule: "0 1 * * 0" command: ["bundle", "exec", "rake", "doorkeeper:db:cleanup"] diff --git a/helm_deploy/values-uat.yaml b/helm_deploy/values-uat.yaml index 94d0b6429..0431e9255 100644 --- a/helm_deploy/values-uat.yaml +++ b/helm_deploy/values-uat.yaml @@ -55,6 +55,9 @@ sidekiq: GOVUK_NOTIFY_API_KEY: govuk_notify_api_key cronJobs: + - name: access-log-cleanup + schedule: "0 2 * * *" + command: ["bundle", "exec", "rake", "access_logs:cleanup"] - name: token-cleanup schedule: "0 1 * * 0" command: ["bundle", "exec", "rake", "doorkeeper:db:cleanup"] diff --git a/lib/tasks/access_logs.rake b/lib/tasks/access_logs.rake new file mode 100644 index 000000000..87b375142 --- /dev/null +++ b/lib/tasks/access_logs.rake @@ -0,0 +1,14 @@ +namespace :access_logs do + desc 'Cleanup access_logs older than 1 year' + task cleanup: [:environment] do + cutoff_date = 1.year.ago + access_logs_count = AccessLog.where('timestamp < ?', cutoff_date).count + number_of_iterations = (access_logs_count.to_f / 1000).ceil + + puts "Cleaning up #{access_logs_count} access_logs in #{number_of_iterations} iterations" + + 1.upto(number_of_iterations).each do + AccessLog.where('timestamp < ?', cutoff_date).limit(1000).delete_all + end + end +end diff --git a/spec/factories/access_logs.rb b/spec/factories/access_logs.rb index 26b36920a..055491584 100644 --- a/spec/factories/access_logs.rb +++ b/spec/factories/access_logs.rb @@ -1,6 +1,5 @@ FactoryBot.define do factory :access_log do - id { '35cc6a19-0d88-453a-a0cb-970e161b5cbb' } request_id { 'b68c0883-540c-426a-a9a4-daf586eb5c78' } timestamp { Time.zone.now } whodunnit { 'AUSER01' } diff --git a/spec/lib/tasks/access_logs_spec.rb b/spec/lib/tasks/access_logs_spec.rb new file mode 100644 index 000000000..e27abf4e1 --- /dev/null +++ b/spec/lib/tasks/access_logs_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Rake::Task['access_logs:cleanup'] do + before do + # 2 access_logs to retain + create(:access_log, timestamp: 1.month.ago) + create(:access_log, timestamp: 7.months.ago) + + # 3 access_logs to delete + create(:access_log, timestamp: 13.months.ago) + create(:access_log, timestamp: 14.months.ago) + create(:access_log, timestamp: 15.months.ago) + + allow($stdout).to receive(:puts) + + described_class.reenable + described_class.invoke + end + + it 'cleans up the access_logs' do + expect(AccessLog.count).to eq(2) + end + + it 'writes to stdout' do + expect($stdout) + .to have_received(:puts) + .with('Cleaning up 3 access_logs in 1 iterations') + end +end