 (defn load-app-instances [app] (redis! (car/smembers (str app ":instances"))))
 (defn add-app-instance [app instance] (redis! (car/sadd (str app ":instances") instance) (notify-routers)))
 (defn remove-app-instance [app instance] (redis! (car/srem (str app ":instances") instance) (notify-routers)))
+
+(defn load-pending-app-instances [app] (redis! (car/smembers (str app ":pending-instances"))))
+(defn add-pending-app-instance [app instance] (redis! (car/sadd (str app ":pending-instances") instance)))
+(defn remove-pending-app-instance [app instance] (redis! (car/srem (str app ":pending-instances") instance)))
 
 (defn load-hosts [] (redis! (car/smembers :hosts)))
 (defn add-host [host] (redis! (car/sadd :hosts host)))


(defn deploy-app-instances [app image count internal-port]
  (println "Deploying" count app "with" image)
  (let [instances (load-app-instances app)]
    (deploy-new-app-instances app image count internal-port)
    (save-deployments app image count)
    (doseq [instance instances]
      (let [[image tag] (ssplit instance)]
        (kill-app-instance app image tag)))))

(defn load-app-logs [app]
  (vec

        (stop-container host id)
        (throw (Exception. "Failed to deploy new instance.")))
      (try
        (println "Adding" (str host ":" port) "to as pending")
        (add-pending-app-instance app (str host ":" port))
        (catch Exception e
          (println "Deploy failed. Rolling back.")
          (try (kill-app-instance app host port)
