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
parent
36668ed148
commit
f858a9aa11
48
src/main.cr
48
src/main.cr
|
@ -167,7 +167,7 @@ class Service
|
||||||
Environment.get environment_name
|
Environment.get environment_name
|
||||||
end
|
end
|
||||||
|
|
||||||
consumes.map do |consume|
|
@consumes.each do |consume|
|
||||||
@providers[consume.token] = consume.from
|
@providers[consume.token] = consume.from
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -313,21 +313,45 @@ class Service
|
||||||
File.delete "#{path}/#{name}.#{@environment.name}.yaml"
|
File.delete "#{path}/#{name}.#{@environment.name}.yaml"
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO:
|
|
||||||
# - Improve error handling when a string is not a proper reference to a
|
|
||||||
# service instance.
|
|
||||||
def self.get_by_id(id)
|
def self.get_by_id(id)
|
||||||
matches = id.match /[^\/]*/
|
matches = id.match /[^\/]*/
|
||||||
unless matches
|
unless matches # Should not happen, above regex would always match.
|
||||||
raise Exception.new "FIXME"
|
raise Exception.new "FIXME"
|
||||||
end
|
end
|
||||||
environment_name = matches[0]
|
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|
|
@@all.find do |service|
|
||||||
service.name == service_name
|
service.name == service_name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias DependencyTree = Array(DependencyTree) | Service
|
||||||
|
|
||||||
|
# Returns a dependency tree.
|
||||||
|
# You’ll 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
|
end
|
||||||
|
|
||||||
args = [] of String
|
args = [] of String
|
||||||
|
@ -374,8 +398,18 @@ begin
|
||||||
elsif args[0] == "del"
|
elsif args[0] == "del"
|
||||||
Service.new(args[1], args[2]?).remove "rc"
|
Service.new(args[1], args[2]?).remove "rc"
|
||||||
elsif args[0] == "start"
|
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"
|
elsif args[0] == "stop"
|
||||||
|
# FIXME: Build revdep tree and stop services started as dependencies?
|
||||||
Service.new(args[1], args[2]?).stop "pid"
|
Service.new(args[1], args[2]?).stop "pid"
|
||||||
elsif args[0] == "status"
|
elsif args[0] == "status"
|
||||||
puts Service.new(args[1], args[2]?).status "pid"
|
puts Service.new(args[1], args[2]?).status "pid"
|
||||||
|
|
Loading…
Reference in New Issue