Browse Source

Failing gracefully on status page if mongo is down

Jim Barritt 11 months ago
parent
commit
4924a967c3

+ 10 - 4
apps/petitions/decode-app/config/server.yaml

@@ -1,20 +1,26 @@
1 1
 development:
2 2
   mongo-port:      27017
3 3
   mongo-host:      "127.0.0.1"
4
+  mongo-server-selection-timeout: 1000
5
+  mongo-connection-timeout: 1000
6
+  mongo-socket-timeout: 1000
4 7
   database:        "decode_sample"
5 8
   chainspace-host: "http://127.0.0.1"
6 9
   chainspace-port: 5010
7
-               
8
-test:        
10
+
11
+test:
9 12
   mongo-port:      27017
10 13
   mongo-host:      "127.0.0.1"
11 14
   database:        "decode-dev-test"
12 15
   chainspace-host: "http://127.0.0.1"
13 16
   chainspace-port: 5010
14
-               
15
-production:  
17
+
18
+production:
16 19
   mongo-port:      27017
17 20
   mongo-host:      "ip-10-0-0-59.eu-west-1.compute.internal"
21
+  mongo-server-selection-timeout: 1000
22
+  mongo-connection-timeout: 10000
23
+  mongo-socket-timeout: 10000
18 24
   database:        "decode"
19 25
   chainspace-host: "ec2-54-194-123-109.eu-west-1.compute.amazonaws.com"
20 26
   chainspace-port: 5010

+ 19 - 11
apps/petitions/decode-app/src/decode_app/database.clj

@@ -4,7 +4,8 @@
4 4
             [monger.operators :refer :all]
5 5
             [monger.result :refer [updated-existing?]]
6 6
             [decode-app.config :as config])
7
-  (:import org.bson.types.ObjectId))
7
+  (:import org.bson.types.ObjectId
8
+           (com.mongodb MongoTimeoutException)))
8 9
 
9 10
 (defn normalise-mongo-id [data]
10 11
   "Swaps _id for id in the map"
@@ -13,7 +14,11 @@
13 14
       (dissoc data :_id))))
14 15
 
15 16
 (defn get-conn [host port]
16
-    (mg/connect {:host host :port port}))
17
+  (let [config (config/get-config)
18
+        options (mg/mongo-options {:server-selection-timeout (:mongo-server-selection-timeout config)
19
+                                   :connect-timeout          (:mongo-connect-timeout config)
20
+                                   :socket-timeout           (:mongo-socket-timeout config)})]
21
+    (mg/connect (mg/server-address host port) options)))
17 22
 
18 23
 (defn get-db []
19 24
   (let [config (config/get-config)
@@ -23,21 +28,24 @@
23 28
     (mg/get-db (get-conn host port) database)))
24 29
 
25 30
 (defn test-connection []
26
-  (let [db (get-db)]
27
-    (let [petition-count (mc/count db "petitions")
28
-          user-count (mc/count db "users")]
29
-      {:petition-count petition-count
30
-       :user-count user-count})))
31
+  (try
32
+    (let [db (get-db)]
33
+      (let [petition-count (mc/count db "petitions")
34
+            user-count (mc/count db "users")]
35
+        {:status         "UP"
36
+         :petition-count petition-count
37
+         :user-count     user-count}))
38
+    (catch Exception e {:status "DOWN" :message (str (.getClass e) " : " (.getMessage e))})))
31 39
 
32 40
 (defn status []
33 41
   (let [config (config/get-config)
34 42
         database (:database config)
35 43
         host (:mongo-host config)
36 44
         port (:mongo-port config)]
37
-    {:db-type "mongo"
38
-     :host host
39
-     :port port
40
-     (keyword database) (test-connection)}))
45
+    {(keyword database) (merge {:db-type           "mongo"
46
+                                :host              host
47
+                                :port              port}
48
+                               (test-connection)) }))
41 49
 
42 50
 (defn find-by-query [collection query]
43 51
   (let [db (get-db)