diff --git a/Gemfile b/Gemfile index 0abd5a1e6..5a9d2bbca 100644 --- a/Gemfile +++ b/Gemfile @@ -125,4 +125,5 @@ group :test do gem 'mock_redis', '~> 0.11' gem 'rake' gem 'test_after_commit' + gem 'timecop' end diff --git a/Gemfile.lock b/Gemfile.lock index 98a69e95b..f071bdfcc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -432,6 +432,7 @@ GEM time_diff (0.3.0) activesupport i18n + timecop (0.7.1) tmp_cache (0.1.1) treetop (1.4.15) polyglot @@ -546,6 +547,7 @@ DEPENDENCIES therubyracer (~> 0.12.1) therubyrhino (~> 1.73.1) time_diff + timecop uglifier (~> 2.4) underscore-rails whenever (~> 0.9.0) diff --git a/app/jobs/clean_api_defender_statistics_job.rb b/app/jobs/clean_api_defender_statistics_job.rb new file mode 100644 index 000000000..6fea6e786 --- /dev/null +++ b/app/jobs/clean_api_defender_statistics_job.rb @@ -0,0 +1,17 @@ +class CleanApiDefenderStatisticsJob + @queue = :clone_build + + def self.perform + deadline = Date.today - 1.month + Redis.current.keys.select do |key| + next if key !~ /^throttle:/ + # See: https://github.com/datagraph/rack-throttle/blob/master/lib/rack/throttle/daily.rb#L41 + # Formats: + # 'throttle:uname:%Y-%m-%dT%H', 'throttle:uname:%Y-%m-%d' + # example: "throttle:uname1:2014-01-25T20", "throttle:uname1:2014-01-25" + date = key.gsub(/.*:/, '').gsub(/T.*/, '') + Redis.current.del(key) if Date.parse(date) < deadline + end + end + +end diff --git a/config/resque_schedule.yml b/config/resque_schedule.yml index 60cd0e700..44b2dfac2 100644 --- a/config/resque_schedule.yml +++ b/config/resque_schedule.yml @@ -17,4 +17,11 @@ build_lists_queues_monitoring: - '1m' class: 'BuildListsQueuesMonitoringJob' queue: hook - description: 'Monitoring for "user/mass-build" queues' \ No newline at end of file + description: 'Monitoring for "user/mass-build" queues' + +clean_api_defender_statistics: + every: + - '1d' + class: 'CleanApiDefenderStatisticsJob' + queue: clone_build + description: 'Cleans ApiDefender statistics' \ No newline at end of file diff --git a/spec/jobs/clean_api_defender_statistics_job_spec.rb b/spec/jobs/clean_api_defender_statistics_job_spec.rb new file mode 100644 index 000000000..228c232f9 --- /dev/null +++ b/spec/jobs/clean_api_defender_statistics_job_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe CleanApiDefenderStatisticsJob do + + it 'ensures that not raises error' do + lambda do + CleanApiDefenderStatisticsJob.perform + end.should_not raise_exception + end + + it 'ensures that cleans only old statistics' do + today = Date.today + Timecop.freeze(today) do + key1 = "throttle:key1:#{today.strftime('%Y-%m-%d')}" + key2 = "throttle:key2:#{today.strftime('%Y-%m-%d')}T01" + key3 = "throttle:key1:#{(today - 32.days).strftime('%Y-%m-%d')}" + key4 = "throttle:key2:#{(today - 32.days).strftime('%Y-%m-%d')}T01" + key5 = "other:throttle:key:#{(today - 32.days).strftime('%Y-%m-%d')}" + @redis_instance.set key1, 1 + @redis_instance.set key2, 1 + @redis_instance.set key3, 1 + @redis_instance.set key4, 1 + @redis_instance.set key5, 1 + + CleanApiDefenderStatisticsJob.perform + @redis_instance.keys.should include(key1, key2, key5) + @redis_instance.keys.should_not include(key3, key4) + end + end + +end