diff --git a/src/daemon/funko.cr b/src/daemon/funko.cr index 8d8f433..965457e 100644 --- a/src/daemon/funko.cr +++ b/src/daemon/funko.cr @@ -73,6 +73,9 @@ module Funko end end + # Endpoints for the web frontend + + # General status for the front page get "/funkos/" do |env| funkos = Funko.from_docker funkos.sort! { |a, b| a.name <=> b.name } @@ -93,7 +96,35 @@ module Funko result.to_json end end + # Stop => scale to 0 + get "/funkos/:name/stop" do |env| + name = env.params.url["name"] + funko = Funko.from_names([name])[0] + funko.scale(0) + funko.wait_for(0, 1) + end + # Start => scale to 1 + get "/funkos/:name/start" do |env| + name = env.params.url["name"] + funko = Funko.from_names([name])[0] + if funko.scale == 0 + funko.scale(1) + funko.wait_for(1, 1) + end + end + + # Restart => scale to 0, then 1 + get "/funkos/:name/restart" do |env| + name = env.params.url["name"] + funko = Funko.from_names([name])[0] + funko.scale(0) + funko.wait_for(0, 1) + funko.scale(1) + funko.wait_for(1, 1) + end + + # Helper to run faaso locally and get a response back def run_faaso(args : Array(String)) Log.info { "Running faaso [#{args.join(", ")}, -l]" } output = IO::Memory.new diff --git a/src/funko.cr b/src/funko.cr index 9c43a7c..aaa28f0 100644 --- a/src/funko.cr +++ b/src/funko.cr @@ -234,25 +234,21 @@ module Funko end # Wait up to `t` seconds for the funko to reach the requested `state` - def wait_for(state : String, t) + def wait_for(new_scale : Int, t) channel = Channel(Nil).new spawn do - sleep 0.1.seconds - case state - when "exited" - if self.exited? - channel.send(nil) - end - when "running" - if self.running? - channel.send(nil) - end - when "paused" - if self.paused? - channel.send(nil) - end + loop do + channel.send(nil) if scale == new_scale + sleep 0.2.seconds end end + + select + when channel.receive + Log.info { "Funko #{name} reached scale #{new_scale}" } + when timeout(t.seconds) + Log.error { "Funko #{name} did not reach scale #{new_scale} in #{t} seconds" } + end end # Create a container for this funko diff --git a/tinyproxy.conf b/tinyproxy.conf index 7931997..dc36489 100644 --- a/tinyproxy.conf +++ b/tinyproxy.conf @@ -8,3 +8,4 @@ ReverseOnly Yes ReverseMagic Yes ReversePath "/admin/" "http://127.0.0.1:3000/" + ReversePath "/faaso/hello-fkvrm2/" "http://hello-fkvrm2:3000/" \ No newline at end of file