Skip to content

Commit

Permalink
MAP-1781 Cleanup Access Logs more than 3 months old (#2408)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tobyprivett authored Nov 15, 2024
1 parent 4cd0ea6 commit f4b8d39
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 1 deletion.
3 changes: 3 additions & 0 deletions helm_deploy/values-preprod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 3 additions & 0 deletions helm_deploy/values-production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 3 additions & 0 deletions helm_deploy/values-staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 3 additions & 0 deletions helm_deploy/values-uat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
14 changes: 14 additions & 0 deletions lib/tasks/access_logs.rake
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion spec/factories/access_logs.rb
Original file line number Diff line number Diff line change
@@ -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' }
Expand Down
31 changes: 31 additions & 0 deletions spec/lib/tasks/access_logs_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f4b8d39

Please sign in to comment.