Reverse dependency tree.

- `stop` uses it to stop services that depend upon something that has
    to be stopped.
master
Luka Vandervelden 2019-06-08 19:01:31 +02:00
parent dddf285046
commit e331828c8b
1 changed files with 34 additions and 4 deletions

View File

@ -195,6 +195,14 @@ class Service
}.to_yaml
end
def id
if @environment.name == "root"
name
else
"#{@environment.name}/#{name}"
end
end
# FIXME: At this point, macros would be both more readable and shorter.
def name
@reference.name
@ -340,13 +348,16 @@ class Service
service.name == service_name
end
end
def is_id?(id)
id == self.id || (@environment.name == "root" && id == "root/#{name}")
end
alias DependencyTree = Array(DependencyTree) | Service
alias ServiceTree = Array(ServiceTree) | Service
# Returns a dependency tree.
# Youll probably want to flatten and reverse it afterwards.
def dependency_tree
tree = [self] of DependencyTree
tree = [self] of ServiceTree
@consumes.each do |token|
service = Service.get_by_id token.from
@ -362,6 +373,20 @@ class Service
tree
end
def reverse_dependency_tree
tree = [self] of ServiceTree
@@all.each do |service|
service.providers.any? do |token, id|
if self.is_id?(id) && ! tree.any? service
tree << service
end
end
end
tree
end
end
args = [] of String
@ -442,8 +467,13 @@ begin
services.each do |service|
# FIXME: Build revdep tree and stop services started as dependencies?
if service.status(PID_DIRECTORY) == Service::Status::Running
puts "stopping #{service.to_s}"
service.stop PID_DIRECTORY
# FIXME: Should we remove duplicate services from the
# tree once flattened?
service.reverse_dependency_tree.flatten.reverse.each do |service|
puts "stopping #{service.to_s}"
service.stop PID_DIRECTORY
end
end
end
elsif args[0] == "status"