-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticmq.clj
executable file
·97 lines (78 loc) · 2.96 KB
/
elasticmq.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
(ns elasticmq-clj.elasticmq
(:require [clojure.java.io :as io]
[integrant.core :as ig]
[clojure.tools.logging :as log])
(:import [java.io File]
[java.nio.file Files Paths LinkOption Path]
[java.nio.file.attribute FileAttribute]))
(def ^:private download-url "https://s3-eu-west-1.amazonaws.com/softwaremill-public/elasticmq-server-1.3.9.jar")
(def ^:private elasticmq-directory (str (System/getProperty "user.home") File/separator ".clj-elasticmq-local"))
(def ^:private host {:name (System/getProperty "os.name")
:version (System/getProperty "os.version")
:arch (System/getProperty "os.arch")})
(defn- ->path
"Create a path from the given strings."
[str & strs]
{:pre [(string? str) (every? string? strs)]}
(Paths/get str (into-array String strs)))
(defn- path?
"Is the given argument a path?"
[x]
(instance? Path x))
(defn- exists?
"Does the given path exist?"
[path]
{:pre [(path? path)]}
(Files/exists path (into-array LinkOption [])))
(defn- ensure-elasticmq-directory
"Make sure the directory that elasticmq Local will be downloaded to
exists."
[]
(let [path (->path elasticmq-directory)]
(when-not (exists? path)
(-> (Files/createDirectory path (make-array FileAttribute 0))
(.toString)))))
(defn- build-elasticmq-command
"Build a java command to start elasticmq Local with the required
options."
[conf-path]
(let [conf-path (when conf-path (str "-Dconfig.file=" conf-path))
jar-path (str (io/file elasticmq-directory "elasticmq.jar"))]
(if conf-path
(format "java %s -jar %s" conf-path jar-path)
(format "java -jar %s" jar-path))))
(defn start-elasticmq
"Start elasticmq Local with the desired options."
[config]
(let [elasticmq (->> (build-elasticmq-command config)
(.exec (Runtime/getRuntime)))]
(log/info "Started elasticmq Local")
elasticmq))
(defn- download-elasticmq
"Download elasticmq."
[url]
(log/info "Downloading elasticmq Local" {:elasticmq-directory elasticmq-directory})
(ensure-elasticmq-directory)
(io/copy (io/input-stream (io/as-url url)) (io/as-file (str elasticmq-directory "/" "elasticmq.jar"))))
(defn ensure-installed
"Download and elasticmq Local if it hasn't been already."
[]
(when-not (exists? (->path elasticmq-directory "elasticmq.jar"))
(download-elasticmq download-url)))
(defn handle-shutdown
"Kill the elasticmq Local process on JVM shutdown."
[elasticmq-process]
(doto elasticmq-process (.destroy) (.waitFor))
(log/info (str "Exited" {:exit-value (.exitValue elasticmq-process)})))
(defn create-elasticmq-db-logger
[log]
(fn [& message]
(apply log "elasticmq-local:" message)))
(defn halt! [elasticmq]
(when elasticmq
(handle-shutdown elasticmq)))
(defmethod ig/init-key ::elasticmq [_ config]
(ensure-installed)
(start-elasticmq config))
(defmethod ig/halt-key! ::elasticmq [_ elasticmq]
(halt! elasticmq))