Reverse dependency tree.
- `stop` uses it to stop services that depend upon something that has to be stopped.master
parent
dddf285046
commit
e331828c8b
38
src/main.cr
38
src/main.cr
|
@ -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.
|
||||
# You’ll 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"
|
||||
|
|
Loading…
Reference in New Issue