mirror of
https://github.com/vale981/stream
synced 2025-03-05 18:11:39 -05:00
remove cumbersome with-process
This commit is contained in:
parent
053f982a9d
commit
b52a412b7d
2 changed files with 23 additions and 52 deletions
|
@ -197,9 +197,10 @@
|
||||||
Either `:event` or `:matcher` or both have to be given. `:event`
|
Either `:event` or `:matcher` or both have to be given. `:event`
|
||||||
takes precedence.
|
takes precedence.
|
||||||
"
|
"
|
||||||
[supervisor & {:keys [event timeout matcher]
|
[process & {:keys [event timeout matcher]
|
||||||
:or {timeout +default-timeout+}}]
|
:or {timeout +default-timeout+}}]
|
||||||
(let [prom (promise)]
|
(let [prom (promise)
|
||||||
|
supervisor (:supervisor process)]
|
||||||
(if (and (not event) (not matcher))
|
(if (and (not event) (not matcher))
|
||||||
(throw+ {:type ::commander-error
|
(throw+ {:type ::commander-error
|
||||||
:detail-type ::create-watch-error
|
:detail-type ::create-watch-error
|
||||||
|
@ -269,30 +270,10 @@
|
||||||
[id]
|
[id]
|
||||||
(get @processes 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!
|
(defn delete-process!
|
||||||
"Deletes a process from the process map, stops it and deletes the unit file.
|
"Deletes a process from the process map, stops it and deletes the unit file.
|
||||||
Returns `true` on success, `false` otherwise."
|
Returns `true` on success, `false` otherwise."
|
||||||
[process]
|
[proc]
|
||||||
(with-process process proc
|
|
||||||
(debug "Removing process with ID:" (:id proc))
|
(debug "Removing process with ID:" (:id proc))
|
||||||
(let [{:keys [unit-name monitor]} proc
|
(let [{:keys [unit-name monitor]} proc
|
||||||
[monitor close] monitor]
|
[monitor close] monitor]
|
||||||
|
@ -300,12 +281,12 @@
|
||||||
(sys/remove-service! unit-name)
|
(sys/remove-service! unit-name)
|
||||||
(close)
|
(close)
|
||||||
(dosync (commute processes dissoc (:id proc)))
|
(dosync (commute processes dissoc (:id proc)))
|
||||||
true)))
|
true))
|
||||||
|
|
||||||
(defn delete-all-processes! []
|
(defn delete-all-processes! []
|
||||||
"Deletes all processes."
|
"Deletes all processes."
|
||||||
(doseq [[id _] @processes]
|
(doseq [[_ proc] @processes]
|
||||||
(delete-process! id)))
|
(delete-process! proc)))
|
||||||
|
|
||||||
(defn get-process-state!
|
(defn get-process-state!
|
||||||
"Queries wether a process is running."
|
"Queries wether a process is running."
|
||||||
|
@ -318,12 +299,11 @@
|
||||||
or `:active` or times out after `timeout` ms."
|
or `:active` or times out after `timeout` ms."
|
||||||
([proc timeout]
|
([proc timeout]
|
||||||
(let [prom
|
(let [prom
|
||||||
(wait-for! (:supervisor proc)
|
(wait-for! proc :matcher #(some #{(:event %1)} [:active :failed]))]
|
||||||
:matcher #(some #{(:event %1)} [:active :failed]))]
|
|
||||||
(sys/start-service! (:unit-name proc))
|
(sys/start-service! (:unit-name proc))
|
||||||
prom))
|
prom))
|
||||||
([proc]
|
([proc]
|
||||||
(start-process! process +default-timeout+)))
|
(start-process! proc +default-timeout+)))
|
||||||
|
|
||||||
(defn stop-process!
|
(defn stop-process!
|
||||||
"Stops the service associated to the process."
|
"Stops the service associated to the process."
|
||||||
|
|
|
@ -170,14 +170,14 @@
|
||||||
|
|
||||||
(testing "waiting for the process to start"
|
(testing "waiting for the process to start"
|
||||||
(let [prom (api/wait-for!
|
(let [prom (api/wait-for!
|
||||||
(:supervisor proc)
|
proc
|
||||||
:event :active :timeout 10000)]
|
:event :active :timeout 10000)]
|
||||||
(api/start-process! proc)
|
(api/start-process! proc)
|
||||||
(is (not (= :timeout @prom)))))
|
(is (not (= :timeout @prom)))))
|
||||||
|
|
||||||
(testing "waiting for the process to fail"
|
(testing "waiting for the process to fail"
|
||||||
(let [prom (api/wait-for!
|
(let [prom (api/wait-for!
|
||||||
(:supervisor proc)
|
proc
|
||||||
:event :failed
|
:event :failed
|
||||||
:timeout 100000)]
|
:timeout 100000)]
|
||||||
(api/start-process! proc)
|
(api/start-process! proc)
|
||||||
|
@ -185,7 +185,7 @@
|
||||||
|
|
||||||
(testing "waiting for the process to activate or fail"
|
(testing "waiting for the process to activate or fail"
|
||||||
(let [prom (api/wait-for!
|
(let [prom (api/wait-for!
|
||||||
(:supervisor proc)
|
proc
|
||||||
:matcher #(or (= (:event %1) :active)
|
:matcher #(or (= (:event %1) :active)
|
||||||
(= (:event %1) :failed))
|
(= (:event %1) :failed))
|
||||||
:timeout 1000)]
|
:timeout 1000)]
|
||||||
|
@ -200,25 +200,19 @@
|
||||||
|
|
||||||
(testing "waiting for a timeout"
|
(testing "waiting for a timeout"
|
||||||
(let [prom (api/wait-for!
|
(let [prom (api/wait-for!
|
||||||
(:supervisor proc)
|
proc
|
||||||
:event :one
|
:event :one
|
||||||
:timeout 100)
|
:timeout 100)
|
||||||
prom1 (api/wait-for!
|
prom1 (api/wait-for!
|
||||||
(:supervisor proc)
|
proc
|
||||||
:matcher #(and % false)
|
:matcher #(and % false)
|
||||||
:timeout 100)]
|
:timeout 100)]
|
||||||
(is (= :timeout @prom))
|
(is (= :timeout @prom))
|
||||||
(is (= :timeout @prom1))))
|
(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"
|
(testing "stopping the process"
|
||||||
(let [prom (api/wait-for!
|
(let [prom (api/wait-for!
|
||||||
(:supervisor proc)
|
proc
|
||||||
:matcher #(or (= (:event %1) :inactive)
|
:matcher #(or (= (:event %1) :inactive)
|
||||||
(= (:event %1) :failed)))]
|
(= (:event %1) :failed)))]
|
||||||
(api/start-process! proc)
|
(api/start-process! proc)
|
||||||
|
@ -237,7 +231,7 @@
|
||||||
(is (= (:id proc) (:id (a/<!! c))))))
|
(is (= (:id proc) (:id (a/<!! c))))))
|
||||||
|
|
||||||
(testing "deleting the process"
|
(testing "deleting the process"
|
||||||
(is (api/delete-process! (:id proc)))
|
(is (api/delete-process! proc))
|
||||||
(is (not (api/get-process! (:id proc))))))
|
(is (not (api/get-process! (:id proc))))))
|
||||||
|
|
||||||
(testing "creating two processes"
|
(testing "creating two processes"
|
||||||
|
@ -253,7 +247,4 @@
|
||||||
|
|
||||||
(testing "deleting all processes"
|
(testing "deleting all processes"
|
||||||
(api/delete-all-processes!)
|
(api/delete-all-processes!)
|
||||||
(is (= 0 (count @@#'api/processes))))
|
(is (= 0 (count @@#'api/processes))))))
|
||||||
|
|
||||||
(testing "deleting non-existent process"
|
|
||||||
(is (not (api/delete-process! "nonexistent"))))))
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue