From 067964a23f6c2450af74d07de748a267b4d43668 Mon Sep 17 00:00:00 2001 From: Martin Krulis Date: Fri, 9 Oct 2020 02:29:23 +0200 Subject: [PATCH] Adding retries to job status post (HTTP request may fail). --- src/handlers/status_notifier_handler.cpp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/handlers/status_notifier_handler.cpp b/src/handlers/status_notifier_handler.cpp index 82204c9..1cc7ab4 100644 --- a/src/handlers/status_notifier_handler.cpp +++ b/src/handlers/status_notifier_handler.cpp @@ -1,8 +1,11 @@ +#include "../helpers/curl.h" +#include "status_notifier_handler.h" + #include #include +#include +#include -#include "../helpers/curl.h" -#include "status_notifier_handler.h" const std::string status_notifier_handler::TYPE_ERROR = "error"; const std::string status_notifier_handler::TYPE_JOB_STATUS = "job-status"; @@ -47,9 +50,17 @@ void status_notifier_handler::on_request( ss << "/" << id; } - try { - helpers::curl_post(ss.str(), config_.port, params, config_.username, config_.password); - } catch (helpers::curl_exception &exception) { - logger_->critical("curl failed: {}", exception.what()); + std::size_t retries = 4; // we come by this value using digital suction + auto delay = std::chrono::seconds(1); + while (retries > 0) { + --retries; + try { + helpers::curl_post(ss.str(), config_.port, params, config_.username, config_.password); + return; // success, nothing else to try + } catch (helpers::curl_exception &exception) { + logger_->critical("curl failed: {}", exception.what()); + std::this_thread::sleep_for(delay); + delay *= 2; // exponentially growing waiting intervals are cool + } } }