Fork me on GitHub
Petrus Theron14:04:12

Having a hard time with component. Ring handler keeps running after calling (component/stop ...). Working off repo


before you stop, does the :http-server key in the system have a :server-stop key under it?

Petrus Theron15:04:55

@noisesmith, no:

(keys (:http-server @#'system))
=> (:config :ring-handler :figwheel-system :datomic)

Petrus Theron15:04:37

Here is the HttpServer component:

(ns dat.sys.server
  (:require [taoensso.timbre :as log :include-macros true]
            [com.stuartsierra.component :as component]
            [org.httpkit.server :refer (run-server)]))

(defrecord HttpServer [config ring-handler server-stop]
  (start [component]
    (if server-stop
      (let [component (component/stop component)
            port (-> config :server :port)
            server-stop (run-server (:handler ring-handler) {:port port})]
        (log/info "HTTP server started on port: " port)
        (assoc component :server-stop server-stop))))
  (stop [component]
    (when server-stop (server-stop))
    (log/debug "HTTP server stopped")
    (assoc component :server-stop nil)))

(defn new-http-server []
  (map->HttpServer {}))


that's confusing, because it clearly attaches the :server-stop


yeah, I saw that


why is it calling stop on iself?


component (component/stop component) - that's totally weird to me

Petrus Theron15:04:54

Seems weird yeah. Maybe to ensure that no server is running before starting?

Petrus Theron15:04:27

I don't see the log for "HTTP server started on port: "


but that should be handled by the system - the component's job is to start when you call start, and shut down when you call stop, I don't see why you would stop yourself inside a start method


if the system calls start before stopping you properly, that's the system's job to fix that

Petrus Theron16:04:51

When I run (reset), I get this output:

17-04-16 16:00:31 Petruss-MacBook-Pro.local DEBUG [] - Stopping websocket router
17-04-16 16:00:32 Petruss-MacBook-Pro.local INFO [] - WebSocket connection stopped
:reloading (dat.sys.server dat.sys.system user)
17-04-16 16:00:32 Petruss-MacBook-Pro.local INFO [dat.sys.config:59] - Starting config component
17-04-16 16:00:33 Petruss-MacBook-Pro.local INFO [dat.sys.datomic:42] - Datomic Starting
17-04-16 16:00:33 Petruss-MacBook-Pro.local INFO [dat.sys.import:12] - Importering data
17-04-16 16:00:33 Petruss-MacBook-Pro.local INFO [] - WebSocket connection stopped
17-04-16 16:00:33 Petruss-MacBook-Pro.local INFO [] - WebSocket connection started
17-04-16 16:00:33 Petruss-MacBook-Pro.local INFO [] - Starting websocket router and transaction listener
17-04-16 16:00:33 Petruss-MacBook-Pro.local INFO [] - Figwheel server started on port: 2358
=> #<SystemMap>

Petrus Theron16:04:14

- this is after changing start method to:

(start [component]
    (let [port        (-> config :server :port)
          server-stop (run-server (:handler ring-handler) {:port port})]
      (log/info "HTTP server started on port: " port)
      (assoc component :server-stop server-stop)))


you might want to add a log showing the value of server-stop inside the http component

Petrus Theron16:04:13

Note not seeing anything about HttpServer starting, just figwheel server started on port: 2358. Could they be conflicting?


only if they want the same port... unless your system is set up so the http-server never actually gets started


I'm not familiar with component/using and how it differs from component/system-using which I am used to using

Petrus Theron16:04:36

In user ns, (defn init ...) sets :http-server to use FigwheelServer


oh, so there you go


it's a different component entirely being run


I wouldn't have thought to look for a user.clj in the project... anyway that's why the server isn't stopping, the figwheel component clearly isn't defined to shut down on its stop method