-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
postal.clj
133 lines (109 loc) · 4.82 KB
/
postal.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
(ns taoensso.telemere.postal
"Email handler using `postal`,
Ref. <https://github.com/drewr/postal>."
(:require
[taoensso.encore :as enc :refer [have have?]]
[taoensso.telemere.utils :as utils]
[postal.core :as postal]))
(comment
(require '[taoensso.telemere :as tel])
(remove-ns 'taoensso.telemere.postal)
(:api (enc/interns-overview)))
(defn signal-subject-fn
"Experimental, subject to change.
Returns a (fn format [signal]) that:
- Takes a Telemere signal (map).
- Returns an email subject string like:
\"INFO EVENT :taoensso.telemere.postal/ev-id1 - msg\""
([] (signal-subject-fn nil))
([{:keys [max-len subject-signal-key]
:or
{max-len 128
subject-signal-key :postal/subject}}]
(fn signal-subject [signal]
(or
(get signal subject-signal-key) ; Custom subject
;; Simplified `utils/signal-preamble-fn`
(let [{:keys [level kind #_ns id msg_]} signal
sb (enc/str-builder)
s+spc (enc/sb-appender sb " ")]
(when level (s+spc (utils/format-level level)))
(when kind (s+spc (utils/upper-qn kind)))
(when id (s+spc (utils/format-id nil id)))
(when-let [msg (force msg_)] (s+spc "- " msg))
(enc/get-substr-by-len (str sb) 0 max-len))))))
(comment ((signal-subject-fn) (tel/with-signal (tel/event! ::ev-id1 #_{:postal/subject "My subject"}))))
(def default-dispatch-opts
{:min-level :info
:rate-limit
[[5 (enc/msecs :mins 1)]
[10 (enc/msecs :mins 15)]
[15 (enc/msecs :hours 1)]
[30 (enc/msecs :hours 6)]
]})
(defn handler:postal
"Experimental, subject to change.
Needs `postal`, Ref. <https://github.com/drewr/postal>.
Returns a (fn handler [signal]) that:
- Takes a Telemere signal (map).
- Sends the signal as an email to specified recipient.
Useful for emailing important alerts to admins, etc.
Default handler dispatch options (override when calling `add-handler!`):
`:min-level` - `:info`
`:rate-limit` -
[[5 (enc/msecs :mins 1)] ; Max 5 emails in 1 min
[10 (enc/msecs :mins 15)] ; Max 10 emails in 15 mins
[15 (enc/msecs :hours 1)] ; Max 15 emails in 1 hour
[30 (enc/msecs :hours 6)] ; Max 30 emails in 6 hours
]
Options:
`:conn-opts` - Map of connection opts given to `postal/send-message`
Examples:
{:host \"mail.isp.net\", :user \"jsmith\", :pass \"a-secret\"},
{:host \"smtp.gmail.com\", :user \"jsmith@gmail.com\", :pass \"a-secret\" :port 587 :tls true},
{:host \"email-smtp.us-east-1.amazonaws.com\", :port 587, :tls true,
:user \"AKIAIDTP........\", :pass \"AikCFhx1P.......\"}
`:msg-opts` - Map of message opts given to `postal/send-message`
Examples:
{:from \"foo@example.com\", :to \"bar@example.com\"},
{:from \"Alice <foo@example.com\", :to \"Bob <bar@example.com>\"},
{:from \"no-reply@example.com\", :to [\"first-responders@example.com\",
\"devops@example.com\"],
:cc \"engineering@example.com\"
:X-MyHeader \"A custom header\"}
`:subject-fn` - (fn [signal]) => email subject string
`:body-fn` - (fn [signal]) => email body content string,
see `format-signal-fn` or `pr-signal-fn`
Tips:
- Ref. <https://github.com/drewr/postal> for more info on `postal` options.
- Sending emails can be slow, and can incur financial costs!
Use appropriate handler dispatch options for async handling and rate limiting, etc."
;; ([] (handler:postal nil))
([{:keys [conn-opts msg-opts, subject-fn body-fn]
:or
{subject-fn (signal-subject-fn)
body-fn (utils/format-signal-fn)}}]
(when-not (map? conn-opts) (throw (ex-info "Expected `:conn-opts` map" (enc/typed-val conn-opts))))
(when-not (map? msg-opts) (throw (ex-info "Expected `:msg-opts` map" (enc/typed-val msg-opts))))
(let [handler-fn
(fn a-handler:postal
([]) ; Shut down (no-op)
([signal]
(enc/when-let [subject (subject-fn signal)
body (body-fn signal)]
(let [msg
(assoc msg-opts
:subject (str subject)
:body
(if (string? body)
[{:type "text/plain; charset=utf-8"
:content (str body)}]
body))
[result ex]
(try
[(postal/send-message conn-opts msg) nil]
(catch Exception ex [nil ex]))
success? (= (get result :code) 0)]
(when-not success?
(throw (ex-info "Failed to send email" result ex)))))))]
(with-meta handler-fn default-dispatch-opts))))