Claiming retry/backoff (#10482)
* claiming retry/backoff logic
Timotej S committed
Jan 15, 2021 at 15:21 UTC
5a898b28c9a82ad39d5442568b71a8306f6d2ae9
1 file changed
+36
-17
claim/netdata-claim.sh.in
+36
-17
@@ -289,26 +289,45 @@ fi
289
if [ "${VERBOSE}" == 1 ]; then
290
echo "${URLCOMMAND} \"${TARGET_URL}\""
291
fi
292
-eval "${URLCOMMAND} \"${TARGET_URL}\"" >"${CLAIMING_DIR}/tmpout.txt"
293
-URLCOMMAND_EXIT_CODE=$?
294
-if [ "${URLTOOL}" = "wget" ] && [ "${URLCOMMAND_EXIT_CODE}" -eq 8 ] ; then
295
-# We consider the server issuing an error response a successful attempt at communicating
296
- URLCOMMAND_EXIT_CODE=0
297
-fi
292
299
-rm -f "${CLAIMING_DIR}/tmpin.txt"
293
+attempt_contact () {
294
+ eval "${URLCOMMAND} \"${TARGET_URL}\"" >"${CLAIMING_DIR}/tmpout.txt"
295
+ URLCOMMAND_EXIT_CODE=$?
296
+ if [ "${URLTOOL}" = "wget" ] && [ "${URLCOMMAND_EXIT_CODE}" -eq 8 ] ; then
297
+ # We consider the server issuing an error response a successful attempt at communicating
298
+ URLCOMMAND_EXIT_CODE=0
299
+ fi
300
301
-# Check if URLCOMMAND connected and received reply
302
-if [ "${URLCOMMAND_EXIT_CODE}" -ne 0 ] ; then
303
- echo >&2 "Failed to connect to ${URL_BASE}, return code ${URLCOMMAND_EXIT_CODE}"
304
- rm -f "${CLAIMING_DIR}/tmpout.txt"
305
- exit 4
306
-fi
301
+ # Check if URLCOMMAND connected and received reply
302
+ if [ "${URLCOMMAND_EXIT_CODE}" -ne 0 ] ; then
303
+ echo >&2 "Failed to connect to ${URL_BASE}, return code ${URLCOMMAND_EXIT_CODE}"
304
+ rm -f "${CLAIMING_DIR}/tmpout.txt"
305
+ return 4
306
+ fi
307
308
-if [ "${VERBOSE}" == 1 ] ; then
309
- echo "Response from server:"
310
- cat "${CLAIMING_DIR}/tmpout.txt"
311
-fi
308
+ if [ "${VERBOSE}" == 1 ] ; then
309
+ echo "Response from server:"
310
+ cat "${CLAIMING_DIR}/tmpout.txt"
311
+ fi
312
+
313
+ return 0
314
+}
315
+
316
+for i in {1..5}
317
+do
318
+ if attempt_contact ; then
319
+ echo "Connection attempt $i successful"
320
+ break
321
+ fi
322
+ echo "Connection attempt $i failed. Retry in ${i}s."
323
+ if [ "$i" -eq 5 ] ; then
324
+ rm -f "${CLAIMING_DIR}/tmpin.txt"
325
+ exit 4
326
+ fi
327
+ sleep "$i"
328
+done
329
+
330
+rm -f "${CLAIMING_DIR}/tmpin.txt"
331
332
ERROR_KEY=$(grep "\"errorMsgKey\":" "${CLAIMING_DIR}/tmpout.txt" | awk -F "errorMsgKey\":\"" '{print $2}' | awk -F "\"" '{print $1}')
333
case ${ERROR_KEY} in