class CF::App::Start

Constants

APP_CHECK_LIMIT

Public Instance Methods

start() click to toggle source
# File lib/cf/cli/app/start.rb, line 13
def start
  apps = input[:all] ? client.apps : input[:apps]
  fail "No applications given." if apps.empty?

  spaced(apps) do |app|
    app = filter(:start_app, app)

    switch_mode(app, input[:debug_mode])

    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.nil? && app.debug_mode != "none" && !quiet?
      line
      invoke :instances, :app => app
    end
  end
end

Private Instance Methods

all_instances_running?(instances) click to toggle source
# File lib/cf/cli/app/start.rb, line 135
def all_instances_running?(instances)
  instances.all? { |i| i.state == "RUNNING" }
end
any_instance_flapping?(instances) click to toggle source
# File lib/cf/cli/app/start.rb, line 139
def any_instance_flapping?(instances)
  instances.any? { |i| i.state == "FLAPPING" }
end
check_application(app) click to toggle source
# File lib/cf/cli/app/start.rb, line 86
def check_application(app)
  if app.debug == "suspend"
    line "Application is in suspended debugging mode."
    line "It will wait for you to attach to it before starting."
    return
  end

  print("Checking status of app '#{c(app.name, :name)}'...")

  seconds = 0
  @first_time_after_staging_succeeded = true

  begin
    instances = []
    while true
      if any_instance_flapping?(instances) || seconds == APP_CHECK_LIMIT
        err "Push unsuccessful."
        line "#{c("TIP: The system will continue to attempt restarting all requested app instances that have crashed. Try 'truck app' to monitor app status. To troubleshoot crashes, try 'truck events' and 'truck crashlogs'.", :warning)}"
        return
      end

      begin
        return unless instances = app.instances

        indented { print_instances_summary(instances) }

        if one_instance_running?(instances)
          line "#{c("Push successful! App '#{app.name}' available at #{app.host}.#{app.domain}", :good)}"
          unless all_instances_running?(instances)
            line "#{c("TIP: The system will continue to start all requested app instances. Try 'truck app' to monitor app status.", :warning)}"
          end
          return
        end
      rescue CFoundry::NotStaged
        print (".")
      end

      sleep 1
      seconds += 1
    end
  rescue CFoundry::StagingError
    err "Application failed to stage"
  end
end
for_output(state) click to toggle source
# File lib/cf/cli/app/start.rb, line 170
def for_output(state)
  state = "CRASHING" if state == "FLAPPING"
  state.downcase
end
one_instance_running?(instances) click to toggle source
# File lib/cf/cli/app/start.rb, line 131
def one_instance_running?(instances)
  instances.any? { |i| i.state == "RUNNING" }
end
print_instances_summary(instances) click to toggle source
start_app(app) click to toggle source
# File lib/cf/cli/app/start.rb, line 40
def start_app(app)
  log = nil
  with_progress("Preparing to start #{c(app.name, :name)}") do
    app.start! do |url|
      log = url
    end
  end
  log
end
stream_start_log(log) click to toggle source
# File lib/cf/cli/app/start.rb, line 50
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
switch_mode(app, mode) click to toggle source

set app debug mode, ensuring it's valid, and shutting it down

# File lib/cf/cli/app/start.rb, line 66
def switch_mode(app, mode)
  mode = "run" if mode == "" # no value given

  return false if app.debug == mode

  if mode == "none"
    with_progress("Removing debug mode") do
      app.debug = mode
      app.stop! if app.started?
    end

    return true
  end

  with_progress("Switching mode to #{c(mode, :name)}") do |s|
    app.debug = mode
    app.stop! if app.started?
  end
end