From 4a40764173ad0fa344fd32532dbcdd9f1b373bbf Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Fri, 20 Jun 2014 22:02:32 +0400 Subject: [PATCH] #403: move abf_worker/*_observer to app/jobs/abf_worker/ --- .../product_build_lists_controller.rb | 2 +- .../projects/build_lists_controller.rb | 2 +- {lib => app/jobs}/abf_worker/base_observer.rb | 0 .../jobs}/abf_worker/iso_worker_observer.rb | 0 .../jobs}/abf_worker/publish_observer.rb | 0 .../jobs}/abf_worker/rpm_worker_observer.rb | 0 app/models/build_list.rb | 2 +- .../models/concerns/abf_worker_methods.rb | 16 ++--- app/models/product_build_list.rb | 2 +- app/presenters/abf_worker_status_presenter.rb | 59 ++++++++++++++++++ lib/abf_worker/status_inspector.rb | 60 ------------------- 11 files changed, 68 insertions(+), 75 deletions(-) rename {lib => app/jobs}/abf_worker/base_observer.rb (100%) rename {lib => app/jobs}/abf_worker/iso_worker_observer.rb (100%) rename {lib => app/jobs}/abf_worker/publish_observer.rb (100%) rename {lib => app/jobs}/abf_worker/rpm_worker_observer.rb (100%) rename lib/abf_worker/model_helper.rb => app/models/concerns/abf_worker_methods.rb (91%) create mode 100644 app/presenters/abf_worker_status_presenter.rb delete mode 100644 lib/abf_worker/status_inspector.rb diff --git a/app/controllers/platforms/product_build_lists_controller.rb b/app/controllers/platforms/product_build_lists_controller.rb index 2e3d1a145..ef0067f99 100644 --- a/app/controllers/platforms/product_build_lists_controller.rb +++ b/app/controllers/platforms/product_build_lists_controller.rb @@ -78,7 +78,7 @@ class Platforms::ProductBuildListsController < Platforms::BaseController @product_build_lists = @product_build_lists.for_status(params[:status]) if params[:status].present? end @product_build_lists = @product_build_lists.recent.paginate page: params[:page] - @build_server_status = AbfWorker::StatusInspector.products_status + @build_server_status = AbfWorkerStatusPresenter.new.products_status end protected diff --git a/app/controllers/projects/build_lists_controller.rb b/app/controllers/projects/build_lists_controller.rb index 40fd22be0..52f0aa571 100644 --- a/app/controllers/projects/build_lists_controller.rb +++ b/app/controllers/projects/build_lists_controller.rb @@ -38,7 +38,7 @@ class Projects::BuildListsController < Projects::BaseController project: :project_statistics ) - @build_server_status = AbfWorker::StatusInspector.projects_status + @build_server_status = AbfWorkerStatusPresenter.new.projects_status end end end diff --git a/lib/abf_worker/base_observer.rb b/app/jobs/abf_worker/base_observer.rb similarity index 100% rename from lib/abf_worker/base_observer.rb rename to app/jobs/abf_worker/base_observer.rb diff --git a/lib/abf_worker/iso_worker_observer.rb b/app/jobs/abf_worker/iso_worker_observer.rb similarity index 100% rename from lib/abf_worker/iso_worker_observer.rb rename to app/jobs/abf_worker/iso_worker_observer.rb diff --git a/lib/abf_worker/publish_observer.rb b/app/jobs/abf_worker/publish_observer.rb similarity index 100% rename from lib/abf_worker/publish_observer.rb rename to app/jobs/abf_worker/publish_observer.rb diff --git a/lib/abf_worker/rpm_worker_observer.rb b/app/jobs/abf_worker/rpm_worker_observer.rb similarity index 100% rename from lib/abf_worker/rpm_worker_observer.rb rename to app/jobs/abf_worker/rpm_worker_observer.rb diff --git a/app/models/build_list.rb b/app/models/build_list.rb index 98c58ee2a..8507de12f 100644 --- a/app/models/build_list.rb +++ b/app/models/build_list.rb @@ -1,7 +1,7 @@ class BuildList < ActiveRecord::Base include CommitAndVersion include FileStoreClean - include AbfWorker::ModelHelper + include AbfWorkerMethods include Feed::BuildList include BuildListObserver include EventLoggable diff --git a/lib/abf_worker/model_helper.rb b/app/models/concerns/abf_worker_methods.rb similarity index 91% rename from lib/abf_worker/model_helper.rb rename to app/models/concerns/abf_worker_methods.rb index 2aba51664..30f4e6e19 100644 --- a/lib/abf_worker/model_helper.rb +++ b/app/models/concerns/abf_worker_methods.rb @@ -1,15 +1,9 @@ -module AbfWorker::ModelHelper - # In model which contains this helper should be: - # - #abf_worker_args - # - #build_canceled +module AbfWorkerMethods + extend ActiveSupport::Concern MASS_BUILDS_SET = 'abf-worker::mass-builds' USER_BUILDS_SET = 'abf-worker::user-builds' - def self.included(base) - base.extend(ClassMethods) - end - module ClassMethods def log_server @log_server ||= Redis.new( @@ -18,10 +12,9 @@ module AbfWorker::ModelHelper ) end - def self.next_build + def next_build raise NotImplementedError end - end def abf_worker_log @@ -106,4 +99,5 @@ module AbfWorker::ModelHelper "abfworker::#{abf_worker_base_queue.gsub(/\_/, '-')}-#{id}" end -end \ No newline at end of file + +end diff --git a/app/models/product_build_list.rb b/app/models/product_build_list.rb index 8f3e78951..5c8464928 100644 --- a/app/models/product_build_list.rb +++ b/app/models/product_build_list.rb @@ -3,7 +3,7 @@ class ProductBuildList < ActiveRecord::Base include TimeLiving include FileStoreClean include UrlHelper - include AbfWorker::ModelHelper + include AbfWorkerMethods include EventLoggable delegate :url_helpers, to: 'Rails.application.routes' diff --git a/app/presenters/abf_worker_status_presenter.rb b/app/presenters/abf_worker_status_presenter.rb new file mode 100644 index 000000000..da0ae4512 --- /dev/null +++ b/app/presenters/abf_worker_status_presenter.rb @@ -0,0 +1,59 @@ +class AbfWorkerStatusPresenter < ApplicationPresenter + + def initialize + end + + def projects_status + Rails.cache.fetch([AbfWorkerStatusPresenter, :projects_status], expires_in: 10.seconds) do + result = get_status(:rpm, :publish) { |w, worker| w.to_s =~ /#{worker}_worker_default/ } + nodes = RpmBuildNode.total_statistics + result[:rpm][:workers] += nodes[:systems] + result[:rpm][:build_tasks] += nodes[:busy] + result[:rpm][:other_workers] = nodes[:others] + + external_bls = BuildList.for_status(BuildList::BUILD_PENDING).external_nodes(:everything).count + result[:rpm][:default_tasks] += external_bls + count_of_tasks('user_build_') + + mass_build_tasks = count_of_tasks('mass_build_') + result[:rpm][:low_tasks] += mass_build_tasks + result[:rpm][:tasks] += external_bls + mass_build_tasks + result + end + end + + def products_status + get_status(:iso) { |w, worker| + str = w.to_s + str =~ /iso_worker/ && str !~ /observer/ + } + end + + protected + + def count_of_tasks(regexp) + Redis.current.smembers('resque:queues'). + select{ |q| q =~ /#{regexp}/ }. + map{ |q| Redis.current.llen("resque:queue:#{q}") }.sum + end + + def get_status(*queues) + status = {} + queues.each do |worker| + workers = Resque.workers.select{ |w| yield w, worker } + status[worker] = status_of_worker workers, worker + end + status + end + + def status_of_worker(workers, worker) + key = "resque:queue:#{worker}_worker" + default_tasks, tasks = Redis.current.llen("#{key}_default"), Redis.current.llen(key) + { + workers: workers.count, + build_tasks: workers.select{ |w| w.working? }.count, + default_tasks: default_tasks, + low_tasks: tasks, + tasks: (default_tasks + tasks) + } + end +end diff --git a/lib/abf_worker/status_inspector.rb b/lib/abf_worker/status_inspector.rb deleted file mode 100644 index 0106f3df3..000000000 --- a/lib/abf_worker/status_inspector.rb +++ /dev/null @@ -1,60 +0,0 @@ -module AbfWorker - class StatusInspector - - class << self - def projects_status - Rails.cache.fetch([AbfWorker::StatusInspector, :projects_status], expires_in: 10.seconds) do - result = get_status(:rpm, :publish) { |w, worker| w.to_s =~ /#{worker}_worker_default/ } - nodes = RpmBuildNode.total_statistics - result[:rpm][:workers] += nodes[:systems] - result[:rpm][:build_tasks] += nodes[:busy] - result[:rpm][:other_workers] = nodes[:others] - - external_bls = BuildList.for_status(BuildList::BUILD_PENDING).external_nodes(:everything).count - result[:rpm][:default_tasks] += external_bls + count_of_tasks('user_build_') - - mass_build_tasks = count_of_tasks('mass_build_') - result[:rpm][:low_tasks] += mass_build_tasks - result[:rpm][:tasks] += external_bls + mass_build_tasks - result - end - end - - def count_of_tasks(regexp) - Redis.current.smembers('resque:queues'). - select{ |q| q =~ /#{regexp}/ }. - map{ |q| Redis.current.llen("resque:queue:#{q}") }.sum - end - - def products_status - get_status(:iso) { |w, worker| - str = w.to_s - str =~ /iso_worker/ && str !~ /observer/ - } - end - - protected - - def get_status(*queues) - status = {} - queues.each do |worker| - workers = Resque.workers.select{ |w| yield w, worker } - status[worker] = status_of_worker workers, worker - end - status - end - - def status_of_worker(workers, worker) - key = "resque:queue:#{worker}_worker" - default_tasks, tasks = Redis.current.llen("#{key}_default"), Redis.current.llen(key) - { - workers: workers.count, - build_tasks: workers.select{ |w| w.working? }.count, - default_tasks: default_tasks, - low_tasks: tasks, - tasks: (default_tasks + tasks) - } - end - end - end -end