component

Petrus Theron 2017-04-16T14:16:12.152295Z

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

2017-04-16T14:56:54.317342Z

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

Petrus Theron 2017-04-16T15:54:55.565263Z

@noisesmith, no:

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

Petrus Theron 2017-04-16T15:56:37.572513Z

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 {}))

2017-04-16T15:56:48.573379Z

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

2017-04-16T15:56:51.573553Z

yeah, I saw that

2017-04-16T15:58:00.578455Z

why is it calling stop on iself?

2017-04-16T15:58:19.579768Z

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

Petrus Theron 2017-04-16T15:58:54.582225Z

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

Petrus Theron 2017-04-16T15:59:27.584638Z

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

2017-04-16T15:59:56.586615Z

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

2017-04-16T16:00:18.588890Z

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

Petrus Theron 2017-04-16T16:00:51.591903Z

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 [<http://dat.sys.ws:33|dat.sys.ws:33>] - 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 [<http://dat.sys.ws:33|dat.sys.ws:33>] - WebSocket connection stopped
17-04-16 16:00:33 Petruss-MacBook-Pro.local INFO [<http://dat.sys.ws:24|dat.sys.ws:24>] - 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
=&gt; #&lt;SystemMap&gt;

Petrus Theron 2017-04-16T16:01:14.593727Z

- this is after changing start method to:

(start [component]
    (let [port        (-&gt; 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)))

2017-04-16T16:01:29.594943Z

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

Petrus Theron 2017-04-16T16:02:13.598259Z

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

2017-04-16T16:02:43.600481Z

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

2017-04-16T16:03:31.604094Z

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

Petrus Theron 2017-04-16T16:11:36.639694Z

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

2017-04-16T16:11:54.640987Z

oh, so there you go

2017-04-16T16:12:04.641664Z

it's a different component entirely being run

2017-04-16T16:14:15.650828Z

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