-
Notifications
You must be signed in to change notification settings - Fork 64
/
init.clj
240 lines (199 loc) · 7.94 KB
/
init.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
(ns ziggurat.init
"Contains the entry point for your application."
(:require [clojure.tools.logging :as log]
[mount.core :as mount :refer [defstate]]
[schema.core :as s]
[ziggurat.config :refer [ziggurat-config] :as config]
[ziggurat.messaging.messaging :as messaging]
[ziggurat.messaging.consumer :as messaging-consumer]
[ziggurat.messaging.producer :as messaging-producer]
[ziggurat.metrics :as metrics]
[ziggurat.nrepl-server :as nrepl-server]
[ziggurat.producer :as producer :refer [kafka-producers]]
[ziggurat.sentry :refer [sentry-reporter]]
[ziggurat.server :as server]
[ziggurat.streams :as streams]
[ziggurat.tracer :as tracer]
[ziggurat.util.java-util :as util]
[ziggurat.kafka-consumer.executor-service :as executor-service]
[ziggurat.kafka-consumer.consumer-driver :as consumer-driver])
(:gen-class
:methods [^{:static true} [init [java.util.Map] void]]
:name tech.gojek.ziggurat.internal.Init))
(defn- event-routes [args]
(merge (:stream-routes args) (:batch-routes args)))
(defn- start*
([states]
(start* states nil))
([states args]
(-> (mount/only states)
(mount/with-args args)
(mount/start))))
(defn- start-messaging-connection [args]
(messaging/start-connection config/config (:stream-routes args)))
(defn- start-messaging-consumers [args]
(start-messaging-connection args)
(messaging-consumer/start-subscribers (get args :stream-routes) (get args :batch-routes) (ziggurat-config)))
(defn- start-messaging-producers [args]
(start-messaging-connection args)
(messaging-producer/make-queues (event-routes args)))
(defn start-kafka-producers []
(start* #{#'kafka-producers}))
(defn start-kafka-streams [args]
(start* #{#'streams/stream} args))
(defn start-stream [args]
(start-kafka-producers)
(start-messaging-producers args)
(start-kafka-streams args))
(defn start-management-apis [args]
(start-messaging-connection args)
(start* #{#'server/server} (dissoc args :actor-routes)))
(defn start-server [args]
(start-messaging-connection args)
(start* #{#'server/server} args))
(defn start-workers [args]
(start-kafka-producers)
(start-messaging-producers args)
(start-messaging-consumers args))
(defn- stop-messaging []
(messaging/stop-connection config/config (:stream-routes mount/args)))
(defn stop-kafka-producers []
(mount/stop #'kafka-producers))
(defn stop-kafka-streams []
(mount/stop #'streams/stream))
(defn stop-workers []
(stop-messaging)
(stop-kafka-producers))
(defn stop-server []
(mount/stop #'server/server)
(stop-messaging))
(defn stop-stream []
(stop-kafka-streams)
(stop-messaging)
(stop-kafka-producers))
(defn stop-management-apis []
(mount/stop #'server/server)
(stop-messaging))
(defn start-batch-consumer [args]
(-> (mount/only #{#'executor-service/thread-pool
#'consumer-driver/consumer-groups})
(mount/with-args (:batch-routes args))
(mount/start)))
(defn stop-batch-consumer []
(-> (mount/only #{#'executor-service/thread-pool
#'consumer-driver/consumer-groups})
(mount/stop)))
(def valid-modes-fns
{:api-server {:start-fn start-server :stop-fn stop-server}
:stream-worker {:start-fn start-stream :stop-fn stop-stream}
:worker {:start-fn start-workers :stop-fn stop-workers}
:batch-worker {:start-fn start-batch-consumer :stop-fn stop-batch-consumer}
:management-api {:start-fn start-management-apis :stop-fn stop-management-apis}})
(defn- valid-modes []
(keys valid-modes-fns))
(defn- all-modes-except-batch-consumer []
(remove #(= % :batch-worker) (valid-modes)))
(defn- execute-function
([modes fnk]
(execute-function modes fnk nil))
([modes fnk args]
(doseq [mode (-> modes
(or (valid-modes))
sort)]
(if (nil? args)
((fnk (get valid-modes-fns mode)))
((fnk (get valid-modes-fns mode)) args)))))
(defn start-common-states []
(start* #{#'config/config
#'metrics/statsd-reporter
#'sentry-reporter
#'nrepl-server/server
#'tracer/tracer}))
(defn stop-common-states []
(mount/stop #'config/config
#'metrics/statsd-reporter
#'nrepl-server/server
#'tracer/tracer)
(stop-messaging))
(defn start
"Starts up Ziggurat's config, reporters, actor fn, rabbitmq connection and then streams, server etc"
[actor-start-fn stream-routes batch-routes actor-routes modes]
(start-common-states)
(actor-start-fn)
(execute-function modes
:start-fn
{:actor-routes actor-routes
:stream-routes stream-routes
:batch-routes batch-routes}))
(defn stop
"Calls the Ziggurat's state stop fns and then actor-stop-fn."
[actor-stop-fn modes]
(actor-stop-fn)
(stop-common-states)
(execute-function modes :stop-fn))
(defn- add-shutdown-hook [actor-stop-fn modes]
(.addShutdownHook
(Runtime/getRuntime)
(Thread. ^Runnable #((stop actor-stop-fn modes)
(shutdown-agents))
"Shutdown-handler")))
(declare StreamRoute)
(s/defschema StreamRoute
(s/conditional
#(and (seq %)
(map? %))
{s/Keyword {:handler-fn (s/pred #(fn? %))
s/Keyword (s/pred #(fn? %))}}))
(s/defschema BatchRoute
(s/conditional
#(and (seq %)
(map? %))
{s/Keyword {:handler-fn (s/pred #(fn? %))}}))
(defn validate-routes [stream-routes batch-routes modes]
(when (contains? (set modes) :stream-worker)
(s/validate StreamRoute stream-routes))
(when (contains? (set modes) :batch-worker)
(s/validate BatchRoute batch-routes)))
(defn- derive-modes [stream-routes batch-routes actor-routes]
(let [base-modes [:management-api :worker]]
(if (and (nil? stream-routes) (nil? batch-routes))
(throw (IllegalArgumentException. "Either :stream-routes or :batch-routes should be present in init args")))
(cond-> base-modes
(some? stream-routes) (conj :stream-worker)
(some? batch-routes) (conj :batch-worker)
(some? actor-routes) (conj :api-server))))
(defn validate-modes [modes stream-routes batch-routes actor-routes]
(let [derived-modes (if-not (empty? modes)
modes
(derive-modes stream-routes batch-routes actor-routes))
invalid-modes (filter #(not (contains? (set (valid-modes)) %)) derived-modes)]
(if (pos? (count invalid-modes))
(throw (ex-info "Wrong modes argument passed" {:invalid-modes invalid-modes}))
derived-modes)))
(defn main
"The entry point for your application.
Accepts stream-routes as a nested map keyed by the topic entities.
Each topic entity is a map with a handler-fn described. For eg.,
{:default {:handler-fn (fn [message] :success)}}
:handler-fn must return :success, :retry or :skip
start-fn takes no parameters, and will be run on application startup.
stop-fn takes no parameters, and will be run on application shutdown."
([start-fn stop-fn stream-routes]
(main start-fn stop-fn stream-routes []))
([start-fn stop-fn stream-routes actor-routes]
(main {:start-fn start-fn :stop-fn stop-fn :stream-routes stream-routes :actor-routes actor-routes :modes (all-modes-except-batch-consumer)}))
([{:keys [start-fn stop-fn stream-routes batch-routes actor-routes modes]}]
(try
(let [derived-modes (validate-modes modes stream-routes batch-routes actor-routes)]
(validate-routes stream-routes batch-routes derived-modes)
(add-shutdown-hook stop-fn derived-modes)
(start start-fn stream-routes batch-routes actor-routes derived-modes))
(catch clojure.lang.ExceptionInfo e
(log/error e)
(System/exit 1))
(catch Exception e
(log/error e)
(stop stop-fn modes)
(System/exit 1)))))
(defn -init [args]
(main (util/java-map->clojure-map args)))