Fork me on GitHub
#component
<
2017-04-16
>
Petrus Theron14:04:12

Having a hard time with component. Ring handler keeps running after calling (component/stop ...). Working off https://github.com/metasoarous/datsys repo

noisesmith14:04:54

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]
  component/Lifecycle
  (start [component]
    (if server-stop
      component
      (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 {}))

noisesmith15:04:48

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

noisesmith15:04:51

yeah, I saw that

noisesmith15:04:00

why is it calling stop on iself?

noisesmith15:04:19

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: "

noisesmith15:04:56

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

noisesmith16:04:18

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:

(reset)
17-04-16 16:00:31 Petruss-MacBook-Pro.local DEBUG [dat.sys.app:98] - Stopping websocket router
17-04-16 16:00:32 Petruss-MacBook-Pro.local INFO [] - WebSocket connection stopped
:reloading (dat.sys.server dat.sys.system dat.sys.run 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 [dat.sys.app:88] - Starting websocket router and transaction listener
17-04-16 16:00:33 Petruss-MacBook-Pro.local INFO [dat.sys.dev.figwheel-server:21] - 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)))

noisesmith16:04:29

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?

noisesmith16:04:43

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

noisesmith16:04:31

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

noisesmith16:04:54

oh, so there you go

noisesmith16:04:04

it's a different component entirely being run

noisesmith16:04:15

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