Skip to content

Commit

Permalink
webhook: added last failure notification to webhook failure (#1853)
Browse files Browse the repository at this point in the history
  • Loading branch information
gioelecerati authored Aug 17, 2023
1 parent 0f085ee commit da749f0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/api/src/store/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace messages {
stream?: DBStream;
retries?: number;
lastInterval?: number;
lastFailureNotification?: number;
}

export type TaskInfo = {
Expand Down
24 changes: 23 additions & 1 deletion packages/api/src/webhooks/cannon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export default class WebhookCannon {
`Webhook Cannon| Max Retries Reached, id: ${trigger.id}, streamId: ${trigger.stream?.id}`
);
try {
this.notifyFailedWebhook(trigger, webhookPayload, err);
trigger = webhookFailNotification(trigger, webhookPayload, err);
} catch (err) {
console.error(
`Webhook Cannon| Error sending notification email to user, id: ${trigger.id}, streamId: ${trigger.stream?.id}`
Expand Down Expand Up @@ -613,6 +613,28 @@ export async function storeTriggerStatus(
}
}

export function webhookFailNotification(
trigger: messages.WebhookTrigger,
webhookPayload: RequestInitWithTimeout,
err: Error
): messages.WebhookTrigger {
const lastFailureNotification = trigger?.lastFailureNotification;
const currentTime = Date.now();
if (
!lastFailureNotification ||
currentTime - lastFailureNotification > 24 * 60 * 60 * 1000
) {
this.notifyFailedWebhook(trigger, webhookPayload, err);
}

trigger = {
...trigger,
lastFailureNotification: currentTime,
};

return trigger;
}

export function signatureHeaders(
payload: string,
sharedSecret: string,
Expand Down

0 comments on commit da749f0

Please sign in to comment.