Skip to content

Commit 529118b

Browse files
authored
Merge pull request #9 from PractiTest/keep-running-with-500
for call fail attempts try another 10 times and then stop
2 parents 656ff86 + 74a94c4 commit 529118b

1 file changed

Lines changed: 53 additions & 29 deletions

File tree

src/practitest_firecracker/practitest.clj

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
;; utils
1717

1818
(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)
1921
(def run-batch-bucket-size 10)
2022

2123
(def custom-field-cache (atom {}))
@@ -27,38 +29,60 @@
2729
(defn build-uri [base-uri resource-uri-template & params]
2830
(apply format (str base-uri resource-uri-template) params))
2931

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+
3037
(defn api-call [{:keys [credentials uri method query-params form-params]}]
3138
(assert (not (and query-params form-params))
3239
"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))))))
6286

6387
;; ===========================================================================
6488
;; constants

0 commit comments

Comments
 (0)