1
0
Fork 0
mirror of synced 2024-07-12 18:05:55 +12:00

Merge pull request #7402 from appwrite/fix-limit-failed-webhook-attempts-review

Webhook attempts PR suggestions
This commit is contained in:
Khushboo Verma 2024-01-08 16:07:24 +05:30 committed by GitHub
commit e62f3eeffb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -120,24 +120,26 @@ class Webhooks extends Action
\curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
$curl_exec_result = \curl_exec($ch);
$responseBody = \curl_exec($ch);
$curlError = \curl_error($ch);
$statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
\curl_close($ch);
if (\curl_error($ch) !== '' || \curl_getinfo($ch, CURLINFO_RESPONSE_CODE) >= 400) {
if (!empty($curlError) || $statusCode >= 400) {
$dbForConsole->increaseDocumentAttribute('webhooks', $webhook->getId(), 'attempts', 1);
$webhook = $dbForConsole->getDocument('webhooks', $webhook->getId());
$attempts = $webhook->getAttribute('attempts');
$logs = json_encode([
'URL' => $webhook->getAttribute('url'),
'Method' => 'POST',
]);
$logs = '';
$logs .= 'URL: ' . $webhook->getAttribute('url') . "\n";
$logs .= 'Method: ' . 'POST' . "\n";
if (\curl_error($ch) !== '') {
$logs['CurlError'] = \curl_error($ch);
$logs['Events'] = implode(', ', $events);
if (!empty($curlError)) {
$logs .= 'CURL Error: ' . $curlError . "\n";
$logs .= 'Events: ' . implode(', ', $events) . "\n";
} else {
$logs['Response status code'] = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
$logs['Response body'] = $curl_exec_result;
$logs .= 'Status code: ' . $statusCode . "\n";
$logs .= 'Body: ' . "\n" . \mb_strcut($responseBody, 0, 10000) . "\n"; // Limit to 10kb
}
$webhook->setAttribute('logs', $logs);
@ -152,8 +154,8 @@ class Webhooks extends Action
$this->errors[] = $logs;
} else {
$webhook->setAttribute('attempts', 0); // Reset attempts on success
$dbForConsole->updateDocument('webhooks', $webhook->getId(), $webhook);
$dbForConsole->deleteCachedDocument('projects', $project->getId());
}
\curl_close($ch);
}
}