-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
metabot_lifecycle.clj
44 lines (33 loc) · 1.84 KB
/
metabot_lifecycle.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
(ns metabase.events.metabot-lifecycle
(:require [clojure.core.async :as async]
[clojure.tools.logging :as log]
[metabase
[events :as events]
[metabot :as metabot]]))
(def ^:const ^:private metabot-lifecycle-topics
"The `Set` of event topics which are subscribed to for use in metabot lifecycle."
#{:settings-update})
(def ^:private metabot-lifecycle-channel
"Channel for receiving event notifications we want to subscribe to for metabot lifecycle events."
(async/chan))
;;; ## ---------------------------------------- EVENT PROCESSING ----------------------------------------
(defn process-metabot-lifecycle-event
"Handle processing for a single event notification received on the metabot-lifecycle-channel"
[metabot-lifecycle-event]
;; try/catch here to prevent individual topic processing exceptions from bubbling up. better to handle them here.
(when-let [{topic :topic object :item} metabot-lifecycle-event]
(try
;; if someone updated our slack-token, or metabot was enabled/disabled then react accordingly
(when (and (contains? object :metabot-enabled) (contains? object :slack-token))
(let [{:keys [slack-token metabot-enabled]} object]
(cond
(nil? slack-token) (metabot/stop-metabot!)
(not metabot-enabled) (metabot/stop-metabot!)
:else (metabot/restart-metabot!))))
(catch Throwable e
(log/warn (format "Failed to process driver notifications event. %s" topic) e)))))
;;; ## ---------------------------------------- LIFECYLE ----------------------------------------
(defn events-init
"Automatically called during startup; start event listener for metabot lifecycle events."
[]
(events/start-event-listener! metabot-lifecycle-topics metabot-lifecycle-channel process-metabot-lifecycle-event))