Skip to content

Commit

Permalink
fix: Omnichannel webhook is not retrying requests (#30687)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman committed Oct 20, 2023
1 parent 4fb5523 commit bba3c9d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-pans-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

fix: Omnichannel webhook is not retrying requests
20 changes: 13 additions & 7 deletions apps/meteor/app/livechat/server/lib/LivechatTyped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,12 +796,15 @@ class LivechatClass {
attempts = 10,
) {
if (!attempts) {
Livechat.logger.error({ msg: 'Omnichannel webhook call failed. Max attempts reached' });
return;
}
const timeout = settings.get<number>('Livechat_http_timeout');
const secretToken = settings.get<string>('Livechat_secret_token');
const webhookUrl = settings.get<string>('Livechat_webhookUrl');
try {
const result = await fetch(settings.get('Livechat_webhookUrl'), {
Livechat.webhookLogger.debug({ msg: 'Sending webhook request', postData });
const result = await fetch(webhookUrl, {
method: 'POST',
headers: {
...(secretToken && { 'X-RocketChat-Livechat-Token': secretToken }),
Expand All @@ -812,17 +815,20 @@ class LivechatClass {

if (result.status === 200) {
metrics.totalLivechatWebhooksSuccess.inc();
} else {
metrics.totalLivechatWebhooksFailures.inc();
return result;
}
return result;

metrics.totalLivechatWebhooksFailures.inc();
throw new Error(await result.text());
} catch (err) {
Livechat.webhookLogger.error({ msg: `Response error on ${11 - attempts} try ->`, err });
const retryAfter = timeout * 4;
Livechat.webhookLogger.error({ msg: `Error response on ${11 - attempts} try ->`, err });
// try 10 times after 20 seconds each
attempts - 1 && Livechat.webhookLogger.warn(`Will try again in ${(timeout / 1000) * 4} seconds ...`);
attempts - 1 &&
Livechat.webhookLogger.warn({ msg: `Webhook call failed. Retrying`, newAttemptAfterSeconds: retryAfter / 1000, webhookUrl });
setTimeout(async () => {
await Livechat.sendRequest(postData, attempts - 1);
}, timeout * 4);
}, retryAfter);
}
}

Expand Down

0 comments on commit bba3c9d

Please sign in to comment.