-
Notifications
You must be signed in to change notification settings - Fork 47
/
publisher.clj
238 lines (166 loc) · 6.51 KB
/
publisher.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
(ns com.brunobonacci.mulog.publisher
(:require [com.brunobonacci.mulog.agents :as ag]
[com.brunobonacci.mulog.buffer :as rb]
[clojure.java.io :as io]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; ----==| P U B L I S H E R P R O T O C O L |==---- ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defprotocol PPublisher
"Publisher protocol"
(agent-buffer [this]
"Returns the agent-buffer where items are sent to, basically your
inbox.")
(publish-delay [this]
"The number of milliseconds between two calls to `publish` function.
return `nil` if you don't want mu/log call the `publish` function")
(publish [this buffer]
"publishes the items in the buffer and returns the new state of
the buffer which presumably doesn't contains the messages
successfully sent.")
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; ----==| C O N S O L E |==---- ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deftype ConsolePublisher
[config buffer]
com.brunobonacci.mulog.publisher.PPublisher
(agent-buffer [_]
buffer)
(publish-delay [_]
200)
(publish [_ buffer]
;; items are pairs [offset <item>]
(doseq [item (map second (rb/items buffer))]
(printf "%s\n" (pr-str item)))
(flush)
(rb/clear buffer)))
(defn console-publisher
[config]
(ConsolePublisher. config (ag/buffer-agent 10000)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; ----==| S I M P L E - F I L E |==---- ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deftype SimpleFilePublisher
[config ^java.io.Writer filewriter buffer]
com.brunobonacci.mulog.publisher.PPublisher
(agent-buffer [_]
buffer)
(publish-delay [_]
500)
(publish [_ buffer]
;; items are pairs [offset <item>]
(doseq [item (map second (rb/items buffer))]
(.write filewriter (prn-str item)))
(.flush filewriter)
(rb/clear buffer)))
(defn simple-file-publisher
[{:keys [filename] :as config}]
{:pre [filename]}
(let [filename (io/file filename)]
;; make parte dirs
(.mkdirs (.getParentFile filename))
(SimpleFilePublisher.
config
(io/writer filename :append true)
(ag/buffer-agent 10000))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; ----==| F A C T O R Y |==---- ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- load-function-from-name
([fqn-fname]
(if (string? fqn-fname)
(let [[_ fns ff] (re-find #"([^/]+)/([^/]+)" fqn-fname)]
(when (not (and fns ff))
(throw
(ex-info
(str "function '" fqn-fname
"' is invalid format. must be \"namespace/fun-name\".")
{:fqn-fname fqn-fname})))
(load-function-from-name fns ff))
fqn-fname))
([fn-ns fn-name]
(when (not (and fn-ns fn-name))
(throw (ex-info (str "function '" fn-ns "/" fn-name "' not found.")
{:fn-ns fn-ns :fn-name fn-name})))
;; requiring the namespace
(require (symbol fn-ns))
(let [fn-symbol (resolve (symbol fn-ns fn-name))]
(when-not fn-symbol
(throw (ex-info (str "function '" fn-ns "/" fn-name "' not found.")
{:fn-ns fn-ns :fn-name fn-name})))
fn-symbol)))
(defn- loading-error [stage info cause]
(case stage
:loading
(throw
(ex-info
(str "Unable to load appropriate publisher."
" Please ensure you have the following dependency "
"[com.brunobonacci/mulog-"
(some-> info :config :type name) " \"x.y.z\"]"
" in your project.clj")
info cause))
:init
(throw
(ex-info
(str "Unable to initialize publisher."
" Please ensure you have the publisher has a function with 1 argument.")
info cause))
:verify
(throw
(ex-info "Invalid publisher, not an instance of com.brunobonacci.mulog.publisher.PPublisher"
info))))
(defn- load-dynamic-publisher
[publisher-name config]
(let [;; load publisher factory function
publisher* (try
(load-function-from-name publisher-name)
(catch Exception x
(loading-error :loading {:config config} x)))
;; initialize publisher
publisher (try (publisher* config)
(catch Exception x
(loading-error :init {:config config} x)))]
;; verify type
(when-not (instance? com.brunobonacci.mulog.publisher.PPublisher publisher)
(loading-error :verify {:config config
:name publisher-name
:type (type publisher)} nil))
;; if all ok return the publisher
publisher))
(defmulti publisher-factory
""
(fn [cfg] (:type cfg)))
(defmethod publisher-factory :default
[cfg]
(throw
(ex-info "mu/log Invalid or no reporting method selected."
{:type (:type cfg)
:config cfg})))
(defmethod publisher-factory :custom
[{:keys [fqn-function] :as cfg}]
(load-dynamic-publisher fqn-function cfg))
(defmethod publisher-factory :console
[config]
(console-publisher config))
(defmethod publisher-factory :simple-file
[config]
(simple-file-publisher config))
(defmethod publisher-factory :elasticsearch
[config]
(load-dynamic-publisher
"com.brunobonacci.mulog.publishers.elasticsearch/elasticsearch-publisher"
config))
(defmethod publisher-factory :kafka
[config]
(load-dynamic-publisher
"com.brunobonacci.mulog.publishers.kafka/kafka-publisher"
config))