add restart logic and slimline

This commit is contained in:
Valentin Boettcher 2020-08-03 18:00:38 +02:00
parent 432223bced
commit 105f8c1f80
2 changed files with 38 additions and 17 deletions

View file

@ -65,6 +65,7 @@
[update id] [update id]
(condp = (:ActiveState update) (condp = (:ActiveState update)
"failed" {:event :failed "failed" {:event :failed
:reason (keyword (:SubState update))
;; we take the last message because the journal does not ;; we take the last message because the journal does not
;; distinct between message types from the executable ;; distinct between message types from the executable
:error :error
@ -75,10 +76,10 @@
(:unit-name process) (:unit-name process)
:number 1 :number 1
:executable (:ffmpeg-path (:ffmpeg-config process))))))} :executable (:ffmpeg-path (:ffmpeg-config process))))))}
"active" {:event :active} "active" {:event :active :reason (keyword (:SubState update))}
"activating" {:event :activating} "activating" {:event :activating :reason (keyword (:SubState update))}
"deactivating" {:event :deactivating} "deactivating" {:event :deactivating :reason (keyword (:SubState update))}
"inactive" {:event :inactive} "inactive" {:event :inactive :reason (keyword (:SubState update))}
nil)) nil))
(defn- put-process-event! (defn- put-process-event!
@ -210,6 +211,11 @@
:event event :promise prom :timeout timeout})) :event event :promise prom :timeout timeout}))
prom)) prom))
(defn multi-event-matcher
"Matches any of the given events."
[& events]
#(some #{(:event %1)} [:inactive :failed]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Process Management ; ; Process Management ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -294,21 +300,37 @@
(sys/get-service-state! (:unit-name proc))) (sys/get-service-state! (:unit-name proc)))
(defn start-process! (defn start-process!
"Starts the service associated to the process `proc` (either id or "Starts the service associated to the process `proc`. Returns a
process object). Returns a promise that resolves to event `:failed` promise that resolves to event `:failed` or `:active` or times out
or `:active` or times out after `timeout` ms." after `timeout` ms."
([proc timeout] ([proc timeout]
(let [prom (let [prom
(wait-for! proc :matcher #(some #{(:event %1)} [:active :failed]))] (wait-for! proc :matcher (multi-event-matcher [:active :failed]))]
(sys/start-service! (:unit-name proc)) (sys/start-service! (:unit-name proc))
prom)) prom))
([proc] ([proc]
(start-process! proc +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 `proc`. Returns a promise
[proc] that resolves to event `:failed` or `:inactive` or times out after
(sys/stop-service! (:unit-name proc))) `timeout` ms."
([proc timeout]
(let [prom
(wait-for! proc :matcher (multi-event-matcher [:inactive :failed]))]
(sys/stop-service! (:unit-name proc))
prom))
([proc]
(start-process! proc +default-timeout+)))
(defn restart-process!
"Restarts a process `proc` and wait for stop and start to happen
within `timeout`."
([proc timeout]
@(stop-process! proc timeout)
(start-process! proc timeout))
([proc]
(restart-process! proc +default-timeout+)))
(defn process-running? (defn process-running?
"Queries wether a process is running." "Queries wether a process is running."

View file

@ -211,15 +211,14 @@
(is (= :timeout @prom1)))) (is (= :timeout @prom1))))
(testing "stopping the process" (testing "stopping the process"
(let [prom (api/wait-for! @(api/start-process! proc)
proc (let [prom (api/stop-process! proc)]
:matcher #(or (= (:event %1) :inactive)
(= (:event %1) :failed)))]
(api/start-process! proc)
(api/stop-process! proc)
(is (not (= :timeout @prom))) (is (not (= :timeout @prom)))
(is (not (api/process-running? proc))))) (is (not (api/process-running? proc)))))
(testing "re starting a process"
(is (not (= :timeout (:event @(api/restart-process! proc))))))
(testing "enabling the process" (testing "enabling the process"
(api/enable-process! proc) (api/enable-process! proc)
(is (api/process-enabled? proc))) (is (api/process-enabled? proc)))