Retry habitat upload in case of save conflict

pull/27/head
David Brooke 2016-09-05 20:46:55 +01:00
rodzic 5de3db0cbd
commit 1ec0908fd1
1 zmienionych plików z 30 dodań i 15 usunięć

Wyświetl plik

@ -72,6 +72,8 @@ UploadTelemetryPacket( telemetry_t * t )
struct curl_slist *headers = NULL;
time_t rawtime;
struct tm *tm;
int retries;
long int http_resp;
// Get formatted timestamp
time( &rawtime );
@ -85,7 +87,7 @@ UploadTelemetryPacket( telemetry_t * t )
curl_easy_setopt( curl, CURLOPT_TIMEOUT, 15 );
// RJH capture http errors and report
curl_easy_setopt( curl, CURLOPT_FAILONERROR, 1 );
// curl_easy_setopt( curl, CURLOPT_FAILONERROR, 1 );
curl_easy_setopt( curl, CURLOPT_ERRORBUFFER, curl_error );
// Avoid curl library bug that happens if above timeout occurs (sigh)
@ -135,21 +137,34 @@ UploadTelemetryPacket( telemetry_t * t )
curl_easy_setopt( curl, CURLOPT_CUSTOMREQUEST, "PUT" );
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, json );
// Perform the request, res will get the return code
res = curl_easy_perform( curl );
retries = 0;
do {
// Perform the request, res will get the return code
res = curl_easy_perform( curl );
// Check for errors
if ( res == CURLE_OK )
{
// LogMessage("OK\n");
}
else
{
LogMessage( "Failed for URL '%s'\n", url );
LogMessage( "curl_easy_perform() failed: %s\n",
curl_easy_strerror( res ) );
LogMessage( "error: %s\n", curl_error );
}
// Check for errors
if ( res == CURLE_OK )
{
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if ( http_resp != 201 && http_resp != 409 )
{
// unhandled response
LogMessage( "Failed for URL '%s'\n", url );
LogMessage( "curl_easy_perform() failed: %s\n",
curl_easy_strerror( res ) );
LogMessage( "error: %s\n", curl_error );
LogMessage( "HTTP response %ld\n", http_resp );
}
}
else
{
http_resp = 0;
LogMessage( "Failed for URL '%s'\n", url );
LogMessage( "curl_easy_perform() failed: %s\n",
curl_easy_strerror( res ) );
LogMessage( "error: %s\n", curl_error );
}
} while ((http_resp == 409) && (++retries < 5));
// always cleanup
curl_slist_free_all( headers );