~pepe/pan.earth

059b87d11ac2c0a3bd4a8642ee43f80bbeecffe6 — Josef Pospíšil 2 years ago 02c4a51
Fix supervisor
2 files changed, 18 insertions(+), 17 deletions(-)

M panearth/acts.janet
M panearth/app.janet
M panearth/acts.janet => panearth/acts.janet +1 -1
@@ 238,7 238,7 @@
                       :render-all [RenderContent RenderPosts]
                       :list-content ListContent})
         host port
         app/good-supervisor)))
         app/supervisor)))
   :effect
   (fn [_ {:chidi {:host host :port port}} _]
     (print "Chidi Present on " host ":" port))})

M panearth/app.janet => panearth/app.janet +17 -16
@@ 3,27 3,28 @@

(use ./utils)

(defn good-supervisor
(defn supervisor
  [chan handling]
  (var last-connection nil)
  (forever
    (match (ev/take chan)
      [:emergence act] (cocoon/emerge act)
      [:close connection] (:close connection)
      [:error fiber]
      (do
        (def err (fiber/last-value fiber))
        (eprint err)
        (unless (or (= err "Connection reset by peer")
                    (= err "stream is closed"))
          (:write last-connection (internal-server-error))
          (ev/give chan [:conn last-connection])))
      [:conn connection]
      (do
        (set last-connection connection)
    (forever
      (match (ev/take chan)
        [:emergence act] (cocoon/emerge act)
        [:close connection] (:close connection)
        [:error fiber]
        (let [err (fiber/last-value fiber)]
          (unless (or (= err "Connection reset by peer")
                      (= err "stream is closed"))
            (debug/stacktrace fiber err)
            (def conn ((fiber/getenv fiber) :conn))
            (:write conn (internal-server-error err))
            (:close conn)))
        [:conn connection]
        (ev/go
          (fiber/new
            (fn [] (handling connection)) :tp) nil chan)))))
            (fn []
              (setdyn :conn connection)
              (handling connection)) :tp) nil chan)))))

(defn- html-success [next-middleware]
  (fn [req]