|
16 | 16 | ;; utils |
17 | 17 |
|
18 | 18 | (def backoff-timeout "Backoff timeout in seconds" 20) |
| 19 | +(def max-attempts "Number of attempts to run" 10) |
| 20 | +(def timeout-between-attempts "Timeout in seconds between attempts" 1) |
19 | 21 | (def run-batch-bucket-size 10) |
20 | 22 |
|
21 | 23 | (def custom-field-cache (atom {})) |
|
27 | 29 | (defn build-uri [base-uri resource-uri-template & params] |
28 | 30 | (apply format (str base-uri resource-uri-template) params)) |
29 | 31 |
|
| 32 | +(defn throw-api-exception [ex-info status body uri] |
| 33 | + (throw (ex-info "API request failed" {:status status |
| 34 | + :body body |
| 35 | + :uri uri}))) |
| 36 | + |
30 | 37 | (defn api-call [{:keys [credentials uri method query-params form-params]}] |
31 | 38 | (assert (not (and query-params form-params)) |
32 | 39 | "both `query-params` and `form-params` can't be specified") |
33 | | - (loop [results [] |
34 | | - uri uri |
35 | | - params (cond-> {:basic-auth credentials |
36 | | - :throw-exceptions false |
37 | | - :as :json} |
38 | | - query-params (assoc :query-params (conj query-params {:source "firecracker" :firecracker-version fc-version}) ) |
39 | | - form-params (assoc :form-params (conj form-params {:source "firecracker" :firecracker-version fc-version}) :content-type :json))] |
40 | | - (if (nil? uri) |
41 | | - results |
42 | | - (let [{:keys [status body]} (method uri params)] |
43 | | - (case status |
44 | | - 504 (do |
45 | | - ;; load balancer freaking out, lets try again |
46 | | - (Thread/sleep 1000) |
47 | | - (recur results uri params)) |
48 | | - 429 (do |
49 | | - (log/warnf "API rate limit reached, waiting for %s seconds" backoff-timeout) |
50 | | - (Thread/sleep (* backoff-timeout 1000)) |
51 | | - (recur results uri params)) |
52 | | - 200 (let [data (:data body)] |
53 | | - (recur (vec |
54 | | - (if (sequential? data) |
55 | | - (concat results data) |
56 | | - (conj results data))) |
57 | | - (get-in body [:links :next]) |
58 | | - (dissoc params :query-params))) |
59 | | - (throw (ex-info "API request failed" {:status status |
60 | | - :body body |
61 | | - :uri uri}))))))) |
| 40 | + (loop [results [] |
| 41 | + uri uri |
| 42 | + attempts max-attempts |
| 43 | + params (cond-> {:basic-auth credentials |
| 44 | + :throw-exceptions false |
| 45 | + :as :json} |
| 46 | + query-params (assoc :query-params (conj query-params {:source "firecracker" :firecracker-version fc-version}) ) |
| 47 | + form-params (assoc :form-params (conj form-params {:source "firecracker" :firecracker-version fc-version}) :content-type :json))] |
| 48 | + (if (nil? uri) |
| 49 | + results |
| 50 | + (let [{:keys [status body]} (method uri params)] |
| 51 | + (if (> attempts 0) |
| 52 | + (case status |
| 53 | + 504 (do |
| 54 | + ;; load balancer freaking out, lets try again |
| 55 | + (Thread/sleep (* timeout-between-attempts 1000)) |
| 56 | + (recur results uri (dec attempts) params)) |
| 57 | + 502 (do |
| 58 | + ;; 502 Bad Gateway Error, lets try again |
| 59 | + (log/warnf "%s responded with 502 - %s more attempts" uri attempts) |
| 60 | + (Thread/sleep (* timeout-between-attempts 1000)) |
| 61 | + (recur results uri (dec attempts) params)) |
| 62 | + 500 (do |
| 63 | + ;; 500 Bad Gateway Error, lets try again |
| 64 | + (log/warnf "%s responded with 500 - %s more attempts" uri attempts) |
| 65 | + (Thread/sleep (* timeout-between-attempts 1000)) |
| 66 | + (recur results uri (dec attempts) params)) |
| 67 | + 503 (do |
| 68 | + ;; 503 Service Unavailable, lets try again |
| 69 | + (log/warnf "%s responded with 503 - %s more attempts" uri attempts) |
| 70 | + (Thread/sleep (* timeout-between-attempts 1000)) |
| 71 | + (recur results uri (dec attempts) params)) |
| 72 | + 429 (do |
| 73 | + (log/warnf "API rate limit reached, waiting for %s seconds" backoff-timeout) |
| 74 | + (Thread/sleep (* backoff-timeout 1000)) |
| 75 | + (recur results (dec attempts) uri params)) |
| 76 | + 200 (let [data (:data body)] |
| 77 | + (recur (vec |
| 78 | + (if (sequential? data) |
| 79 | + (concat results data) |
| 80 | + (conj results data))) |
| 81 | + (get-in body [:links :next]) |
| 82 | + attempts |
| 83 | + (dissoc params :query-params))) |
| 84 | + (throw-api-exception ex-info status body uri)) |
| 85 | + (throw-api-exception ex-info status body uri)))))) |
62 | 86 |
|
63 | 87 | ;; =========================================================================== |
64 | 88 | ;; constants |
|
0 commit comments