Last active
February 14, 2018 05:46
-
-
Save zarkone/618f9d8d24ba3ebbc61b8b104a75b42a to your computer and use it in GitHub Desktop.
Sample config reload example for Clojure (from our inner project Reaper)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns reaper.config | |
(:import [sun.misc Signal SignalHandler]) | |
(:require [cheshire.core :as json] | |
[clojure.java.io :as io] | |
[camel-snake-kebab.core :as case])) | |
(def config-atom (atom {})) | |
(defn get-env [var-name] | |
(or (System/getenv var-name) | |
(throw (Exception. (str "Error loading config - " var-name " environment variable is not set. "))))) | |
(defn data-dir-path [name] | |
(str (get-env "REAPER_DATA_DIR") "/" name)) | |
(defn transform-fields [config] | |
(update-in config [:sources :arxiv :fetchers] (comp set #(map keyword %)))) | |
(defn read-config [] | |
(-> "config.json" | |
data-dir-path | |
io/file | |
slurp | |
(json/parse-string case/->kebab-case-keyword) | |
(transform-fields))) | |
(defn reload-config! [] | |
(reset! config-atom (read-config))) | |
(defn get-config [] | |
(when (empty? @config-atom) | |
(reload-config!)) | |
@config-atom) | |
;; Taken from | |
;; https://github.com/riemann/riemann/blob/d3c3660e4a692cb07ef6df022ead40bde0073a27/src/riemann/bin.clj#L51 | |
(defn handle-signals | |
"Sets up POSIX signal handlers." | |
[] | |
(Signal/handle | |
(Signal. "HUP") | |
(proxy [SignalHandler] [] | |
(handle [sig] | |
(println "Caught SIGHUP, reloading") | |
(reload-config!))))) | |
;; ................... then, somewhere in init (i.e. in core namespace) | |
(defn pid | |
"Process identifier, such as it is on the JVM. :-/" | |
[] | |
(let [name (-> (java.lang.management.ManagementFactory/getRuntimeMXBean) | |
(.getName))] | |
(try | |
(get (re-find #"^(\d+).*" name) 1) | |
(catch Exception e name)))) | |
(defn write-pid [] | |
(spit (config/data-dir-path ".pid") (pid))) | |
(defn -main [& args] | |
(config/handle-signals) | |
(write-pid) | |
(run-all-fetchers) | |
(reset! scheduler-atom | |
(schedulers/schedule-fetchers FetchJob))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment