Added error handling to agents.

This commit is contained in:
Alexander K. Hudek 2014-10-07 01:29:03 -04:00
parent 2f34c6c39a
commit 8a0063040f
3 changed files with 14 additions and 3 deletions

View file

@ -1,10 +1,10 @@
(defproject net.thegeez/clj-browserchannel-jetty-adapter "0.0.3" (defproject net.thegeez/clj-browserchannel-jetty-adapter "0.0.4"
:description "Jetty async adapter for BrowserChannel" :description "Jetty async adapter for BrowserChannel"
:url "" :url ""
:dependencies [[ring/ring-core "1.3.1"] :dependencies [[ring/ring-core "1.3.1"]
[ring/ring-servlet "1.3.1"] [ring/ring-servlet "1.3.1"]
[org.eclipse.jetty/jetty-server "8.1.16.v20140903"];; includes ssl [org.eclipse.jetty/jetty-server "8.1.16.v20140903"];; includes ssl
[net.thegeez/clj-browserchannel-server "0.0.7"]] [net.thegeez/clj-browserchannel-server "0.0.8"]]
:profiles {:provided :profiles {:provided
{:dependencies {:dependencies
[[org.clojure/clojure "1.6.0"]]}}) [[org.clojure/clojure "1.6.0"]]}})

View file

@ -1,4 +1,4 @@
(defproject net.thegeez/clj-browserchannel-server "0.0.7" (defproject net.thegeez/clj-browserchannel-server "0.0.8"
:description "BrowserChannel server implementation in Clojure" :description "BrowserChannel server implementation in Clojure"
:dependencies [[ring/ring-core "1.3.1"] :dependencies [[ring/ring-core "1.3.1"]
[org.clojure/data.json "0.2.5"]] [org.clojure/data.json "0.2.5"]]

View file

@ -128,6 +128,14 @@
(defn error-response [status-code message] (defn error-response [status-code message]
{:status status-code {:status status-code
:body (str "<html><body><h1>" message "</h1></body></html>")}) :body (str "<html><body><h1>" message "</h1></body></html>")})
(defn agent-error-handler-fn
"Prints the error and tries to restart the agent."
[id]
(fn [ag ^Exception e]
(println "ERROR:" id "agent threw" e (.getMessage e))
(restart-agent ag @ag)))
;;;;;; end of utils ;;;;;; end of utils
;;;; listeners ;;;; listeners
@ -136,6 +144,8 @@
;; sessionId -> :event -> [call back] ;; sessionId -> :event -> [call back]
;; event: :map | :close ;; event: :map | :close
(def listeners-agent (agent {})) (def listeners-agent (agent {}))
(set-error-handler! listeners-agent (agent-error-handler-fn "listener"))
(defn add-listener [session-id event-key f] (defn add-listener [session-id event-key f]
(send-off listeners-agent (send-off listeners-agent
@ -506,6 +516,7 @@
;; when the client never connects with a backchannel ;; when the client never connects with a backchannel
refresh-session-timeout) refresh-session-timeout)
session-agent (agent session)] session-agent (agent session)]
(set-error-handler! session-agent (agent-error-handler-fn (str "session-" (:id session))))
(swap! sessions assoc id session-agent) (swap! sessions assoc id session-agent)
(when-let [notify (:on-session options)] (when-let [notify (:on-session options)]
(notify id req)) (notify id req))