From 4b764436e5ee8fe379df8678c55377b466d1d601 Mon Sep 17 00:00:00 2001 From: Carina Wollendorfer <30310907+CarinaWolli@users.noreply.github.com> Date: Mon, 18 Mar 2024 11:47:12 -0400 Subject: [PATCH] catch error (#14104) Co-authored-by: CarinaWolli --- packages/features/webhooks/lib/cron.ts | 38 ++++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/packages/features/webhooks/lib/cron.ts b/packages/features/webhooks/lib/cron.ts index 92ca8b0712..79dc78a6ef 100644 --- a/packages/features/webhooks/lib/cron.ts +++ b/packages/features/webhooks/lib/cron.ts @@ -2,6 +2,7 @@ import type { NextApiRequest, NextApiResponse } from "next"; import dayjs from "@calcom/dayjs"; +import logger from "@calcom/lib/logger"; import { defaultHandler } from "@calcom/lib/server"; import prisma from "@calcom/prisma"; @@ -14,6 +15,15 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { return; } + // make sure to not trigger webhooks that are too far in the past (in case cron job was down for a while) + await prisma.webhookScheduledTriggers.deleteMany({ + where: { + startAfter: { + lte: dayjs().subtract(1, "day").toISOString(), + }, + }, + }); + // get jobs that should be run const jobsToRun = await prisma.webhookScheduledTriggers.findMany({ where: { @@ -27,18 +37,30 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { for (const job of jobsToRun) { // Fetch the webhook configuration so that we can get the secret. const [appId, subscriberId] = job.jobName.split("_"); - const webhook = await prisma.webhook.findUniqueOrThrow({ - where: { id: subscriberId, appId: appId !== "null" ? appId : null }, - }); + + let webhook; + try { + webhook = await prisma.webhook.findUniqueOrThrow({ + where: { id: subscriberId, appId: appId !== "null" ? appId : null }, + }); + } catch { + logger.error(`Error finding webhook for subscriberId: ${subscriberId}, appId: ${appId}`); + } + + const headers: Record = { + "Content-Type": + !job.payload || jsonParse(job.payload) ? "application/json" : "application/x-www-form-urlencoded", + }; + + if (webhook) { + headers["X-Cal-Signature-256"] = createWebhookSignature({ secret: webhook.secret, body: job.payload }); + } + try { await fetch(job.subscriberUrl, { method: "POST", body: job.payload, - headers: { - "Content-Type": - !job.payload || jsonParse(job.payload) ? "application/json" : "application/x-www-form-urlencoded", - "X-Cal-Signature-256": createWebhookSignature({ secret: webhook.secret, body: job.payload }), - }, + headers, }); } catch (error) { console.log(`Error running webhook trigger (retry count: ${job.retryCount}): ${error}`);