Skip to content

Commit 73ab859

Browse files
committed
change some logging; catch an exception, move along
1 parent ef1aa6a commit 73ab859

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

src/firedamp/core.clj

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
[twitter.oauth :as tw-auth]
1515
[twitter.api.restful :as tw-api]
1616
[twitter.callbacks.handlers :as tw-handlers])
17-
(:import
18-
(twitter.callbacks.protocols SyncSingleCallback))
1917
(:gen-class))
2018

2119
(def github "https://status.github.com/api/status.json")
@@ -25,7 +23,7 @@
2523
(def status-io-good "All Systems Operational")
2624
(def github-good "good")
2725

28-
(def state (atom {:alarm-state ::bad
26+
(def state (atom {:alarm-state ::good
2927
:last-update (time/now)}))
3028

3129
(defn parse-github
@@ -39,13 +37,18 @@
3937
(defn fetch-json-status!
4038
[url]
4139
(timbre/info "fetching url" url)
42-
(md/chain
43-
(http/get url)
44-
:body
45-
bs/to-reader
46-
(fn [s]
47-
(timbre/info "got response from" url)
48-
(json/parse-stream s true))))
40+
;; these should be able to timeout
41+
(->
42+
(md/chain
43+
(http/get url)
44+
:body
45+
bs/to-reader
46+
(fn [s]
47+
(timbre/info "got response from" url)
48+
(json/parse-stream s true)))
49+
(md/catch Exception
50+
(fn [exc] (timbre/warn "exception while fetching" exc)))))
51+
4952

5053
(defn get-parse-statuses!
5154
"Fetch and parse the status messages for all of the providers.
@@ -75,15 +78,27 @@
7578
[::bad ::bad] ::dark
7679
[::bad ::good] ::brightening))
7780

81+
(defn setup-twitter
82+
[env]
83+
(let [{:keys [api-key api-secret access-token access-token-secret]} env
84+
token (tw-auth/make-oauth-creds api-key
85+
api-secret
86+
access-token
87+
access-token-secret)]
88+
token))
89+
7890
(defn tweet!
7991
[message token]
8092
(timbre/info "tweeting" message)
81-
(md/chain
82-
;; hmm. wrongness
83-
(md/future
84-
(tw-api/statuses-update :oauth-creds token
85-
:params {:status message}))
86-
#(timbre/info "tweeted")))
93+
(let [token (setup-twitter env/env)]
94+
(->
95+
(md/chain
96+
;; this should also be able to timeout
97+
(md/future (tw-api/statuses-update :oauth-creds token :params {:status message}))
98+
#(timbre/info "tweeted"))
99+
(md/catch
100+
Exception
101+
(fn [exc] (timbre/warn "exception while tweeting:" exc))))))
87102

88103
(defn tweet-alert!
89104
[token status]
@@ -108,12 +123,14 @@
108123

109124
(defn run-world!
110125
[]
111-
(md/let-flow [statuses (get-parse-statuses!)]
126+
(let [old-state @state]
112127
(md/chain
113-
(alert! @state statuses)
128+
(get-parse-statuses!)
129+
#(alert! @state %)
114130
(fn [new-world]
115131
(reset! state new-world)
116-
(timbre/info "current state of the world" (:alarm-state @state))))))
132+
(timbre/infof "s0=%s, s1=%s" (:alarm-state old-state) (:alarm-state @state))))))
133+
117134

118135
(defn keep-checking
119136
[period]
@@ -123,17 +140,7 @@
123140
[]
124141
(.start (Thread. (fn [] (.join (Thread/currentThread))) "staying alive")))
125142

126-
(defn setup-twitter
127-
[env]
128-
(let [{:keys [api-key api-secret access-token access-token-secret]} env
129-
token (tw-auth/make-oauth-creds api-key
130-
api-secret
131-
access-token
132-
access-token-secret)]
133-
token))
134-
135143
(defn -main
136144
[& args]
137145
(staying-alive)
138-
(swap! state conj {:token (setup-twitter env/env)})
139-
(keep-checking (mt/minutes 2)))
146+
(keep-checking (mt/minutes 1)))

0 commit comments

Comments
 (0)