Skip to content

Instantly share code, notes, and snippets.

@alandipert
Created August 20, 2010 04:42
Show Gist options
  • Save alandipert/539621 to your computer and use it in GitHub Desktop.
Save alandipert/539621 to your computer and use it in GitHub Desktop.
(ns org.dipert.reconfig
"Reads a file of Clojure data into *config*
when a SIGHUP is recieved.
Useful for daemons that support the reloading of
configuration data while running."
(:use [clojure.contrib.io :only (reader)]
[clojure.contrib.logging :only (info warn)])
(:import (sun.misc Signal SignalHandler)
(java.io File PushbackReader)))
(def *config* (agent nil))
(defn load-config [f]
(send-off *config*
(constantly
(let [config-file (File. f)]
(if (.exists config-file)
(do (info (format "[reconfig] Reading config file '%s'" config-file))
(try
(read (PushbackReader. (reader config-file)))
(catch Exception e
(warn (format "[reconfig] Reading config file '%s' failed with %s" config-file e)))))
(do (warn (format "[reconfig] Config file '%s' doesn't exist"))))))))
(defn set-config [f]
(Signal/handle (Signal. "HUP")
(reify SignalHandler
(handle [_ _] (load-config f)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment