Skip to content

Commit b069034

Browse files
authored
fix(polling): mark webhook failed on webhook trigger errors (#2146)
1 parent 3a4f130 commit b069034

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

apps/sim/lib/webhooks/gmail-polling-service.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export async function pollGmailWebhooks() {
205205
const emailsToProcess = emails
206206

207207
// Process emails
208-
const processed = await processEmails(
208+
const { processedCount, failedCount } = await processEmails(
209209
emailsToProcess,
210210
webhookData,
211211
config,
@@ -215,12 +215,23 @@ export async function pollGmailWebhooks() {
215215

216216
// Update webhook with latest history ID and timestamp
217217
await updateWebhookData(webhookId, now.toISOString(), latestHistoryId || config.historyId)
218-
await markWebhookSuccess(webhookId)
219-
successCount++
220218

221-
logger.info(
222-
`[${requestId}] Successfully processed ${processed} emails for webhook ${webhookId}`
223-
)
219+
// If all emails failed, mark webhook as failed. Otherwise mark as success.
220+
if (failedCount > 0 && processedCount === 0) {
221+
// All emails failed to process - mark webhook as failed
222+
await markWebhookFailed(webhookId)
223+
failureCount++
224+
logger.warn(
225+
`[${requestId}] All ${failedCount} emails failed to process for webhook ${webhookId}`
226+
)
227+
} else {
228+
// At least some emails processed successfully
229+
await markWebhookSuccess(webhookId)
230+
successCount++
231+
logger.info(
232+
`[${requestId}] Successfully processed ${processedCount} emails for webhook ${webhookId}${failedCount > 0 ? ` (${failedCount} failed)` : ''}`
233+
)
234+
}
224235
} catch (error) {
225236
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
226237
logger.error(`[${requestId}] Error processing Gmail webhook ${webhookId}:`, error)
@@ -571,6 +582,7 @@ async function processEmails(
571582
requestId: string
572583
) {
573584
let processedCount = 0
585+
let failedCount = 0
574586

575587
for (const email of emails) {
576588
try {
@@ -717,11 +729,12 @@ async function processEmails(
717729
} catch (error) {
718730
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
719731
logger.error(`[${requestId}] Error processing email ${email.id}:`, errorMessage)
732+
failedCount++
720733
// Continue processing other emails even if one fails
721734
}
722735
}
723736

724-
return processedCount
737+
return { processedCount, failedCount }
725738
}
726739

727740
async function markEmailAsRead(accessToken: string, messageId: string) {

apps/sim/lib/webhooks/outlook-polling-service.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ export async function pollOutlookWebhooks() {
248248
logger.info(`[${requestId}] Processing ${emails.length} emails for webhook ${webhookId}`)
249249

250250
// Process emails
251-
const processed = await processOutlookEmails(
251+
const { processedCount, failedCount } = await processOutlookEmails(
252252
emails,
253253
webhookData,
254254
config,
@@ -258,12 +258,23 @@ export async function pollOutlookWebhooks() {
258258

259259
// Update webhook with latest timestamp
260260
await updateWebhookLastChecked(webhookId, now.toISOString())
261-
await markWebhookSuccess(webhookId)
262-
successCount++
263261

264-
logger.info(
265-
`[${requestId}] Successfully processed ${processed} emails for webhook ${webhookId}`
266-
)
262+
// If all emails failed, mark webhook as failed. Otherwise mark as success.
263+
if (failedCount > 0 && processedCount === 0) {
264+
// All emails failed to process - mark webhook as failed
265+
await markWebhookFailed(webhookId)
266+
failureCount++
267+
logger.warn(
268+
`[${requestId}] All ${failedCount} emails failed to process for webhook ${webhookId}`
269+
)
270+
} else {
271+
// At least some emails processed successfully
272+
await markWebhookSuccess(webhookId)
273+
successCount++
274+
logger.info(
275+
`[${requestId}] Successfully processed ${processedCount} emails for webhook ${webhookId}${failedCount > 0 ? ` (${failedCount} failed)` : ''}`
276+
)
277+
}
267278
} catch (error) {
268279
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
269280
logger.error(`[${requestId}] Error processing Outlook webhook ${webhookId}:`, error)
@@ -404,6 +415,7 @@ async function processOutlookEmails(
404415
requestId: string
405416
) {
406417
let processedCount = 0
418+
let failedCount = 0
407419

408420
for (const email of emails) {
409421
try {
@@ -510,10 +522,11 @@ async function processOutlookEmails(
510522
processedCount++
511523
} catch (error) {
512524
logger.error(`[${requestId}] Error processing email ${email.id}:`, error)
525+
failedCount++
513526
}
514527
}
515528

516-
return processedCount
529+
return { processedCount, failedCount }
517530
}
518531

519532
async function downloadOutlookAttachments(

0 commit comments

Comments
 (0)