From 28618a8d6845bcb71069bcc0fc8721f89565a09d Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Fri, 26 Sep 2014 23:51:28 +0400 Subject: [PATCH] #430: Cleans old RPM buildroots from BuildLists --- app/jobs/build_lists/clean_buildroot_job.rb | 30 ++++++++++++++++ config/resque_schedule.yml | 7 ++++ spec/jobs/clean_buildroot_job_spec.rb | 38 +++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 app/jobs/build_lists/clean_buildroot_job.rb create mode 100644 spec/jobs/clean_buildroot_job_spec.rb diff --git a/app/jobs/build_lists/clean_buildroot_job.rb b/app/jobs/build_lists/clean_buildroot_job.rb new file mode 100644 index 000000000..f1e8bfceb --- /dev/null +++ b/app/jobs/build_lists/clean_buildroot_job.rb @@ -0,0 +1,30 @@ +module BuildLists + class CleanBuildrootJob + @queue = :middle + + FILENAME = 'rpm-buildroot.tar.gz' + + def self.perform + build_lists = BuildList.where(save_buildroot: true). + where('updated_at < ?', Time.now - 1.hour). + where('results ~ ?', "file_name: #{FILENAME}") + + build_lists.find_each do |build_list| + buildroots = build_list.results.select do |r| + r['file_name'] == FILENAME + end + build_list.results -= buildroots + build_list.save(validate: false) + + clean_file_store buildroots + end + end + + def self.clean_file_store(buildroots) + buildroots.each do |r| + FileStoreService::File.new(sha1: r['sha1']).destroy + end + end + + end +end \ No newline at end of file diff --git a/config/resque_schedule.yml b/config/resque_schedule.yml index dc911c9a1..9b9ba8127 100644 --- a/config/resque_schedule.yml +++ b/config/resque_schedule.yml @@ -26,6 +26,13 @@ clean_api_defender_statistics: queue: low description: 'Cleans ApiDefender statistics' +clean_build_list_buildroot: + every: + - '1h' + class: 'BuildLists::CleanBuildrootJob' + queue: middle + description: 'Cleans RPM buildroot from BuildList' + run_extra_mass_builds: every: - '5m' diff --git a/spec/jobs/clean_buildroot_job_spec.rb b/spec/jobs/clean_buildroot_job_spec.rb new file mode 100644 index 000000000..da4e32785 --- /dev/null +++ b/spec/jobs/clean_buildroot_job_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +describe BuildLists::CleanBuildrootJob do + + before { stub_symlink_methods } + subject { BuildLists::CleanBuildrootJob } + + it 'ensures that not raises error' do + FactoryGirl.create(:build_list) + expect(FileStoreService::File).to_not receive(:new) + expect do + subject.perform + end.to_not raise_exception + end + + it 'cleans RPM buildroot' do + results = [ + { 'sha1' => 'sha1-1', 'file_name' => BuildLists::CleanBuildrootJob::FILENAME }, + { 'sha1' => 'sha1-2', 'file_name' => 'test.log' } + ] + FactoryGirl.create(:build_list, + results: results, + save_buildroot: true + ) + bl = FactoryGirl.create(:build_list, + results: results, + save_buildroot: true, + updated_at: Time.now - 2.hours + ) + file_store_service = double(:file_store_service, destroy: true) + + expect(FileStoreService::File).to receive(:new).with(sha1: 'sha1-1').and_return(file_store_service) + + subject.perform + expect(bl.reload.results).to eq [{ 'sha1' => 'sha1-2', 'file_name' => 'test.log' }] + end + +end