Skip to content

Commit

Permalink
Merge pull request #3 from Rocketseat/feat/retry-webhooks
Browse files Browse the repository at this point in the history
feat: show how many retries have been made to an webhook log
  • Loading branch information
josepholiveira authored Mar 3, 2024
2 parents 743f972 + f6f21a3 commit 2889583
Show file tree
Hide file tree
Showing 11 changed files with 2,188 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export default async function WebhookLogsDetailsLoading() {
<Skeleton className="h-5 w-32" />
</TableCell>
</TableRow>
<TableRow>
<TableCell>Number of retries</TableCell>
<TableCell>
<Skeleton className="h-5 w-20" />
</TableCell>
</TableRow>
<TableRow>
<TableCell>HTTP Status</TableCell>
<TableCell>
Expand Down
27 changes: 27 additions & 0 deletions apps/web/src/app/(app)/settings/developers/logs/[logId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ export default async function WebhookLogsDetails({
})
: null

const nextRetryDelay = Math.round(
Math.min(86400, Math.exp(2.5 * webhookLog.numberOfRetries)),
)

const nextRetryDate = dayjs(webhookLog.finishedAt).add(
nextRetryDelay,
'second',
)

const isNextRetryDateInFuture = nextRetryDate.isAfter(dayjs())

const formattedNextRetryDate = isNextRetryDateInFuture
? dayjs().to(nextRetryDate, true)
: ''

const nextRetryDateFormatted = `(Next retry in ${formattedNextRetryDate.trim()})`

return (
<div className="space-y-4 p-6">
<div className="flex items-center gap-2">
Expand Down Expand Up @@ -112,6 +129,16 @@ export default async function WebhookLogsDetails({
<TableCell>Error reason</TableCell>
<TableCell>{webhookLog.errorReason || '-'}</TableCell>
</TableRow>
<TableRow>
<TableCell>Number of retries</TableCell>
<TableCell>
{webhookLog.status === 'ERROR'
? `${webhookLog.numberOfRetries} ${
isNextRetryDateInFuture ? nextRetryDateFormatted : ''
}`
: '-'}
</TableCell>
</TableRow>
<TableRow>
<TableCell>HTTP Status</TableCell>
<TableCell>
Expand Down
8 changes: 7 additions & 1 deletion apps/web/src/app/api/webhooks/external/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ export const runtime = 'edge'

async function handler(request: NextRequest) {
const requestBody = await request.json()
const numberOfRetries = Number(request.headers.get('Upstash-Retried')) || 0

const webhookEvent = webhookEventSchema.parse(requestBody)
const webhookData = {
...requestBody,
numberOfRetries,
}

const webhookEvent = webhookEventSchema.parse(webhookData)

const { success } = await handleWebhookEvent(webhookEvent)

Expand Down
1 change: 1 addition & 0 deletions packages/drizzle/migrations/0012_unique_black_tom.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "company_webhook_logs" ADD COLUMN "number_of_retries" integer DEFAULT 0;
1 change: 1 addition & 0 deletions packages/drizzle/migrations/0013_nervous_abomination.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "company_webhook_logs" ALTER COLUMN "number_of_retries" SET NOT NULL;
Loading

0 comments on commit 2889583

Please sign in to comment.