Skip to content

Commit

Permalink
Sync systemd services and processes
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Nov 4, 2021
1 parent a062596 commit 6a13a03
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 39 deletions.
6 changes: 0 additions & 6 deletions app/models/miq_server/worker_management/kubernetes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ class MiqServer::WorkerManagement::Kubernetes < MiqServer::WorkerManagement

attr_accessor :deployments_monitor_thread, :pods_monitor_thread

def sync_monitor
sync_from_system

super
end

def sync_from_system
# All miq_server instances have to reside on the same Kubernetes cluster, so
# we only have to sync the list of pods and deployments once
Expand Down
3 changes: 3 additions & 0 deletions app/models/miq_server/worker_management/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def monitor_workers
# Reload my_server so we can detect role and possibly other changes faster
my_server.reload

# Cache a list of the native objects backing the miq_workers (e.g.: pods, services, or processes)
sync_from_system

sync_monitor

# Sync the workers after sync'ing the child worker settings
Expand Down
6 changes: 6 additions & 0 deletions app/models/miq_server/worker_management/process.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
class MiqServer::WorkerManagement::Process < MiqServer::WorkerManagement
attr_accessor :miq_processes

def sync_from_system
self.miq_processes = miq_workers.pluck(:pid).map { |pid| MiqProcess.processInfo(pid) }
end

def monitor_workers
super

Expand Down
55 changes: 28 additions & 27 deletions app/models/miq_server/worker_management/systemd.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
class MiqServer::WorkerManagement::Systemd < MiqServer::WorkerManagement
def cleanup_failed_workers
attr_accessor :miq_services
attr_reader :systemd_manager

def initialize(*)
super

cleanup_failed_systemd_services
require "dbus/systemd"
@systemd_manager = DBus::Systemd::Manager.new
end

def cleanup_failed_systemd_services
failed_service_names = systemd_failed_miq_services.map { |service| service[:name] }
return if failed_service_names.empty?

_log.info("Disabling failed unit files: [#{failed_service_names.join(", ")}]")
systemd_stop_services(failed_service_names)
def sync_from_system
self.miq_services = systemd_services.select { |unit| manageiq_service?(unit) }
end

def systemd_failed_miq_services
miq_services(systemd_failed_services)
end
def cleanup_failed_workers
super

def systemd_all_miq_services
miq_services(systemd_services)
cleanup_failed_systemd_services
end

private
def cleanup_failed_systemd_services
service_names = failed_miq_service_namees
return if service_names.empty?

def systemd_manager
@systemd_manager ||= begin
require "dbus/systemd"
DBus::Systemd::Manager.new
end
_log.info("Disabling failed unit files: [#{service_names.join(", ")}]")
systemd_stop_services(service_names)
end

private

def systemd_stop_services(service_names)
service_names.each do |service_name|
systemd_manager.StopUnit(service_name, "replace")
Expand All @@ -46,14 +45,12 @@ def systemd_unit_dir
Pathname.new("/lib/systemd/system")
end

def miq_services(services)
services.select { |unit| systemd_miq_service_base_names.include?(systemd_service_base_name(unit)) }
def manageiq_service?(unit)
manageiq_service_base_names.include?(systemd_service_base_name(unit))
end

def systemd_miq_service_base_names
@systemd_miq_service_base_names ||= begin
MiqWorkerType.worker_classes.map(&:service_base_name)
end
def manageiq_service_base_names
@manageiq_service_base_names ||= MiqWorkerType.worker_classes.map(&:service_base_name)
end

def systemd_service_name(unit)
Expand All @@ -64,8 +61,12 @@ def systemd_service_base_name(unit)
systemd_service_name(unit).split("@").first
end

def systemd_failed_services
systemd_services.select { |service| service[:active_state] == "failed" }
def failed_miq_services
miq_services.select { |service| service[:active_state] == "failed" }
end

def failed_miq_service_namees
failed_miq_services.map { |service| service[:name] }
end

def systemd_services
Expand Down
18 changes: 12 additions & 6 deletions spec/models/miq_server/worker_management/systemd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
end

context "#cleanup_failed_systemd_services" do
before { server.worker_manager.sync_from_system }

context "with no failed services" do
let(:units) { [{:name => "[email protected]", :description => "ManageIQ Generic Worker", :load_state => "loaded", :active_state => "active", :sub_state => "plugged", :job_id => 0, :job_type => "", :job_object_path => "/"}] }

Expand All @@ -35,7 +37,9 @@
end
end

context "#systemd_all_miq_services" do
context "#sync_from_system" do
before { server.worker_manager.sync_from_system }

let(:units) do
[
{:name => "[email protected]", :active_state => "failed"},
Expand All @@ -45,11 +49,13 @@
end

it "filters out non-miq services" do
expect(server.worker_manager.systemd_all_miq_services.count).to eq(2)
expect(server.worker_manager.miq_services.count).to eq(2)
end
end

context "#systemd_failed_miq_services" do
context "#failed_miq_services (private)" do
before { server.worker_manager.sync_from_system }

let(:units) do
[
{:name => "[email protected]", :active_state => "failed"},
Expand All @@ -58,13 +64,13 @@
end

it "filters out only failed services" do
expect(server.worker_manager.systemd_failed_miq_services.count).to eq(1)
expect(server.worker_manager.send(:failed_miq_services).count).to eq(1)
end
end

context "#systemd_miq_service_base_names (private)" do
context "#manageiq_service_base_names (private)" do
it "returns the minimal_class_name" do
expect(server.worker_manager.send(:systemd_miq_service_base_names)).to include("manageiq-generic", "manageiq-ui")
expect(server.worker_manager.send(:manageiq_service_base_names)).to include("manageiq-generic", "manageiq-ui")
end
end

Expand Down

0 comments on commit 6a13a03

Please sign in to comment.