/
manifold_sql.clj
35 lines (31 loc) · 1.31 KB
/
manifold_sql.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(ns ablauf.job.manifold-sql
(:require [clojure.tools.logging :as log]
[manifold.deferred :as d]
[ablauf.job.sql :as sql]))
(defn make-sql-runner
"SQL runner compatible with the manifold one, useful to test against both."
[db-spec]
(fn sql-runner [store ast {:keys [action-fn id context]}]
(let [the-id (or id (random-uuid))
get-job (fn [] (sql/workflow-by-uuid db-spec the-id))
job-done? (fn [] (some? (#{"success" "failure"}
(:workflow_run/status (get-job)))))]
;; submit the job
(d/future
(try
(sql/submit db-spec store ast {:context context :uuid the-id})
(sql/worker db-spec store {:action-fn action-fn} 100 900 job-done?)
(log/info "done processing job")
(let [r (get-job)]
(cond
(nil? r)
(throw (ex-info "workflow run is gone!" {}))
(= "success" (:workflow_run/status r))
(:workflow_run/job r)
(= "failure" (:workflow_run/status r))
(throw (ex-info "action error" {:data (:workflow_run/job r)}))
:else
(throw (ex-info "unknown workflow run state!" {}))))
(catch Throwable e
(log/error e "worker error")
(throw e)))))))