 # This file contains helper functions meant to access the main cluster, parse
 # output from fleetctl and deliver status information to the Huxley API server.
 
-{async, shell, sleep, collect, map} = require "fairmont"
+{async, shell, sleep, collect, map, keys} = require "fairmont"
 api = require "./api"
 
 

  # Parse the output for structured data.
  data = stdout.split("\n")[1..]
  status = {}
  console.log 2.5, services
  console.log 2.6, data
  for line in data
    continue if line == ""

    fields = line.split "\t"
    name = fields[0][...-8]
    console.log 2.7, name

    # Save service status, ignoring those not part of this deployment.
    if name in services
      status[name] = fields[2]
    else
      continue


# Monitor the services as they spin-up.
monitor = async (context, services) ->
  {cluster} = context
  while true
    # Read the status of all services
    status = yield get_status cluster, services

    # Check for success. All services must be "active" to pass, but a single
    # failure ruins the whole thing.
    is_active = (x) -> x == "active"
    is_failed = (x) -> x == "failed"
    success = collect map is_active, status
    failure = collect map is_failed, status

    if true in failure
      return false        # Failure detected. All is lost, haha.
    else if false !in success
      return true         # *All* services are online and ready.
    else
      yield sleep 10000   # Continue polling.

module.exports = {monitor}
