~pepe/neil

dcce7992ed20a474633a05cf40f8de0fa422c4bc — Josef Pospíšil a month ago 1a16e62
Code relax
4 files changed, 35 insertions(+), 54 deletions(-)

M neil/sock/acts.janet
M neil/sock/init.janet
M neil/watch/init.janet
M neil/watch/tasks.janet
M neil/sock/acts.janet => neil/sock/acts.janet +2 -4
@@ 294,8 294,7 @@
  (make-watch
    (fn [_ {:brush brush} _]
      (def running (running-task brush))
      (def {"query" query
            "action" action} msg)
      (def {"query" query "action" action} msg)
      (if (empty? action)
        (results sck query)
        (perform sck action)))


@@ 304,8 303,7 @@
(define-watch Open [_ {:trevor t :env env :brush b} _]
  (give (start t env b add-one remove-one actions)))

(define-watch Init
  [_ {:hemple h :brush b :env env} _]
(define-watch Init [_ {:hemple h :brush b :env env} _]
  (cond-> @[Open (log (. "Trevor Present on " (h env)))]
          (:soak b :task/running)
          (array/push RunningTime)))

M neil/sock/init.janet => neil/sock/init.janet +10 -13
@@ 44,32 44,29 @@
    :read
    (fn [self _ msg]
      (cocoon/emerge ((cfg :actions) self (json/decode msg))))
    :closed (fn [self] (cocoon/emerge ((cfg :remove) self)))})
    :closed (fn [self]
              (cocoon/emerge ((cfg :remove) self)))})

(defn supervisor
  [chan handling]
  (forever
    (match (ev/take chan)
      [:conn connection]
      (ev/go
        (fiber-fn :tp
                  (setdyn :conn connection)
                  (handling connection))
        nil chan)
      [:emergence acts] (cocoon/emerge ;acts)
      [:close connection]
      (do
        (cocoon/emerge (log "sock close " ((string/split " " (describe connection)) 1)))
        (:close connection))
      [:error fiber]
      (let [err (fiber/last-value fiber)
            conn ((fiber/getenv fiber) :conn)]
        (unless (one-of err
                        "Connection reset by peer"
                        "stream is closed")
          (cocoon/emerge (log "Sock Supervisor: " err) (stacktrace fiber))
          (protect (:write conn (trevor/text (json/encode {:error err}))))
          (ev/give-supervisor :close conn)))
      [:conn connection]
      (ev/go
        (fiber-fn :tp
                  (cocoon/emerge (log "socks conn " ((string/split " " (describe connection)) 1)))
                  (setdyn :conn connection)
                  (handling connection)) nil chan))))
          (ev/give chan [:close conn])))
      [:close connection] (:close connection))))

(defn start [t env brush add remove actions]
  (chidi/start

M neil/watch/init.janet => neil/watch/init.janet +20 -34
@@ 1,7 1,4 @@
(import chidi)
(use chidi/middleware chidi/response)
(use eleanor shawn/acts shawn/act)
(import shawn/cocoon)
(use chidi eleanor shawn/acts shawn/act shawn/cocoon)

(use /neil/watch/utils /neil/watch/middleware)



@@ 10,9 7,7 @@
(import /neil/watch/tasks)
(import /neil/watch/work-intervals)
(import /neil/watch/stats)
(import /neil/watch/auth)
(import /neil/acts/shared)
(import /neil/sock/acts :as sock)
(import /neil/watch/auth) # must be here, cause middleware uses it

(defn dashboard [brush trevor]
  (fn [_]


@@ 47,13 42,13 @@

(defn open [chidi trevor key brush]
  (drive
    {"/" (-> brush (dashboard trevor) (html-get brush))
     "/auth" @{"" (dispatch
    {"/auth" @{"" (dispatch
                    @{"GET" (-> auth/form html-success)
                      "POST" (-> (auth/set chidi (:soak brush :password) key)
                                 urlencoded)})
               "/change" (-> brush (auth/change key) dry (urlenc-post brush))
               "/expire" (-> brush auth/expire dry (urlenc-post brush))}
     "/" (-> brush (dashboard trevor) (html-get brush))
     "/admin" (-> admin (set-admin brush trevor) (html-get brush))
     "/admin/:tab" (-> admin (set-admin brush trevor) (html-get brush))
     "/stats" (stats/routes brush trevor)


@@ 70,41 65,32 @@
    (match (ev/take chan)
      [:conn connection]
      (ev/go
        (fiber/new
          (fn handling-connection [conn]
            (cocoon/emerge (log "watch conn " ((string/split " " (describe conn)) 1)))
            (setdyn :conn conn)
            (handling conn)) :tp)
        connection chan)
      [:emergence act] (cocoon/emerge ;act)
        (fiber-fn :tp
                  (setdyn :conn connection)
                  (handling connection))
        nil chan)
      [:emergence acts] (emerge ;acts)
      [:error fiber]
      (do
        (def err (fiber/last-value fiber))
        (def conn ((fiber/getenv fiber) :conn))
      (let [err (fiber/last-value fiber)
            conn ((fiber/getenv fiber) :conn)]
        (unless (one-of err
                        "Connection reset by peer"
                        "stream is closed")
          (cocoon/emerge (log "Watch Supervisor: " err) (stacktrace fiber))
          (emerge (log "Watch Supervisor: " err) (stacktrace fiber))
          (protect (:write conn
                           (chidi/internal-server-error
                           (internal-server-error
                             (. "Internal Server Error: " err)))))
        (cocoon/emerge (log "watch close " ((string/split " " (describe conn)) 1)))
        (ev/give-supervisor :close conn))
      [:close conn]
      (do
        (cocoon/emerge (log "watch close " ((string/split " " (describe conn)) 1)))
        (:close conn)))))
        (emerge (log "watch close " ((string/split " " (describe conn)) 1)))
        (ev/give [:close conn]))
      [:close conn] (:close conn))))

(defn start [c t env k b]
  (chidi/start
(defn start-chidi [c t env k b]
  (start
    (open (first (string/split ":" (get-in c [env :url])))
          (get-in t [env :url]) k b) ;(string/split ":" (get-in c [env :run])) supervisor))

(define-watch Open [_ e _]
  (def {:chidi c :trevor t :env env
        :key k :brush b} e)
  (cocoon/give
    (start c t env k b)))
(define-watch Open [_ {:chidi c :trevor t :env env :key k :brush b} _]
  (give (start-chidi c t env k b)))

(define-watch Init [_ {:chidi h :env env} _]
  [Open (log (. "Chidi Present on " (get-in h [env :run])))])

M neil/watch/tasks.janet => neil/watch/tasks.janet +3 -3
@@ 15,18 15,18 @@
(defn start [brush]
  (fn [{:params {:id i}
        :headers {"HX-Request" hx}}]
    (cocoon/emerge sock/Started (shared/start-task i))
    (cocoon/emerge (shared/start-task i) sock/Started)
    (if hx (no-content) (see-other "/"))))

(defn stop
  [{:body {"note" n}
    :headers {"HX-Request" hx}}]
  (cocoon/emerge sock/Stopped (shared/stop-task n))
  (cocoon/emerge (shared/stop-task n) sock/Stopped)
  (if hx (no-content) (see-other "/")))

(defn done [{:body {"note" n}
             :headers {"HX-Request" hx}}]
  (cocoon/emerge sock/Stopped (shared/stop-task n "completed"))
  (cocoon/emerge (shared/stop-task n "completed") sock/Stopped)
  (if hx (no-content) (see-other "/")))

(defn complete [brush]