/
bin.clj
120 lines (109 loc) · 3.77 KB
/
bin.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
(ns riemann.bin
"Main function."
(:require [riemann.config :as config]
[riemann.logging :as logging]
riemann.time
[riemann.test :as test]
riemann.pubsub
[cemerick.pomegranate :as pom]
[clojure.java.io :as io]
[clojure.tools.logging :refer :all])
(:gen-class :name riemann.bin))
(def config-file
"The configuration file loaded by the bin tool"
(promise))
(defn set-config-file!
"Sets the config file used by Riemann. Adds the config file's enclosing
directory to the classpath as well."
[file]
(info "Loading" (-> file io/file .getCanonicalPath))
(assert (deliver config-file file)
(str "Config file already set to " (pr-str @config-file)
"--can't change it to " (pr-str file)))
(let [dir (-> file
io/file
.getCanonicalPath
io/file
.getParent)]
(pom/add-classpath dir)))
(def reload-lock (Object.))
(defn reload!
"Reloads the given configuration file by clearing the task scheduler, shutting
down the current core, and loading a new one."
[]
(locking reload-lock
(try
(riemann.config/validate-config @config-file)
(riemann.time/reset-tasks!)
(riemann.config/clear!)
(riemann.pubsub/sweep! (:pubsub @riemann.config/core))
(riemann.config/include @config-file)
(riemann.config/apply!)
:reloaded
(catch Exception e
(error e "Couldn't reload:")
e))))
(defn handle-signals
"Sets up POSIX signal handlers."
[]
(if (not (.contains (. System getProperty "os.name") "Windows"))
(sun.misc.Signal/handle
(sun.misc.Signal. "HUP")
(proxy [sun.misc.SignalHandler] []
(handle [sig]
(info "Caught SIGHUP, reloading")
(reload!))))))
(defn pom-version
"Return version from Maven POM file."
[]
(let [pom "META-INF/maven/riemann/riemann/pom.properties"
props (doto (java.util.Properties.)
(.load (-> pom io/resource io/input-stream)))]
(.getProperty props "version")))
(defn version
"Return version from Leiningen environment or embeddd POM properties."
[]
(or (System/getProperty "riemann.version")
(pom-version)))
(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 -main
"Start Riemann. Loads a configuration file from the first of its args."
([]
(-main "riemann.config"))
([config]
(-main "start" config))
([command config & [test-name]]
(logging/init)
(case command
"start" (try
(info "PID" (pid))
(set-config-file! config)
(handle-signals)
(riemann.time/start!)
(riemann.config/include @config-file)
(riemann.config/apply!)
nil
(catch Exception e
(error e "Couldn't start")))
"test" (try
(test/with-test-env
(set-config-file! config)
(riemann.config/include @config-file)
(binding [test/*streams* (:streams @config/next-core)]
(let [test-name-pattern (if test-name (re-pattern test-name) #".*-test")
results (clojure.test/run-all-tests test-name-pattern)]
(if (and (zero? (:error results))
(zero? (:fail results)))
(System/exit 0)
(System/exit 1))))))
"version" (try
(println (version))
(catch Exception e
(error e "Couldn't read version"))))))