Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions apps/sim/lib/webhooks/gmail-polling-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export async function pollGmailWebhooks() {
const emailsToProcess = emails

// Process emails
const processed = await processEmails(
const { processedCount, failedCount } = await processEmails(
emailsToProcess,
webhookData,
config,
Expand All @@ -215,12 +215,23 @@ export async function pollGmailWebhooks() {

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

logger.info(
`[${requestId}] Successfully processed ${processed} emails for webhook ${webhookId}`
)
// If all emails failed, mark webhook as failed. Otherwise mark as success.
if (failedCount > 0 && processedCount === 0) {
// All emails failed to process - mark webhook as failed
await markWebhookFailed(webhookId)
failureCount++
logger.warn(
`[${requestId}] All ${failedCount} emails failed to process for webhook ${webhookId}`
)
} else {
// At least some emails processed successfully
await markWebhookSuccess(webhookId)
successCount++
logger.info(
`[${requestId}] Successfully processed ${processedCount} emails for webhook ${webhookId}${failedCount > 0 ? ` (${failedCount} failed)` : ''}`
)
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
logger.error(`[${requestId}] Error processing Gmail webhook ${webhookId}:`, error)
Expand Down Expand Up @@ -571,6 +582,7 @@ async function processEmails(
requestId: string
) {
let processedCount = 0
let failedCount = 0

for (const email of emails) {
try {
Expand Down Expand Up @@ -717,11 +729,12 @@ async function processEmails(
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
logger.error(`[${requestId}] Error processing email ${email.id}:`, errorMessage)
failedCount++
// Continue processing other emails even if one fails
}
}

return processedCount
return { processedCount, failedCount }
}

async function markEmailAsRead(accessToken: string, messageId: string) {
Expand Down
27 changes: 20 additions & 7 deletions apps/sim/lib/webhooks/outlook-polling-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export async function pollOutlookWebhooks() {
logger.info(`[${requestId}] Processing ${emails.length} emails for webhook ${webhookId}`)

// Process emails
const processed = await processOutlookEmails(
const { processedCount, failedCount } = await processOutlookEmails(
emails,
webhookData,
config,
Expand All @@ -258,12 +258,23 @@ export async function pollOutlookWebhooks() {

// Update webhook with latest timestamp
await updateWebhookLastChecked(webhookId, now.toISOString())
await markWebhookSuccess(webhookId)
successCount++

logger.info(
`[${requestId}] Successfully processed ${processed} emails for webhook ${webhookId}`
)
// If all emails failed, mark webhook as failed. Otherwise mark as success.
if (failedCount > 0 && processedCount === 0) {
// All emails failed to process - mark webhook as failed
await markWebhookFailed(webhookId)
failureCount++
logger.warn(
`[${requestId}] All ${failedCount} emails failed to process for webhook ${webhookId}`
)
} else {
// At least some emails processed successfully
await markWebhookSuccess(webhookId)
successCount++
logger.info(
`[${requestId}] Successfully processed ${processedCount} emails for webhook ${webhookId}${failedCount > 0 ? ` (${failedCount} failed)` : ''}`
)
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
logger.error(`[${requestId}] Error processing Outlook webhook ${webhookId}:`, error)
Expand Down Expand Up @@ -404,6 +415,7 @@ async function processOutlookEmails(
requestId: string
) {
let processedCount = 0
let failedCount = 0

for (const email of emails) {
try {
Expand Down Expand Up @@ -510,10 +522,11 @@ async function processOutlookEmails(
processedCount++
} catch (error) {
logger.error(`[${requestId}] Error processing email ${email.id}:`, error)
failedCount++
}
}

return processedCount
return { processedCount, failedCount }
}

async function downloadOutlookAttachments(
Expand Down