remove cumbersome with-process

This commit is contained in:
Valentin Boettcher 2020-08-03 16:42:52 +02:00
parent 053f982a9d
commit b52a412b7d
2 changed files with 23 additions and 52 deletions

View file

@ -197,9 +197,10 @@
Either `:event` or `:matcher` or both have to be given. `:event`
takes precedence.
"
[supervisor & {:keys [event timeout matcher]
[process & {:keys [event timeout matcher]
:or {timeout +default-timeout+}}]
(let [prom (promise)]
(let [prom (promise)
supervisor (:supervisor process)]
(if (and (not event) (not matcher))
(throw+ {:type ::commander-error
:detail-type ::create-watch-error
@ -269,43 +270,23 @@
[id]
(get @processes id))
;; TODO: defmulti
(defmacro with-process
"A wrapper to access a process either by id or by the process itself.
`proc` can either be a process or a process-id. The process is bound
to `proc-var`.
Returns `false` if no process is found."
[proc proc-var & body]
`(if-let [~proc-var
(cond
(string? ~proc)
(get-process! ~proc)
(instance? process ~proc)
~proc
:default ~proc)]
(do ~@body)
false))
(defn delete-process!
"Deletes a process from the process map, stops it and deletes the unit file.
Returns `true` on success, `false` otherwise."
[process]
(with-process process proc
(debug "Removing process with ID:" (:id proc))
(let [{:keys [unit-name monitor]} proc
[proc]
(debug "Removing process with ID:" (:id proc))
(let [{:keys [unit-name monitor]} proc
[monitor close] monitor]
(close! (:supervisor proc))
(sys/remove-service! unit-name)
(close)
(dosync (commute processes dissoc (:id proc)))
true)))
(close! (:supervisor proc))
(sys/remove-service! unit-name)
(close)
(dosync (commute processes dissoc (:id proc)))
true))
(defn delete-all-processes! []
"Deletes all processes."
(doseq [[id _] @processes]
(delete-process! id)))
(doseq [[_ proc] @processes]
(delete-process! proc)))
(defn get-process-state!
"Queries wether a process is running."
@ -318,12 +299,11 @@
or `:active` or times out after `timeout` ms."
([proc timeout]
(let [prom
(wait-for! (:supervisor proc)
:matcher #(some #{(:event %1)} [:active :failed]))]
(wait-for! proc :matcher #(some #{(:event %1)} [:active :failed]))]
(sys/start-service! (:unit-name proc))
prom))
([proc]
(start-process! process +default-timeout+)))
(start-process! proc +default-timeout+)))
(defn stop-process!
"Stops the service associated to the process."

View file

@ -170,14 +170,14 @@
(testing "waiting for the process to start"
(let [prom (api/wait-for!
(:supervisor proc)
proc
:event :active :timeout 10000)]
(api/start-process! proc)
(is (not (= :timeout @prom)))))
(testing "waiting for the process to fail"
(let [prom (api/wait-for!
(:supervisor proc)
proc
:event :failed
:timeout 100000)]
(api/start-process! proc)
@ -185,7 +185,7 @@
(testing "waiting for the process to activate or fail"
(let [prom (api/wait-for!
(:supervisor proc)
proc
:matcher #(or (= (:event %1) :active)
(= (:event %1) :failed))
:timeout 1000)]
@ -200,25 +200,19 @@
(testing "waiting for a timeout"
(let [prom (api/wait-for!
(:supervisor proc)
proc
:event :one
:timeout 100)
prom1 (api/wait-for!
(:supervisor proc)
proc
:matcher #(and % false)
:timeout 100)]
(is (= :timeout @prom))
(is (= :timeout @prom1))))
(testing "the with-process macro"
(api/with-process (:id proc) new-proc
(is (= proc new-proc)))
(is (not (api/with-process "none" new-proc
true))))
(testing "stopping the process"
(let [prom (api/wait-for!
(:supervisor proc)
proc
:matcher #(or (= (:event %1) :inactive)
(= (:event %1) :failed)))]
(api/start-process! proc)
@ -237,7 +231,7 @@
(is (= (:id proc) (:id (a/<!! c))))))
(testing "deleting the process"
(is (api/delete-process! (:id proc)))
(is (api/delete-process! proc))
(is (not (api/get-process! (:id proc))))))
(testing "creating two processes"
@ -253,7 +247,4 @@
(testing "deleting all processes"
(api/delete-all-processes!)
(is (= 0 (count @@#'api/processes))))
(testing "deleting non-existent process"
(is (not (api/delete-process! "nonexistent"))))))
(is (= 0 (count @@#'api/processes))))))