mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Add cleanup method for API history keys.
This commit is contained in:
parent
095331792d
commit
0b1478d607
2 changed files with 48 additions and 0 deletions
|
@ -37,6 +37,7 @@ module Sidekiq
|
|||
def initialize(days_previous, start_date = nil)
|
||||
@days_previous = days_previous
|
||||
@start_date = start_date || Time.now.utc.to_date
|
||||
@days_of_stats_to_keep = 180
|
||||
end
|
||||
|
||||
def processed
|
||||
|
@ -47,6 +48,25 @@ module Sidekiq
|
|||
date_stat_hash("failed")
|
||||
end
|
||||
|
||||
def cleanup
|
||||
today = Time.now.utc.to_date
|
||||
delete_before_date = Time.now.utc.to_date - @days_of_stats_to_keep
|
||||
|
||||
Sidekiq.redis do |conn|
|
||||
processed_keys = conn.keys("stat:processed:*")
|
||||
|
||||
processed_keys.each do |key|
|
||||
conn.del(key) if key < "stat:processed:#{delete_before_date.to_s}"
|
||||
end
|
||||
|
||||
failed_keys = conn.keys("stat:failed:*")
|
||||
|
||||
failed_keys.each do |key|
|
||||
conn.del(key) if key < "stat:failed:#{delete_before_date.to_s}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def date_stat_hash(stat)
|
||||
|
|
|
@ -115,6 +115,34 @@ class TestApi < MiniTest::Unit::TestCase
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "cleanup" do
|
||||
it 'removes processed stats outside of keep window' do
|
||||
Sidekiq.redis do |c|
|
||||
c.incrby("stat:processed:2012-05-03", 4)
|
||||
c.incrby("stat:processed:2012-06-03", 4)
|
||||
c.incrby("stat:processed:2012-07-03", 1)
|
||||
end
|
||||
Time.stub(:now, Time.parse("2012-12-01 1:00:00 -0500")) do
|
||||
s = Sidekiq::Stats::History.new(0)
|
||||
s.cleanup
|
||||
assert_equal false, Sidekiq.redis { |c| c.exists("stat:processed:2012-05-03") }
|
||||
end
|
||||
end
|
||||
|
||||
it 'removes failed stats outside of keep window' do
|
||||
Sidekiq.redis do |c|
|
||||
c.incrby("stat:failed:2012-05-03", 4)
|
||||
c.incrby("stat:failed:2012-06-03", 4)
|
||||
c.incrby("stat:failed:2012-07-03", 1)
|
||||
end
|
||||
Time.stub(:now, Time.parse("2012-12-01 1:00:00 -0500")) do
|
||||
s = Sidekiq::Stats::History.new(0)
|
||||
s.cleanup
|
||||
assert_equal false, Sidekiq.redis { |c| c.exists("stat:failed:2012-05-03") }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue