   stop: () ->
     clearInterval @interval if @interval
 
+  updateHistory: (history) ->
+    validateHistory history
+    @history = history
+    @history = @history.slice Math.max(@history.length-@historysize, 0)
+    debug 'history length', @history.length
+
   nextState: (err, queues, details) ->
     state = nextState @config, @history, queues, details
-    @history.push state
-    @history = @history.slice Math.max(@history.length-@historysize, 0)
-    debug 'history length', @history.length
+    history = @history.concat [ state ]
+    @updateHistory history
     return state
 
   runOnceInternal: (callback) ->

  start: () ->
    runFunc = () =>
      @runOnce (err, state) =>


    interval = setInterval runFunc, @pollinterval
    runFunc() # do first iteration right now

    return callback err, state if err
    return callback null, state

# polyfill for Object.values...
objectValues = (obj) ->
  vals = []
  for key, val of obj
    vals.push val
  return vals

historyStateValid = (s) ->
  return s.error or (s.estimated_workers? and s.current_workers?)
historyEntryValid = (r) ->
  return objectValues(r).every historyStateValid
historyValid = (h) ->
  checks =
    isArray: Array.isArray h
    entries: if h.length then h.every (e) -> historyEntryValid e else true
  return objectValues(checks).every (p) -> p == true

# Throws Error if not valid
exports.validateHistory = validateHistory = (h) ->
  throw new Error "Invalid history object: #{JSON.stringify(h)}" if not historyValid h

class Governor extends EventEmitter
  constructor: (c) ->
    @config = c
