Skip to content

Commit

Permalink
Add MiqWorkerType
Browse files Browse the repository at this point in the history
This adds a model which will replace the worker type constant.
It seeds the table based on the leaf subclasses of MiqWorker
and each worker class needs to define its own attributes, specifically
its priority in the kill order and bundler groups.

This allows new workers to be added by simply adding a new subclass
of MiqWorker rather than maintaining a list of worker classes in the
core repo.

Additionally this brings us closer to the ability to run a separate
process that will access the database and determine which workers
should be running on which servers, all outside of the main application
code-base.
  • Loading branch information
carbonin committed Nov 20, 2019
1 parent 1e9398f commit 2947aac
Show file tree
Hide file tree
Showing 20 changed files with 121 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/models/manageiq/providers/base_manager/event_catcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ def friendly_name
end
end
end

def self.kill_priority
130
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ def self.supports_container?
def self.normalized_type
@normalized_type ||= "ems_metrics_collector_worker"
end

def self.kill_priority
20
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class ManageIQ::Providers::BaseManager::OperationsWorker < MiqQueueWorkerBase

include PerEmsWorkerMixin

self.required_roles = "ems_operations"
self.required_roles = %w[ems_operations]

def friendly_name
@friendly_name ||= begin
Expand Down
6 changes: 5 additions & 1 deletion app/models/manageiq/providers/base_manager/refresh_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ManageIQ::Providers::BaseManager::RefreshWorker < MiqQueueWorkerBase

# Don't allow multiple refresh workers to run at once
self.include_stopping_workers_on_synchronize = true
self.required_roles = "ems_inventory"
self.required_roles = %w[ems_inventory]

def friendly_name
@friendly_name ||= begin
Expand All @@ -21,4 +21,8 @@ def friendly_name
def self.normalized_type
@normalized_type ||= "ems_refresh_worker"
end

def self.kill_priority
70
end
end
4 changes: 4 additions & 0 deletions app/models/miq_cockpit_ws_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def friendly_name
@friendly_name ||= "Cockpit Worker"
end

def self.kill_priority
160
end

def self.can_start_cockpit_ws?
@supports_cockpit_ws ||= MiqCockpit::WS.can_start_cockpit_ws?
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/miq_ems_metrics_processor_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ def friendly_name
def self.supports_container?
true
end

def self.kill_priority
10
end
end
4 changes: 4 additions & 0 deletions app/models/miq_event_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ class MiqEventHandler < MiqQueueWorkerBase
def self.supports_container?
true
end

def self.kill_priority
60
end
end
4 changes: 4 additions & 0 deletions app/models/miq_generic_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ class MiqGenericWorker < MiqQueueWorkerBase
def self.supports_container?
true
end

def self.kill_priority
50
end
end
4 changes: 4 additions & 0 deletions app/models/miq_priority_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ def self.queue_priority
def self.supports_container?
true
end

def self.kill_priority
90
end
end
4 changes: 4 additions & 0 deletions app/models/miq_remote_console_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ def friendly_name
def self.supports_container?
true
end

def self.kill_priority
150
end
end
4 changes: 4 additions & 0 deletions app/models/miq_reporting_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ class MiqReportingWorker < MiqQueueWorkerBase
def self.supports_container?
true
end

def self.kill_priority
30
end
end
4 changes: 4 additions & 0 deletions app/models/miq_schedule_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ class MiqScheduleWorker < MiqWorker
def self.supports_container?
true
end

def self.kill_priority
80
end
end
4 changes: 4 additions & 0 deletions app/models/miq_smart_proxy_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ class MiqSmartProxyWorker < MiqQueueWorkerBase

self.required_roles = ["smartproxy"]
self.default_queue_name = "smartproxy"

def self.kill_priority
40
end
end
8 changes: 8 additions & 0 deletions app/models/miq_ui_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ def self.supports_container?
true
end

def self.bundler_groups
%w[manageiq_default ui_dependencies graphql_api]
end

def self.kill_priority
140
end

def container_port
3001
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/miq_web_service_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ def friendly_name
def self.supports_container?
true
end

def self.bundler_groups
%w[manageiq_default graphql_api]
end

def self.kill_priority
100
end
end
8 changes: 8 additions & 0 deletions app/models/miq_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ class << self
attr_writer :workers
end

def self.bundler_groups
%w[manageiq_default]
end

def self.kill_priority
raise NotImplementedError, ".kill_priority must be implemented in a subclass"
end

def self.workers
return (self.has_minimal_env_option? ? 1 : 0) if MiqServer.minimal_env? && check_for_minimal_role
return 0 unless has_required_role?
Expand Down
24 changes: 24 additions & 0 deletions app/models/miq_worker_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class MiqWorkerType < ApplicationRecord
EXCLUDED_CLASS_NAMES = %w[ManageIQ::Providers::BaseManager::OperationsWorker].freeze

scope :in_kill_order, -> { order(:kill_priority => :asc) }

def self.seed
transaction do
classes_for_seed.each { |klass| seed_worker(klass) }
end
end

private_class_method def self.classes_for_seed
MiqWorker.descendants.select { |w| w.subclasses.empty? } - EXCLUDED_CLASS_NAMES.map(&:constantize)
end

private_class_method def self.seed_worker(klass)
instance = find_or_initialize_by(:worker_type => klass.name)

instance.update!(
:bundler_groups => klass.bundler_groups,
:kill_priority => klass.kill_priority
)
end
end
1 change: 1 addition & 0 deletions config/replication_exclude_tables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
- miq_user_roles
- miq_widgets
- miq_widget_contents
- miq_worker_types
- miq_workers
- notifications
- notification_recipients
Expand Down
1 change: 1 addition & 0 deletions lib/evm_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class EvmDatabase
Zone
MiqServer
ServerRole
MiqWorkerType
Tenant
MiqProductFeature
MiqUserRole
Expand Down
21 changes: 21 additions & 0 deletions spec/models/miq_worker_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
describe MiqWorkerType do
describe ".seed" do
before { described_class.seed }

it "doesn't create a row for excluded classes" do
expect(described_class.count).to be > 0
expect(described_class.pluck(:worker_type)).not_to include(*described_class::EXCLUDED_CLASS_NAMES)
end

it "correctly creates records for workers" do
generic_worker = described_class.find_by(:worker_type => "MiqGenericWorker")
ui_worker = described_class.find_by(:worker_type => "MiqUiWorker")

expect(generic_worker.bundler_groups).to match_array(MiqGenericWorker.bundler_groups)
expect(generic_worker.kill_priority).to eq(MiqGenericWorker.kill_priority)

expect(ui_worker.bundler_groups).to match_array(MiqUiWorker.bundler_groups)
expect(ui_worker.kill_priority).to eq(MiqUiWorker.kill_priority)
end
end
end

0 comments on commit 2947aac

Please sign in to comment.