Skip to content

Commit 293b8fa

Browse files
authored
Merge pull request #467 from mrrobot47/fix/api-retry
Enhance and stabilize API retry handling
2 parents bc2b9a0 + 099c8f9 commit 293b8fa

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

src/helper/Site_Backup_Restore.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,9 +1181,10 @@ private function send_dash_failure_callback( $ed_api_url, $backup_id, $verify_to
11811181
private function send_dash_request( $endpoint, $payload ) {
11821182
$max_retries = 3;
11831183
$retry_delay = 300; // 5 minutes in seconds
1184-
$attempt = 0;
1184+
$max_attempts = $max_retries + 1; // 1 initial attempt + 3 retries = 4 total
1185+
$attempt = 1;
11851186

1186-
while ( $attempt <= $max_retries ) {
1187+
while ( $attempt <= $max_attempts ) {
11871188
$ch = curl_init( $endpoint );
11881189

11891190
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
@@ -1200,41 +1201,49 @@ private function send_dash_request( $endpoint, $payload ) {
12001201

12011202
curl_close( $ch );
12021203

1204+
// Normalize response for safe string concatenation
1205+
$response_text = ( false === $response ) ? 'No response received' : $response;
1206+
12031207
// Check if request was successful
12041208
if ( ! $error && $http_code >= 200 && $http_code < 300 ) {
12051209
EE::log( 'EasyEngine Dashboard callback sent successfully.' );
1206-
EE::debug( 'EasyEngine Dashboard response: ' . $response );
1210+
EE::debug( 'EasyEngine Dashboard response: ' . $response_text );
12071211
return; // Success, exit the retry loop
12081212
}
12091213

12101214
// Check if it's a 5xx error (server error) that should be retried
12111215
$is_5xx_error = $http_code >= 500 && $http_code < 600;
12121216

1213-
if ( $is_5xx_error && $attempt < $max_retries ) {
1214-
$attempt++;
1217+
if ( $is_5xx_error && $attempt < $max_attempts ) {
12151218
EE::warning( sprintf(
12161219
'EasyEngine Dashboard callback failed with HTTP %d (attempt %d/%d). Retrying in %d seconds...',
12171220
$http_code,
12181221
$attempt,
1219-
$max_retries,
1222+
$max_attempts,
12201223
$retry_delay
12211224
) );
1222-
EE::debug( 'Response: ' . $response );
1225+
EE::debug( 'Response: ' . $response_text );
12231226
sleep( $retry_delay );
1227+
$attempt++; // Increment at end of loop iteration
12241228
} else {
12251229
// Either not a 5xx error, or we've exhausted all retries
12261230
if ( $error ) {
1231+
// cURL error occurred (network, DNS, timeout, etc.)
12271232
EE::warning( 'Failed to send callback to EasyEngine Dashboard: ' . $error );
12281233
} elseif ( $is_5xx_error ) {
1234+
// 5xx error after all retries exhausted
12291235
EE::warning( sprintf(
12301236
'EasyEngine Dashboard callback failed after %d retries with HTTP %d. Response: %s',
12311237
$max_retries,
12321238
$http_code,
1233-
$response
1239+
$response_text
12341240
) );
1241+
} elseif ( $http_code === 0 ) {
1242+
// No HTTP response received (may indicate network/cURL issue without explicit error)
1243+
EE::warning( 'EasyEngine Dashboard callback failed: No HTTP response received. This may indicate a network or cURL error. Response: ' . $response_text );
12351244
} else {
1236-
// 4xx or other error codes that shouldn't be retried
1237-
EE::warning( 'EasyEngine Dashboard callback returned HTTP ' . $http_code . '. Response: ' . $response );
1245+
// 4xx or other HTTP error codes that shouldn't be retried
1246+
EE::warning( 'EasyEngine Dashboard callback returned HTTP ' . $http_code . '. Response: ' . $response_text );
12381247
}
12391248
break; // Exit the retry loop
12401249
}

0 commit comments

Comments
 (0)