From 726c77e346aba4040babfabdada762f7bef09db3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Padez?= Date: Thu, 5 Mar 2026 05:00:18 +0000 Subject: [PATCH] show live email count during gmail sync instead of mbsync log lines Co-Authored-By: Claude Opus 4.6 --- src/servers/queue/handlers/gmail-sync.ts | 36 ++++++++++++++++++------ 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/servers/queue/handlers/gmail-sync.ts b/src/servers/queue/handlers/gmail-sync.ts index bed17d2d..e6e18a7f 100644 --- a/src/servers/queue/handlers/gmail-sync.ts +++ b/src/servers/queue/handlers/gmail-sync.ts @@ -246,8 +246,23 @@ const gmailSyncHandler: JobHandler = { throw new PermanentError(`Failed to start mbsync: ${msg}`); } - // Stream stderr for live progress - let lastLine = ''; + // Periodically count downloaded emails and update progress + let emailCount = 0; + let counting = true; + const countLoop = (async () => { + while (counting) { + await new Promise((r) => setTimeout(r, 3000)); + if (!counting) break; + emailCount = await countMaildirFiles(maildirPath); + ctx.updateProgress({ + current: emailCount, + total: 0, + label: `Downloading — ${emailCount.toLocaleString()} emails`, + }); + } + })(); + + // Stream stderr for logging let stderrBuf = ''; const reader = (proc.stderr as ReadableStream).getReader(); const decoder = new TextDecoder(); @@ -260,18 +275,15 @@ const gmailSyncHandler: JobHandler = { const lines = chunk.split('\n'); for (const line of lines) { const trimmed = line.trim(); - if (!trimmed) continue; - lastLine = trimmed; - console.log(`[gmail-sync] mbsync: ${trimmed}`); - } - if (lastLine) { - ctx.updateProgress({ current: 0, total: 0, label: `mbsync: ${lastLine.slice(0, 80)}` }); + if (trimmed) console.log(`[gmail-sync] mbsync: ${trimmed}`); } } })(); const exitCode = await proc.exited; + counting = false; await readLoop; + await countLoop; if (exitCode !== 0) { const isOverquota = stderrBuf.includes('OVERQUOTA'); @@ -296,7 +308,13 @@ const gmailSyncHandler: JobHandler = { throw new Error(`mbsync exited with code ${exitCode}: ${stderrBuf.slice(-500)}`); } } else { - console.log('[gmail-sync] mbsync completed successfully'); + emailCount = await countMaildirFiles(maildirPath); + console.log(`[gmail-sync] mbsync completed successfully — ${emailCount} emails`); + await ctx.updateProgress({ + current: emailCount, + total: emailCount, + label: `Download complete — ${emailCount.toLocaleString()} emails`, + }); } } finally { // Always clean up config (contains password)