 (defn kill-app-instance [app host port]
   (println "Killing instance at" (str host ":" port))
   (stop-container-by-port host port)
-  (remove-app-instance app (str host ":" port))
-  (mark-host-health (str host ":" port) true))
+  (remove-app-instance app (str host ":" port)))
 
 (defn deploy-app-instance [app host port internal-port image]
   (println "Pulling new tags for" image)

  (when-let [id (:Id (load-container-by-host-and-port host port))]
    (stop-container host id)))

(defn health-check-host [host port]
  (mark-host-health (str host ":" port)
   (loop [n 15]
     (when (pos? n)
       (if (= 200 (:status @(http/get (str "http://" host ":" port "/") {:timeout 5000})))
         true
         (do
           (Thread/sleep (* (- 15 n) 500))
           (recur (dec n))))))))

(defn health-check-instances []
  (doseq [l (vals (:instances @router))]
    (doseq [i l]
      (let [[host port] (ssplit i)]
        (health-check-host host port)))))

(defn pull-docker-image [host image]
  (let [[image tag] (ssplit image)]

(defn remove-app-env [app env] (redis! (car/hdel (str app ":envs") env)))
(defn load-app-envs [app] (apply hash-map (redis! (car/hgetall (str app ":envs")))))

(defn load-app-instances [app] (redis! (car/lrange (str "frontend:" app) 0 -1)))
(defn add-app-instance [app instance] (redis! (car/rpush (str "frontend:" app) instance)))
(defn remove-app-instance [app instance] (redis! (car/lrem (str "frontend:" app) 0 instance)))

(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)))
