@@ -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
727740async function markEmailAsRead ( accessToken : string , messageId : string ) {
0 commit comments