lib/vmc/cli/app/start.rb in vmc-0.5.0.rc1 vs lib/vmc/cli/app/start.rb in vmc-0.5.0.rc2

- old
+ new

@@ -17,29 +17,53 @@ spaced(apps) do |app| app = filter(:start_app, app) switch_mode(app, input[:debug_mode]) - with_progress("Starting #{c(app.name, :name)}") do |s| - if app.started? - s.skip do - err "Already started." - end - end - - app.start! + if app.started? + err "Application #{b(app.name)} is already started." + next end + log = start_app(app) + stream_start_log(log) if log check_application(app) if app.debug_mode && !quiet? line invoke :instances, :app => app end end end + private + + def start_app(app) + log = nil + with_progress("Starting #{c(app.name, :name)}") do + app.start!(true) do |url| + log = url + end + end + log + end + + def stream_start_log(log) + offset = 0 + + while true + begin + client.stream_url(log + "&tail&tail_offset=#{offset}") do |out| + offset += out.size + print out + end + rescue Timeout::Error + end + end + rescue CFoundry::APIError + end + # set app debug mode, ensuring it's valid, and shutting it down def switch_mode(app, mode) mode = nil if mode == "none" mode = "run" if mode == "debug_mode" # no value given @@ -66,28 +90,61 @@ end end end def check_application(app) - with_progress("Checking #{c(app.name, :name)}") do |s| - if app.debug_mode == "suspend" - s.skip do - line "Application is in suspended debugging mode." - line "It will wait for you to attach to it before starting." - end + if app.debug_mode == "suspend" + line "Application is in suspended debugging mode." + line "It will wait for you to attach to it before starting." + return + end + + line "Checking #{c(app.name, :name)}..." + + seconds = 0 + while instances = app.instances + indented { print_instances_summary(instances) } + + if all_instances_running?(instances) + line "#{c("OK", :good)}" + return end - seconds = 0 - until app.healthy? - sleep 1 - seconds += 1 - if seconds == APP_CHECK_LIMIT - s.give_up do - err "Application failed to start." - # TODO: print logs - end - end + if any_instance_flapping?(instances) || seconds == APP_CHECK_LIMIT + err "Application failed to start." + return end + + sleep 1 + seconds += 1 end + end + + def all_instances_running?(instances) + instances.all? { |i| i.state == "RUNNING" } + end + + def any_instance_flapping?(instances) + instances.any? { |i| i.state == "FLAPPING" } + end + + def print_instances_summary(instances) + counts = Hash.new { 0 } + instances.each do |i| + counts[i.state] += 1 + end + + states = [] + %w{RUNNING STARTING DOWN FLAPPING}.each do |state| + if (num = counts[state]) > 0 + states << "#{b(num)} #{c(state.downcase, state_color(state))}" + end + end + + total = instances.count + running = counts["RUNNING"].to_s.rjust(total.to_s.size) + + ratio = "#{running}#{d("/")}#{total} instances:" + line "#{ratio} #{states.join(", ")}" end end end