Dependency trees.

- Were tested for `start` and seem to work fine with them.
  - `stop` still ignores them compteley at the moment. We’ll need to
    remember what has been started as a dependency and what has not to
    make `stop` work with reverse-dependency-trees.
master
Luka Vandervelden 2019-06-08 18:16:23 +02:00
parent 36668ed148
commit f858a9aa11
1 changed files with 41 additions and 7 deletions

View File

@ -167,7 +167,7 @@ class Service
Environment.get environment_name
end
consumes.map do |consume|
@consumes.each do |consume|
@providers[consume.token] = consume.from
end
end
@ -313,21 +313,45 @@ class Service
File.delete "#{path}/#{name}.#{@environment.name}.yaml"
end
# TODO:
# - Improve error handling when a string is not a proper reference to a
# service instance.
def self.get_by_id(id)
matches = id.match /[^\/]*/
unless matches
unless matches # Should not happen, above regex would always match.
raise Exception.new "FIXME"
end
environment_name = matches[0]
service_name = id.sub 0..matches[0].size, ""
service_name = if environment_name == id
environment_name = "root"
id
else
id.sub 0..matches[0].size, ""
end
@@all.find do |service|
service.name == service_name
end
end
alias DependencyTree = Array(DependencyTree) | Service
# Returns a dependency tree.
# Youll probably want to flatten and reverse it afterwards.
def dependency_tree
tree = [self] of DependencyTree
@consumes.each do |token|
service = Service.get_by_id token.from
unless service
# FIXME: Does it make the dep tree invalid?
# FIXME: Only optional deps should be fine.
next
end
tree << service.dependency_tree
end
tree
end
end
args = [] of String
@ -374,8 +398,18 @@ begin
elsif args[0] == "del"
Service.new(args[1], args[2]?).remove "rc"
elsif args[0] == "start"
Service.new(args[1], args[2]?).start "pid", "log"
#service = Service.new(args[1], args[2]?)
service = Service.get_by_id(args[1]).not_nil!
service.dependency_tree.flatten.reverse.each do |service|
if service.status("pid") == Service::Status::Running
next
end
puts "starting #{service.to_s}"
service.start "pid", "log"
end
elsif args[0] == "stop"
# FIXME: Build revdep tree and stop services started as dependencies?
Service.new(args[1], args[2]?).stop "pid"
elsif args[0] == "status"
puts Service.new(args[1], args[2]?).status "pid"