In both of your solutions you are stopping main thread from exiting, you
could j.c.u CountDountLatch that could have same number of lines as the
second solution but has clearer intent.

BTW, aleph.http/start-server returns closable object if you are planning to
do something fancy.

Kurman

On Wed, Jun 8, 2016 at 7:53 AM, Nathaniel Young <youn...@gmail.com> wrote:

> Good morning (at least from my part of the globe),
>
> I spent a good chunk of time last night wondering why this chunk of code
> kept exiting about a minute after being run:
>
> ----------------------------------------
> (ns narcoleptic.server
>   (:require [aleph.http :as http]
>             [compojure.core :as compojure :refer [GET]]
>             [compojure.route :as route]
>             [ring.middleware.params :as params]))
>
> (def handler
>   (params/wrap-params
>    (compojure/routes
>     (GET "/" [] "Hello World!")
>     (route/not-found "Not Found"))))
>
> (def server (agent nil))
>
> (defn -main []
>   (send-off server #(or % (http/start-server handler {:port 9000}))))
> ----------------------------------------
>
> until I found
> http://tech.puredanger.com/2010/06/08/clojure-agent-thread-pools/ which
> explains that send-off pools die after a minute. Additionally, it looks
> like all of the thread pools that aleph creates are marked as daemon
> threads, so the main thread dies after the send-off, the send-off thread
> dies after a minute and that leaves no non-daemon threads running, so the
> JVM exits. Cool.
>
> My question, then, is this: Is there a best practice for keeping a thread
> open forever so that my server won't ever exit? I'd like to `lein run -m
> narcoleptic.server` and see that it stays up as long as possible.
>
> I've tried two solutions, but the first feels ike an awful lot of typing
> for a language that usually has the right tools close at hand and the
> second feels like a hack and doesn't play as nicely in a REPL environment
> (but they both work):
>
> (1)
> ----------------------------------------
> (defn wait-until-stopped []
>   (let [stopped (promise)]
>     (add-watch server :stop-wait
>       (fn [key server old-state new-state]
>         (when (= new-state :stopped)
>           (remove-watch server :stop-wait)
>           (deliver stopped :ok))))
>     @stopped))
> ----------------------------------------
>
> (2)
> ----------------------------------------
> ;; removed the agents code
> (def stopped (promise))
> (defn -main []
>   (http/start-server handler {:port 9000})
>   @stopped)
> ----------------------------------------
>
> Am I super off in the woods here? Any help/pointers/sage advice
> appreciated. Thanks!
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> --- You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to