Skip to content

Instantly share code, notes, and snippets.

@hiredman
Created September 8, 2014 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiredman/d75e906212a68c657bbf to your computer and use it in GitHub Desktop.
Save hiredman/d75e906212a68c657bbf to your computer and use it in GitHub Desktop.
(ns com.manigfeald.sofp.repl
(:require [com.stuartsierra.component :as component]
[clojure.tools.logging :as log]
[clojure.tools.nrepl.server :refer [start-server stop-server]]))
(defrecord Repl [signals exec]
component/Lifecycle
(start [this]
(log/trace "starting repl")
(let [run? (atom true)
queue (java.util.concurrent.LinkedTransferQueue.)]
(signals "TRAP" (fn [] (future (.offer queue :token))))
(assoc this
:run? run?
:worker (exec (fn []
(try
(while @run?
(.take queue)
(log/info "starting nrepl on 7888")
(let [s (start-server
:host "127.0.0.1"
:port 7888)]
(.take queue)
(log/info "stopping nrepl on 7888")
(stop-server s)))
(catch Exception e
(log/error e "whoops")
(throw e))))))))
(stop [this]
(reset! (:run? this) false)
(future-cancel (:worker this))
(deref (:worker this))
this))
(ns com.manigfeald.sofp.signals
(:require [com.stuartsierra.component :as component]
[clojure.tools.logging :as log]))
(defrecord SignalHandler []
component/Lifecycle
(start [this]
(log/info "start signal handler")
this)
(stop [this]
this)
clojure.lang.IFn
(invoke [_ signal-name handler]
(sun.misc.Signal/handle
(sun.misc.Signal. signal-name)
(reify
sun.misc.SignalHandler
(handle [_ _]
(handler))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment