From b68a50fea3b9693be63d679e570d36ad5b59d148 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sun, 1 Sep 2024 13:52:34 -0300 Subject: [PATCH] Nick: --- apps/api/src/services/queue-worker.ts | 22 +++-------- apps/api/src/services/webhook.ts | 55 +++++++++++++++++---------- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/apps/api/src/services/queue-worker.ts b/apps/api/src/services/queue-worker.ts index fbf7293a..d3a34e96 100644 --- a/apps/api/src/services/queue-worker.ts +++ b/apps/api/src/services/queue-worker.ts @@ -221,12 +221,10 @@ async function processJob(job: Job, token: string) { // No idea what this does and when it is called. if (job.data.mode === "crawl" && !job.data.v1) { - callWebhook(job.data.team_id, job.id as string, data, job.data.webhook, job.data.v1).catch((error) => { - Logger.error(`Error calling webhook for job (1 - mode crawl - v0) ${job.id} - ${error}`); - }); + callWebhook(job.data.team_id, job.id as string, data, job.data.webhook, job.data.v1); } if (job.data.webhook && job.data.mode !== "crawl" && job.data.v1) { - await callWebhook(job.data.team_id, job.data.crawl_id, data, job.data.webhook, job.data.v1); + callWebhook(job.data.team_id, job.data.crawl_id, data, job.data.webhook, job.data.v1); } if (job.data.crawl_id) { @@ -351,15 +349,11 @@ async function processJob(job: Job, token: string) { }; // v0 web hooks, call when done with all the data if (!job.data.v1) { - callWebhook(job.data.team_id, job.data.crawl_id, data, job.data.webhook, job.data.v1, "crawl.completed").catch((error) => { - Logger.error(`Error calling webhook for job ${job.id} - ${error}`); - }); + callWebhook(job.data.team_id, job.data.crawl_id, data, job.data.webhook, job.data.v1, "crawl.completed"); } // v1 web hooks, call when done with no data, but with event completed if (job.data.v1 && job.data.webhook) { - callWebhook(job.data.team_id, job.data.crawl_id, [], job.data.webhook, job.data.v1, "crawl.completed").catch((error) => { - Logger.error(`Error calling webhook for job ${job.id} - ${error}`); - }); + callWebhook(job.data.team_id, job.data.crawl_id, [], job.data.webhook, job.data.v1, "crawl.completed"); } } } @@ -407,14 +401,10 @@ async function processJob(job: Job, token: string) { }; if (!job.data.v1 && (job.data.mode === "crawl" || job.data.crawl_id)) { - callWebhook(job.data.team_id, job.data.crawl_id ?? job.id as string, data, job.data.webhook, job.data.v1).catch((error) => { - Logger.error(`Error calling webhook for job (catch - v0) ${job.id} - ${error}`); - }); + callWebhook(job.data.team_id, job.data.crawl_id ?? job.id as string, data, job.data.webhook, job.data.v1); } if(job.data.v1) { - callWebhook(job.data.team_id, job.id as string, [], job.data.webhook, job.data.v1, "crawl.failed").catch((error) => { - Logger.error(`Error calling webhook for job (catch - v1) ${job.id} - ${error}`); - }); + callWebhook(job.data.team_id, job.id as string, [], job.data.webhook, job.data.v1, "crawl.failed"); } if (job.data.crawl_id) { diff --git a/apps/api/src/services/webhook.ts b/apps/api/src/services/webhook.ts index 1ca7dbad..e088129a 100644 --- a/apps/api/src/services/webhook.ts +++ b/apps/api/src/services/webhook.ts @@ -43,7 +43,12 @@ export const callWebhook = async ( } let dataToSend = []; - if (data && data.result && data.result.links && data.result.links.length !== 0) { + if ( + data && + data.result && + data.result.links && + data.result.links.length !== 0 + ) { for (let i = 0; i < data.result.links.length; i++) { if (v1) { dataToSend.push( @@ -59,26 +64,36 @@ export const callWebhook = async ( } } - axios.post( - webhookUrl, - { - success: !v1 ? data.success : eventType === "crawl.page" ? data.success : true, - type: eventType, - [v1 ? 'id' : 'jobId']: id, - data: dataToSend, - error: !v1 ? data?.error || undefined : eventType === "crawl.page" ? data?.error || undefined : undefined, - }, - { - headers: { - "Content-Type": "application/json", + axios + .post( + webhookUrl, + { + success: !v1 + ? data.success + : eventType === "crawl.page" + ? data.success + : true, + type: eventType, + [v1 ? "id" : "jobId"]: id, + data: dataToSend, + error: !v1 + ? data?.error || undefined + : eventType === "crawl.page" + ? data?.error || undefined + : undefined, }, - timeout: 10000, // 10 seconds timeout - } - ).catch((error) => { - Logger.error( - `Error sending webhook for team ID: ${teamId}, error: ${error.message}` - ); - }); + { + headers: { + "Content-Type": "application/json", + }, + timeout: 10000, // 10 seconds timeout + } + ) + .catch((error) => { + Logger.error( + `Error sending webhook for team ID: ${teamId}, error: ${error.message}` + ); + }); } catch (error) { Logger.debug( `Error sending webhook for team ID: ${teamId}, error: ${error.message}`