|
16 | 16 | ;; utils |
17 | 17 |
|
18 | 18 | (def backoff-timeout "Backoff timeout in seconds" 20) |
| 19 | +(def num-attemps "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 {})) |
|
37 | 39 | "both `query-params` and `form-params` can't be specified") |
38 | 40 | (loop [results [] |
39 | 41 | uri uri |
40 | | - attempts 10 |
| 42 | + attempts num-attemps |
41 | 43 | params (cond-> {:basic-auth credentials |
42 | 44 | :throw-exceptions false |
43 | 45 | :as :json} |
44 | 46 | query-params (assoc :query-params (conj query-params {:source "firecracker" :firecracker-version fc-version}) ) |
45 | 47 | form-params (assoc :form-params (conj form-params {:source "firecracker" :firecracker-version fc-version}) :content-type :json))] |
46 | 48 | (if (nil? uri) |
47 | | - results |
48 | | - (let [{:keys [status body]} (method uri params)] |
49 | | - (case status |
50 | | - 504 (do |
51 | | - ;; load balancer freaking out, lets try again |
52 | | - (Thread/sleep 1000) |
53 | | - (recur results uri (dec attempts) params)) |
54 | | - 502 (if (> attempts 0) |
55 | | - (do |
56 | | - ;; 502 Bad Gateway Error, lets try again |
57 | | - (log/warnf "%s responded with 502 - %s more attempts" uri attempts) |
58 | | - (Thread/sleep 10) |
59 | | - (recur results uri (dec attempts) params)) |
60 | | - (throw-api-exception ex-info status body uri)) |
61 | | - 500 (if (> attempts 0) |
62 | | - (do |
63 | | - ;; 500 Bad Gateway Error, lets try again |
64 | | - (log/warnf "%s responded with 500 - %s more attempts" uri attempts) |
65 | | - (Thread/sleep 10) |
66 | | - (recur results uri (dec attempts) params)) |
67 | | - (throw-api-exception ex-info status body uri)) |
68 | | - 503 (if (> attempts 0) |
69 | | - (do |
70 | | - ;; 503 Service Unavailable, lets try again |
71 | | - (log/warnf "%s responded with 503 - %s more attempts" uri attempts) |
72 | | - (Thread/sleep 10)) |
73 | | - (recur results uri (dec attempts) params)) |
74 | | - 429 (do |
75 | | - (log/warnf "API rate limit reached, waiting for %s seconds" backoff-timeout) |
76 | | - (Thread/sleep (* backoff-timeout 1000)) |
77 | | - (recur results (dec attempts) uri params)) |
78 | | - 200 (let [data (:data body)] |
79 | | - (recur (vec |
80 | | - (if (sequential? data) |
81 | | - (concat results data) |
82 | | - (conj results data))) |
83 | | - (get-in body [:links :next]) |
84 | | - (dec attempts) |
85 | | - (dissoc params :query-params))) |
86 | | - (throw-api-exception ex-info status body 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)))))) |
87 | 86 |
|
88 | 87 | ;; =========================================================================== |
89 | 88 | ;; constants |
|
0 commit comments