diff --git a/src/stream/commander/api.clj b/src/stream/commander/api.clj index 5b00e70..0195bd2 100644 --- a/src/stream/commander/api.clj +++ b/src/stream/commander/api.clj @@ -90,7 +90,7 @@ ; Error Diagnose ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(defn diagnose-error +(defn diagnose-runtime-error "Deduces the problem from the error `message` and the process information `proc` and returns it. @@ -122,10 +122,37 @@ :else :unknown)) +(defn diagnose-start-error + "Deduces the problem from the error `message` and the process + information `proc` and returns it. + + The returned codes are: + " + [message _] + (cond + (string/includes? message "No such file") :ffmpeg-not-found + :else :unknown)) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Monitoring ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(defn- handle-process-error! + [event id] + (when-let [proc (get-process! id)] + (let [problem + (case (:detail-type event) + :nonzero-exit (diagnose-runtime-error (:stderr (:details event)) proc) + :start-failed (diagnose-start-error (:message (:details event)) proc) + nil)] + + (dosync + (alter processes + (fn [procs] + (let [proc (get procs id) + updated-proc (assoc proc :problems (conj (:problems proc) problem))] + (assoc procs id updated-proc)))))))) + (defn- put-process-event! [id data] (a/put! master-monitor {:type :process-event @@ -135,12 +162,8 @@ (defn- handle-status-event! "Handles status `event`s from the process with the `id`." [id event] - (println "------------------------------>" event - (case (:type event) - :error (when (= :nonzero-exit (:detail-type :nonzero-exit)) - (when-let [proc (get-process! id)] - (diagnose-error (:stderr (:details event)) proc))) - :else nil)) + (case (:type event) + :error (handle-process-error! event id)) (put-process-event! id event)) ;; TODO: specs diff --git a/src/stream/commander/processes.clj b/src/stream/commander/processes.clj index c45a404..71c269a 100644 --- a/src/stream/commander/processes.clj +++ b/src/stream/commander/processes.clj @@ -119,7 +119,7 @@ (do (a/put! status (error-status err :start-failed - :stderr err)) + :message err)) (deliver prom {:success false :error err}) nil))))) diff --git a/test/stream/commander_test.clj b/test/stream/commander_test.clj index 282449a..8c5d5e7 100644 --- a/test/stream/commander_test.clj +++ b/test/stream/commander_test.clj @@ -27,6 +27,7 @@ (let [config {:cam-ip "0.0.0.0" :cam-rtsp-port "554" :profile "bla" + :ffmpeg-path "nonex" :stream-server "server" :stream-key "key"}] ;; TODO: proper spec testing @@ -41,6 +42,10 @@ (testing "starting a process (which will fail)" (is (not (:success @(p/start! proc))))) + (testing "problem diagnosis" + (is ((:problems (api/get-process! (:id proc))) + :ffmpeg-not-found))) + (testing "spilling junk into the monitor channel" (a/>!! (:monitor proc) "junk"))