Skip to content

Commit

Permalink
Adding retries to job status post (HTTP request may fail).
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Krulis committed Oct 9, 2020
1 parent 70c4233 commit 067964a
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/handlers/status_notifier_handler.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "../helpers/curl.h"
#include "status_notifier_handler.h"

#include <map>
#include <sstream>
#include <thread>
#include <chrono>

#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";
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit 067964a

Please sign in to comment.