 (ns lambdacd.runners
+  "Runners are what keeps a pipeline going. The start new builds based on some logic,
+   e.g. when the previous build is finished or (e.g if the first step is a trigger) after the first step is done."
   (:require [lambdacd.execution.core :as execution]
             [clojure.core.async :as async]
             [lambdacd.event-bus :as event-bus]

  should-trigger-next-build?
  (every-pred is-first-step? is-not-retriggered?))

(defmacro while-not-stopped [ctx & body]
  `(let [running# (atom true)]
     (async/go
       (async/<! (event-bus/subscribe ~ctx :stop-runner))

        (async/<!! first-steps-finisehd)))))

(defn start-one-run-after-another
  "Runner that always keeps one pipeline-run active. It waits for a run to complete, then starts a new one.
  Commonly used when running multiple pipelines in parallel is not an option or the pipeline has no trigger."
  [{pipeline-def :pipeline-def context :context}]
 (async/thread
   (while-not-stopped context
     (execution/run-pipeline pipeline-def context))))

(defn stop-runner
  "Triggers an event that will stop runners so they no longer start new pipeline-runs.
  Does not stop running pipelines and does not wait for runners to stop."
  [ctx]
  (log/info "Stopping runner...")
  (event-bus/publish!! ctx :stop-runner {}))
