From d0d43b009093545e95d2c6dc11e4e75da6a58727 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Tue, 22 Jan 2013 20:34:16 +0600 Subject: [PATCH] [refs #838] add cleaning file-store when destroing product build list && refactoring --- app/models/build_list.rb | 17 +------------ app/models/product_build_list.rb | 1 + lib/modules/models/file_store_clean.rb | 35 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 lib/modules/models/file_store_clean.rb diff --git a/app/models/build_list.rb b/app/models/build_list.rb index 93446409f..7658e1778 100644 --- a/app/models/build_list.rb +++ b/app/models/build_list.rb @@ -1,6 +1,7 @@ # -*- encoding : utf-8 -*- class BuildList < ActiveRecord::Base include Modules::Models::CommitAndVersion + include Modules::Models::FileStoreClean include AbfWorker::ModelHelper belongs_to :project @@ -390,22 +391,6 @@ class BuildList < ActiveRecord::Base .recent end - def destroy - files, url = [], "#{APP_CONFIG['file_store_url']}/api/v1/file_stores" - self.results.each {|r| files << r['sha1'] if r['sha1'].present?} - self.packages.each {|pk| files << pk.sha1 if pk.sha1.present?} - - files.each do |sha1| - begin - token = User.system.find_by_uname('file_store').authentication_token - `curl --user #{token}: -X DELETE #{url}/#{sha1}.json` - rescue # FIXME - end - end - super - end - later :destroy, :queue => :clone_build - protected def abf_worker_priority diff --git a/app/models/product_build_list.rb b/app/models/product_build_list.rb index 7142d3f7f..0d2248097 100644 --- a/app/models/product_build_list.rb +++ b/app/models/product_build_list.rb @@ -2,6 +2,7 @@ class ProductBuildList < ActiveRecord::Base include Modules::Models::CommitAndVersion include Modules::Models::TimeLiving + include Modules::Models::FileStoreClean include AbfWorker::ModelHelper delegate :url_helpers, to: 'Rails.application.routes' diff --git a/lib/modules/models/file_store_clean.rb b/lib/modules/models/file_store_clean.rb new file mode 100644 index 000000000..bef23b3e0 --- /dev/null +++ b/lib/modules/models/file_store_clean.rb @@ -0,0 +1,35 @@ +# -*- encoding : utf-8 -*- +module Modules + module Models + module FileStoreClean + extend ActiveSupport::Concern + + included do + def destroy + files, url = [], "#{APP_CONFIG['file_store_url']}/api/v1/file_stores" + self.results.each {|r| files << r['sha1'] if r['sha1'].present?} + if self.respond_to? :packages + self.packages.each {|pk| files << pk.sha1 if pk.sha1.present?} + end + if files.count > 0 + token = User.system.find_by_uname('file_store').authentication_token + uri = URI APP_CONFIG['file_store_url'] + Net::HTTP.start(uri.host, uri.port) do |http| + files.each do |sha1| + begin + req = Net::HTTP::Delete.new("/api/v1/file_stores/#{sha1}.json") + req.basic_auth token, '' + http.request(req) + rescue # Dont care about it + end + end + end + end + + super + end + later :destroy, :queue => :clone_build + end + end + end +end