Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Commit

Permalink
Limit stored payload_log_entry data to 20
Browse files Browse the repository at this point in the history
[#92367008]
  • Loading branch information
Anthony Dreessen and Jared Friese committed Apr 14, 2015
1 parent 217c9a2 commit 010c5ad
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 112 deletions.
1 change: 1 addition & 0 deletions .pairs
Expand Up @@ -14,6 +14,7 @@ pairs:
dt: David Tengdin; dtengdin
eg: Edgar Gonzalez; edgar
gs: Graham Siener; gsiener
jf: Jared Friese; jfriese
jam: Joe Masilotti; jmasilotti
britz: Johnathon Britz; jbritz
jb: Jonathan Berger; jonathanpberger
Expand Down
2 changes: 0 additions & 2 deletions Gemfile.lock
Expand Up @@ -264,15 +264,13 @@ GEM
railties (>= 3.2)
sass-rails (>= 3.2)
pivotal-tracker (0.5.12)
builder
builder
crack
happymapper (>= 0.3.2)
nokogiri (>= 1.4.3)
nokogiri (>= 1.5.5)
nokogiri-happymapper (>= 0.5.4)
rest-client (~> 1.6.0)
rest-client (~> 1.6.0)
pry (0.9.12.2)
coderay (~> 1.0.5)
method_source (~> 0.8)
Expand Down
15 changes: 6 additions & 9 deletions app/models/payload_log_entry.rb
@@ -1,6 +1,8 @@
class PayloadLogEntry < ActiveRecord::Base
belongs_to :project
after_create :remove_duplicates
after_create :purge_old_logs

LOG_ENTRIES_TO_KEEP = 20

default_scope { order('created_at DESC') }
scope :reverse_chronological, -> () { order('created_at DESC') }
Expand All @@ -13,13 +15,8 @@ def to_s
"#{update_method} #{status}"
end

def remove_duplicates
if status == 'failed'
PayloadLogEntry.where(project_id: project_id).order(created_at: :asc).limit(2).each do |entry|
if entry.id != id && entry.status == 'failed' && entry.error_text == error_text
entry.delete()
end
end
end
def purge_old_logs
payloads = PayloadLogEntry.where(project_id: project_id).order(created_at: :desc).limit(LOG_ENTRIES_TO_KEEP)
PayloadLogEntry.where("created_at < ? and project_id = ?", payloads.last.created_at, project_id).delete_all
end
end
3 changes: 1 addition & 2 deletions lib/tasks/truncate.rake
@@ -1,10 +1,9 @@
namespace "truncate" do
TRUNCATE_DEFAULT_DURATION = 1
TRUNCATE_DEFAULT_COUNT = 2
TRUNCATE_DEFAULT_COUNT = 15

desc "Truncate Payload Log Entries that are older than a certain date or successful (Default: #{TRUNCATE_DEFAULT_DURATION} days ago)"
task :payload_log_entries, [:duration] => :environment do |task, args|
default_duration = 3
duration = (args[:duration] ? args[:duration].to_i : TRUNCATE_DEFAULT_DURATION).days.ago
duration_count = PayloadLogEntry.where('created_at < ?', duration).count
Rails.logger.info "#{'*' * 20}Truncating #{duration_count} Payload Log Entries greater than #{duration.strftime('%D')}...#{'*' * 20}"
Expand Down
99 changes: 0 additions & 99 deletions spec/lib/payload_log_entry_spec.rb

This file was deleted.

61 changes: 61 additions & 0 deletions spec/models/payload_log_entry_spec.rb
@@ -0,0 +1,61 @@
require 'spec_helper'

describe PayloadLogEntry do

describe ".reverse_chronological" do
subject { PayloadLogEntry.reverse_chronological }
let!(:entry1) { PayloadLogEntry.create(created_at: 2.years.ago) }
let!(:entry2) { PayloadLogEntry.create(created_at: 1.year.ago) }
let!(:entry3) { PayloadLogEntry.create }
it { is_expected.to eq([entry3, entry2, entry1]) }
end

describe '.latest' do
let!(:entry1) { PayloadLogEntry.create(created_at: 2.years.ago) }
let!(:entry2) { PayloadLogEntry.create(created_at: 1.year.ago) }
let!(:entry3) { PayloadLogEntry.create }

it "should return the latest" do
expect(PayloadLogEntry.latest).to eq(entry3)
end
end

describe 'record creation' do
before do
(0...PayloadLogEntry::LOG_ENTRIES_TO_KEEP).each do |i|
log = PayloadLogEntry.create(
created_at: (PayloadLogEntry::LOG_ENTRIES_TO_KEEP - i).days.ago,
status: 'failed',
error_text: 'does not matter',
project_id: 1
)

PayloadLogEntry.create(
created_at: ((PayloadLogEntry::LOG_ENTRIES_TO_KEEP - i) * 10 ).days.ago,
status: 'failed',
error_text: 'does not matter',
project_id: 2
)

@oldest_log ||= log
end

end

it 'removes all but the last PayloadLogEntry::LOG_ENTRIES_TO_KEEP logs for a given project' do
expect(PayloadLogEntry.count).to eq(PayloadLogEntry::LOG_ENTRIES_TO_KEEP*2)

newest_log = PayloadLogEntry.create(
created_at: Time.now,
status: 'failed',
error_text: 'does not matter',
project_id:1
)

expect(PayloadLogEntry.count).to eq(PayloadLogEntry::LOG_ENTRIES_TO_KEEP*2)
expect(PayloadLogEntry.where(project_id: 1)).not_to include(@oldest_log)
expect(PayloadLogEntry.where(project_id: 1)).to include(newest_log)
end
end
end

0 comments on commit 010c5ad

Please sign in to comment.