Fork me on GitHub
#leiningen
<
2018-07-16
>
bozhidar08:07:06

Hey everyone! Does anyone know how to keep a lein process running after it spawned some server? I’m trying to create a simple lein plugin to just run the new nREPL server, but I hit a surprising roadblock - the server starts and then lein just exits and kills it.

(defn nrepl
  "Start a headless nREPL server within your project's context."
  [project & args]
  (println args)
  (eval/eval-in-project
   project
   `(start-nrepl ~(convert-args args))))
(this just uses the start-nrepl wrapper from cider-nrepl and passes it a hash with the params it normally expects. I took a look at https://github.com/technomancy/leiningen/blob/master/src/leiningen/repl.clj and I don’t quite understand everything it does to spin the headless server, so I decided it might be simpler to ask here.

bozhidar09:07:33

Basically it seems to me that the magic happens here:

(defn server [project cfg headless?]
  (nrepl.ack/reset-ack-port!)
  (when-not (nrepl-dependency? project)
    (main/info "Warning: no nREPL dependency detected.")
    (main/info "Be sure to include org.clojure/tools.nrepl in :dependencies"
               "of your profile."))
  (let [prep-blocker @eval/prep-blocker
        ack-port (:port @ack-server)]
    (-> (bound-fn []
          (binding [eval/*pump-in* false]
            (let [[evals requires]
                  (server-forms project cfg ack-port headless?)]
              (eval/eval-in-project project
                                    `(do ~(ignore-sigint-form) ~evals)
                                    requires))))
        (Thread.) (.start))
    (when project @prep-blocker)
    (when headless? @(promise))
    (if-let [repl-port (nrepl.ack/wait-for-ack
                        (get-in project [:repl-options :timeout] 60000))]
      (do (main/info "nREPL server started on port"
                     repl-port "on host" (:host cfg)
                     (str "- nrepl://" (:host cfg) ":" repl-port))
          repl-port)
      (main/abort "REPL server launch timed out."))))
But I wondered if there isn’t some general purpose machinery in Lein for dealing with daemons.

hiredman21:07:32

(when headless? @(promise))