|
9 | 9 | ;; and sends emails via SMTP. The cadence is the operator's |
10 | 10 | ;; responsibility: schedule bb notify from cron or a systemd timer. |
11 | 11 | ;; |
| 12 | +;; Operational state lives in public/.last-notify-failures.edn -- a |
| 13 | +;; per-(subscriber, source) timestamp of the last successful send, |
| 14 | +;; used to avoid re-mailing the same failures. Delete the file to |
| 15 | +;; replay all failures on the next run. |
| 16 | +;; |
12 | 17 | ;; Usage: |
13 | 18 | ;; bb notify -- send notifications |
14 | 19 | ;; bb notify --dry-run -- show what would be sent without sending |
15 | 20 | ;; bb notify --debug -- verbose diagnostics |
16 | 21 |
|
17 | 22 | (require '[babashka.pods :as pods] |
18 | 23 | '[clojure.string :as str] |
| 24 | + '[clojure.edn :as edn] |
| 25 | + '[clojure.java.io :as io] |
19 | 26 | '[taoensso.timbre :as log] |
20 | 27 | '[bark.common :refer [get-header format-date format-date-iso |
21 | 28 | report-priority report-status report-descendant-count |
|
30 | 37 |
|
31 | 38 | (require '[pod.tzzh.mail :as mail]) |
32 | 39 |
|
| 40 | +;; --------------------------------------------------------------------------- |
| 41 | +;; Per-(subscriber, source) failures-shown tracking |
| 42 | +;; --------------------------------------------------------------------------- |
| 43 | +;; |
| 44 | +;; We persist the timestamp of the last successful notification per |
| 45 | +;; (subscriber, source) pair to avoid re-sending the same failure on |
| 46 | +;; every run. This is operational state, not configuration -- subscriber |
| 47 | +;; identity and filters live in config.edn. |
| 48 | + |
| 49 | +(def ^:private last-failures-file "public/.last-notify-failures.edn") |
| 50 | + |
| 51 | +(defn- load-last-failures-shown |
| 52 | + "Read {key -> epoch-millis} from the state file, or {}. A corrupt |
| 53 | + file would re-spam every subscriber with old failures, so log at warn |
| 54 | + before returning the empty default." |
| 55 | + [] |
| 56 | + (let [f (io/file last-failures-file)] |
| 57 | + (if (.exists f) |
| 58 | + (try (edn/read-string (slurp f)) |
| 59 | + (catch Exception e |
| 60 | + (log/warn "Could not parse" last-failures-file |
| 61 | + "-- starting from empty state. Subscribers may" |
| 62 | + "see previously-shown failures on this run." |
| 63 | + (.getMessage e)) |
| 64 | + {})) |
| 65 | + {}))) |
| 66 | + |
| 67 | +(defn- save-last-failures-shown! |
| 68 | + [m] |
| 69 | + (io/make-parents last-failures-file) |
| 70 | + (spit last-failures-file (pr-str m))) |
| 71 | + |
| 72 | +(defn- failures-key |
| 73 | + "Key under which we track the last-shown timestamp for a subscription." |
| 74 | + [email source] |
| 75 | + (str source ":" (str/lower-case email))) |
| 76 | + |
33 | 77 | ;; --------------------------------------------------------------------------- |
34 | 78 | ;; Failure queries |
35 | 79 | ;; --------------------------------------------------------------------------- |
|
43 | 87 | (.getMessage e))))) |
44 | 88 |
|
45 | 89 | (defn- failures-for-subscriber |
46 | | - "Return failures on `source` routed to `email-addr`. |
| 90 | + "Return failures on `source` routed to `email-addr`, posted after |
| 91 | + `since-date` (or all of them if since-date is nil). |
47 | 92 |
|
48 | 93 | Routing is driven by the `:audience` field on each failure entry: |
49 | 94 | - `:author` -- shown only to the address that triggered the |
50 | 95 | failure (someone seeing their own typo); default |
51 | 96 | for legacy entries that predate the field. |
52 | 97 | - `:maintainers` -- shown to every subscriber on the source." |
53 | | - [all-failures email-addr source] |
| 98 | + [all-failures email-addr source since-date] |
54 | 99 | (let [addr (str/lower-case email-addr)] |
55 | 100 | (->> all-failures |
56 | | - (filter (fn [{:keys [from audience] src :source}] |
| 101 | + (filter (fn [{:keys [from audience date] src :source}] |
57 | 102 | (and (= source src) |
| 103 | + (or (nil? since-date) |
| 104 | + (and date (.after ^java.util.Date date since-date))) |
58 | 105 | (case (or audience :author) |
59 | 106 | :author (= addr from) |
60 | 107 | :maintainers true |
|
134 | 181 | "\n"))) |
135 | 182 |
|
136 | 183 | (defn- filter-relevant-reports |
137 | | - "Filter the full report set against a subscription's filters: actionable |
138 | | - type, open, on-source, meets min-priority and min-status, and (optionally) |
139 | | - the :subject-match / :topic substring filters." |
140 | | - [reports {:keys [source min-pri min-sts subj-match topic]}] |
| 184 | + "Filter the full report set against scope filters: actionable type, |
| 185 | + open, on-source, plus optional :subject-match / :topic substrings." |
| 186 | + [reports {:keys [source subj-match topic]}] |
141 | 187 | (let [subj-lc (some-> subj-match str/lower-case) |
142 | 188 | topic-lc (some-> topic str/lower-case)] |
143 | 189 | (cond->> reports |
144 | 190 | true (filter #(contains? actionable-types (:report/type %))) |
145 | 191 | true (filter open?) |
146 | 192 | true (filter #(= source (get-in % [:report/email :email/source]))) |
147 | | - true (filter #(>= (report-priority %) min-pri)) |
148 | | - true (filter #(>= (report-status %) min-sts)) |
149 | 193 | subj-lc (filter #(some-> (get-in % [:report/email :email/subject]) |
150 | 194 | str/lower-case |
151 | 195 | (str/includes? subj-lc))) |
|
154 | 198 | (str/includes? topic-lc)))))) |
155 | 199 |
|
156 | 200 | (defn- build-sections |
157 | | - "Group relevant reports into the three body sections." |
158 | | - [relevant email] |
| 201 | + "Group relevant reports into the three body sections. Sections 1 |
| 202 | + and 2 (owned by you, with or without deadline) always show what you |
| 203 | + own. Section 3 (unacked & unowned) is the noisy one and is the |
| 204 | + only one filtered by min-priority and min-status." |
| 205 | + [relevant email {:keys [min-pri min-sts]}] |
159 | 206 | (let [owned (filter #(owned-by? % email) relevant)] |
160 | 207 | {:dl (->> owned |
161 | 208 | (filter :report/deadline-value) |
|
165 | 212 | (sort-by #(- (report-priority %)))) |
166 | 213 | :unacked (->> relevant |
167 | 214 | (filter unacked?) |
168 | | - (filter unowned?))})) |
| 215 | + (filter unowned?) |
| 216 | + (filter #(>= (report-priority %) min-pri)) |
| 217 | + (filter #(>= (report-status %) min-sts)))})) |
169 | 218 |
|
170 | 219 | (defn- failure-subjects-map |
171 | 220 | "Build {message-id -> subject} for the message-ids referenced by `failures`." |
|
203 | 252 | [db reports email subscription failures] |
204 | 253 | (let [source (:source subscription) |
205 | 254 | prefs {:source source |
206 | | - :min-pri (:min-priority subscription 0) |
| 255 | + :min-pri (:min-priority subscription 1) |
207 | 256 | :min-sts (:min-status subscription 0) |
208 | 257 | :subj-match (:subject-match subscription) |
209 | 258 | :topic (:topic subscription)} |
210 | 259 | relevant (->> (filter-relevant-reports reports prefs) |
211 | 260 | (sort-by (juxt report-priority report-descendant-count) |
212 | 261 | #(compare %2 %1))) |
213 | | - {:keys [dl owned unacked]} (build-sections relevant email) |
| 262 | + {:keys [dl owned unacked]} (build-sections relevant email prefs) |
214 | 263 | sec-fail (failures-section db source failures) |
215 | 264 | sec-dl (section |
216 | 265 | (str "== Upcoming deadlines -- owned by you (" source ") ==") |
|
287 | 336 | (log/info "No :subscribers configured.") |
288 | 337 | (System/exit 0)) |
289 | 338 | (try |
290 | | - (let [db (d/db conn) |
291 | | - src-map (build-source-map config) |
292 | | - reports (all-reports db) |
293 | | - all-failures (load-failures) |
294 | | - pairs (expand-subscribers subscribers) |
295 | | - _ (log/debug (count pairs) "subscription(s) configured") |
296 | | - live-pairs (filter (fn [[_ s]] (source-notify-enabled? src-map (:source s))) pairs) |
297 | | - _ (do (when (< (count live-pairs) (count pairs)) |
298 | | - (doseq [[email s] pairs |
299 | | - :when (not (source-notify-enabled? src-map (:source s)))] |
300 | | - (log/debug "SKIPPED" email |
301 | | - "-- notifications disabled for source" |
302 | | - (:source s)))) |
303 | | - (log/debug (count live-pairs) "after per-source filter")) |
304 | | - sent (atom 0)] |
305 | | - (if (empty? live-pairs) |
306 | | - (log/info "No active subscriptions.") |
307 | | - (doseq [[email subscription] live-pairs] |
308 | | - (let [source (:source subscription) |
309 | | - failures (failures-for-subscriber all-failures email source) |
310 | | - body (build-email-body db reports email subscription failures)] |
311 | | - (if body |
312 | | - (do (log/info (if dry-run? "[dry-run]" "") |
313 | | - "Notifying" email (str "(source: " source ")")) |
314 | | - (when-not dry-run? |
315 | | - (try |
316 | | - (send-notification! smtp email source body) |
317 | | - (swap! sent inc) |
318 | | - (catch Exception e |
319 | | - (log/error "Failed to send to" email |
320 | | - (str "(source: " source "):") |
321 | | - (.getMessage e))))) |
322 | | - (when dry-run? |
323 | | - (println "---") |
324 | | - (println body) |
325 | | - (println "---"))) |
326 | | - (log/info "No open items for" email |
327 | | - (str "(source: " source "),") "skipping."))))) |
328 | | - (log/info "Done." (if dry-run? "Dry run, no emails sent." (str @sent " email(s) sent.")))) |
| 339 | + (let [db (d/db conn) |
| 340 | + src-map (build-source-map config) |
| 341 | + reports (all-reports db) |
| 342 | + all-failures (load-failures) |
| 343 | + last-shown (load-last-failures-shown) |
| 344 | + pairs (expand-subscribers subscribers) |
| 345 | + _ (log/debug (count pairs) "subscription(s) configured") |
| 346 | + live-pairs (filter (fn [[_ s]] (source-notify-enabled? src-map (:source s))) pairs) |
| 347 | + _ (do (when (< (count live-pairs) (count pairs)) |
| 348 | + (doseq [[email s] pairs |
| 349 | + :when (not (source-notify-enabled? src-map (:source s)))] |
| 350 | + (log/debug "SKIPPED" email |
| 351 | + "-- notifications disabled for source" |
| 352 | + (:source s)))) |
| 353 | + (log/debug (count live-pairs) "after per-source filter")) |
| 354 | + sent (atom 0) |
| 355 | + updated-shown (atom last-shown)] |
| 356 | + (try |
| 357 | + (if (empty? live-pairs) |
| 358 | + (log/info "No active subscriptions.") |
| 359 | + (doseq [[email subscription] live-pairs] |
| 360 | + (let [source (:source subscription) |
| 361 | + k (failures-key email source) |
| 362 | + since-ms (get last-shown k) |
| 363 | + since (when since-ms (java.util.Date. (long since-ms))) |
| 364 | + failures (failures-for-subscriber all-failures email source since) |
| 365 | + body (build-email-body db reports email subscription failures)] |
| 366 | + (if body |
| 367 | + (do (log/info (if dry-run? "[dry-run]" "") |
| 368 | + "Notifying" email (str "(source: " source ")")) |
| 369 | + (when-not dry-run? |
| 370 | + (try |
| 371 | + (send-notification! smtp email source body) |
| 372 | + (swap! updated-shown assoc k (.getTime (java.util.Date.))) |
| 373 | + (swap! sent inc) |
| 374 | + (catch Exception e |
| 375 | + ;; Don't advance the timestamp on failure so the |
| 376 | + ;; same failures are retried next run. |
| 377 | + (log/error "Failed to send to" email |
| 378 | + (str "(source: " source "):") |
| 379 | + (.getMessage e))))) |
| 380 | + (when dry-run? |
| 381 | + (println "---") |
| 382 | + (println body) |
| 383 | + (println "---"))) |
| 384 | + (log/info "No open items for" email |
| 385 | + (str "(source: " source "),") "skipping."))))) |
| 386 | + (log/info "Done." (if dry-run? "Dry run, no emails sent." (str @sent " email(s) sent."))) |
| 387 | + (finally |
| 388 | + (when-not dry-run? |
| 389 | + (try (save-last-failures-shown! @updated-shown) |
| 390 | + (catch Exception e |
| 391 | + (log/error "Failed to persist last-notify-failures:" (.getMessage e)))))))) |
329 | 392 | (finally |
330 | 393 | (d/close conn)))))) |
0 commit comments