Fork me on GitHub
#yada
<
2016-08-28
>
Matt Butler15:08:57

Hi, does anyone have any advice for using yada with leiningen? Its my understanding that lein wants a function to run when you do “lein run” however when you put a yada listener function call inside the -main function it executes the function then terminates the thread (i assume). If i put the server declaration outside of the -main function (see snippet) it runs fine but only in the repl/when running tests as they leave the process for as long as required. I’m guessing i need some way of telling lein to just wait? I’m new to clojure so apologies if i’ve made some mistakes.

lmergen15:08:15

@mbutler i would suggest adding a dev/user.clj or something to put the -main function in for development

lmergen15:08:41

just add that as an extra source path for :dev

bhagany15:08:33

I solve this problem by adding @(promise) after the call to listener, at the point that I want to wait

bhagany15:08:31

this is, in a roundabout way, the same way the edge sample application does it

Matt Butler15:08:36

@lmergen thanks, sorry if i misunderstand but would that not still have the same problem of just exiting out after the main function has run.

Matt Butler15:08:00

@bhagany Thanks, that seemed to work 😄, atm i just have the def server at the bottom of my core file so i added @(promise) under it.

bhagany15:08:00

yes, that ought to do it - fwiw, I think your original idea to do this inside -main is good too

bhagany15:08:42

(it's basically what I do)

Matt Butler15:08:57

I think i might move it into a function call because atm its causing my tests not to run because its just waiting 😄

bhagany15:08:56

heh, yeah, I didn't think of that

Matt Butler16:08:34

well either way thank you very much, that had been weighing over me for a while 😄

bhagany16:08:57

sure, no problem. I had the same question only a few weeks ago, just paying it forward 🙂

lmergen16:08:38

ah sorry @mbutler, i probably read too quick. for what it’s worth, boot internally also uses the @(promise) technique, and I use that in production as well

Matt Butler16:08:54

Yeah I had considered using boot as that is what edge uses but happy to find a solution 🙂 thanks again

lmergen16:08:55

i do think it would make sense if yada would expose netty’s wait until complete function, but i guess that would only lock yada in even more in a specific vendor (aleph)

Matt Butler16:08:43

I had looked at aleph and was wondering if i needed to make a call to its server start function but that seemed wrong.

lmergen16:08:49

aleph uses netty, and netty exposes the function we need to use

lmergen16:08:38

however, in order to manually call that, you need the server value here: https://github.com/juxt/yada/blob/master/src/yada/aleph.clj#L12

lmergen16:08:42

which is not exposed by yada

lmergen16:08:44

so you cannot do it

lmergen16:08:46

i think the best way forward is to expose a :wait function in that listener, similar to :close:, that blocks until the server has exited gracefully

lmergen16:08:37

@malcolmsparks what are your thoughts on this ? this issue seems to pop up once in a while, and I think this should be handled by yada in some way

vinnyataide19:08:56

is it normal that when I start a yada server in aleph to take 1GB of my ram?

vinnyataide19:08:17

another thing... is this the correct way to manipulate a server instance in the repl?

(defrecord Server [port server handler]
  Lifecycle
  (start [component]
    (let [server (start-server
                  (make-handler handler)
                  {:port port :join? false})]
      (assoc component :server server)))
  (stop [component]
    (when-let [server (get component :server)]
      (.close server)
      component)))
It seems that I can't stop the server with (.stop server) when I do
(defn new-server
  ([port]
   (new-server port nil))
  ([port handler]
   (map->Server {:port port :handler handler})))

(def server (new-server 3000 (yada "Hello World")))
in the repl