Reverse dependency tree.
- `stop` uses it to stop services that depend upon something that has to be stopped.master
parent
dddf285046
commit
e331828c8b
34
src/main.cr
34
src/main.cr
|
@ -195,6 +195,14 @@ class Service
|
||||||
}.to_yaml
|
}.to_yaml
|
||||||
end
|
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.
|
# FIXME: At this point, macros would be both more readable and shorter.
|
||||||
def name
|
def name
|
||||||
@reference.name
|
@reference.name
|
||||||
|
@ -340,13 +348,16 @@ class Service
|
||||||
service.name == service_name
|
service.name == service_name
|
||||||
end
|
end
|
||||||
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.
|
# Returns a dependency tree.
|
||||||
# You’ll probably want to flatten and reverse it afterwards.
|
# You’ll probably want to flatten and reverse it afterwards.
|
||||||
def dependency_tree
|
def dependency_tree
|
||||||
tree = [self] of DependencyTree
|
tree = [self] of ServiceTree
|
||||||
|
|
||||||
@consumes.each do |token|
|
@consumes.each do |token|
|
||||||
service = Service.get_by_id token.from
|
service = Service.get_by_id token.from
|
||||||
|
@ -362,6 +373,20 @@ class Service
|
||||||
|
|
||||||
tree
|
tree
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
args = [] of String
|
args = [] of String
|
||||||
|
@ -442,10 +467,15 @@ begin
|
||||||
services.each do |service|
|
services.each do |service|
|
||||||
# FIXME: Build revdep tree and stop services started as dependencies?
|
# FIXME: Build revdep tree and stop services started as dependencies?
|
||||||
if service.status(PID_DIRECTORY) == Service::Status::Running
|
if service.status(PID_DIRECTORY) == Service::Status::Running
|
||||||
|
# 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}"
|
puts "stopping #{service.to_s}"
|
||||||
service.stop PID_DIRECTORY
|
service.stop PID_DIRECTORY
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
elsif args[0] == "status"
|
elsif args[0] == "status"
|
||||||
puts Service.new(args[1], args[2]?).status PID_DIRECTORY
|
puts Service.new(args[1], args[2]?).status PID_DIRECTORY
|
||||||
elsif args[0] == "show"
|
elsif args[0] == "show"
|
||||||
|
|
Loading…
Reference in New Issue