Try to continue after encountering an exception

This commit is contained in:
niten 2023-06-07 13:27:47 -07:00
parent f0d8f59cc1
commit 4e3fa0914a

View File

@ -6,7 +6,8 @@
[objectifier-client.core :as obj] [objectifier-client.core :as obj]
[clojure.core.async :as async :refer [<! >! go-loop <!! timeout]] [clojure.core.async :as async :refer [<! >! go-loop <!! timeout]]
[clojure.string :as str]) [clojure.string :as str])
(:import java.time.Instant)) (:import java.time.Instant
java.lang.Exception))
;; Let's see: ;; Let's see:
;; ;;
@ -19,6 +20,10 @@
;; ;;
;; - If anything is detected, send a notification to the callback. ;; - If anything is detected, send a notification to the callback.
(defn- exception? [obj]
(instance? Exception obj))
(defprotocol ISuanNiServer (defprotocol ISuanNiServer
(object-channel [_])) (object-channel [_]))
@ -39,13 +44,17 @@
[false e])))) [false e]))))
max-wait (* 5 60 1000)] ;; wait at most 5 minutes max-wait (* 5 60 1000)] ;; wait at most 5 minutes
(loop [[success? result] (wrap-attempt) (loop [[success? result] (wrap-attempt)
wait-ms 1000] wait-ms 1000
n 0]
(if success? (if success?
result result
(do (when verbose (do (when verbose
(println (format "attempt failed, sleeping %s ms" wait-ms))) (println (format "attempt failed, sleeping %s ms" wait-ms)))
(if (< n 10)
(do
(<!! (timeout wait-ms)) (<!! (timeout wait-ms))
(recur (wrap-attempt) (min (* wait-ms 1.25) max-wait))))))) (recur (wrap-attempt) (min (* wait-ms 1.25) max-wait) (+ n 1)))
(throw result)))))))
(defn start! (defn start!
[& {:keys [listen-host [& {:keys [listen-host
@ -72,6 +81,7 @@
(async/close! image-chan)) (async/close! image-chan))
(when (-> event :type (= :motion-detected)) (when (-> event :type (= :motion-detected))
(let [cam (syno/get-camera-by-location! syno-client (:location event))] (let [cam (syno/get-camera-by-location! syno-client (:location event))]
(try
(>! image-chan (>! image-chan
{ {
:location (syno/location cam) :location (syno/location cam)
@ -79,7 +89,9 @@
:snapshot (syno/take-snapshot! cam) :snapshot (syno/take-snapshot! cam)
:time (Instant/now) :time (Instant/now)
:camera cam :camera cam
})) })
(catch Exception e
(println (.toString e)))))
(recur (<! event-chan))))) (recur (<! event-chan)))))
(go-loop [image-data (<! image-chan)] (go-loop [image-data (<! image-chan)]
(if (nil? image-data) (if (nil? image-data)
@ -89,6 +101,9 @@
(let [{:keys [location camera-id snapshot time]} image-data (let [{:keys [location camera-id snapshot time]} image-data
summary (retry-attempt verbose summary (retry-attempt verbose
#(obj/get-summary! obj-client snapshot))] #(obj/get-summary! obj-client snapshot))]
(if (exception? summary)
(println (.toString summary))
(do
(when verbose (when verbose
(println (str "detected " (println (str "detected "
(count (:objects summary)) (count (:objects summary))
@ -108,13 +123,14 @@
:snapshot snapshot :snapshot snapshot
:objects (:objects summary) :objects (:objects summary)
:detection-url (:output summary) :detection-url (:output summary)
})) }))))
(recur (<! image-chan))))) (recur (<! image-chan)))))
(go-loop [detection-event (<! obj-chan)] (go-loop [detection-event (<! obj-chan)]
(if (nil? detection-event) (if (nil? detection-event)
(when verbose (when verbose
(println "stopping object listener") (println "stopping object listener")
(async/close! mqtt-chan)) (async/close! mqtt-chan))
(try
(do (>! mqtt-chan (do (>! mqtt-chan
{:type :detection-event {:type :detection-event
:time (Instant/now) :time (Instant/now)
@ -125,5 +141,7 @@
:detect-time :detect-time
:objects :objects
:detection-url])}) :detection-url])})
(recur (<! obj-chan))))) (recur (<! obj-chan)))
(catch Exception e
(println (.toString e))))))
(->SuanNiServer event-chan image-chan obj-chan listener))) (->SuanNiServer event-chan image-chan obj-chan listener)))