-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
4cd0ea6
commit f4b8d39
Showing
7 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |