1+ import { logError , logInfo } from "./logger.js" ;
2+
13export type SyncJobState =
24 | "pending"
35 | "running"
@@ -77,6 +79,7 @@ export function createSyncQueue({
7779 job . state = state ;
7880 job . finishedAt = stamp ( ) ;
7981 inactive . unshift ( job ) ;
82+ logInfo ( "sync.queue.inactive" , syncJobLogFields ( job ) ) ;
8083 }
8184
8285 async function drain ( ) {
@@ -93,14 +96,23 @@ export function createSyncQueue({
9396 running = job ;
9497 job . state = "running" ;
9598 job . startedAt = stamp ( ) ;
99+ logInfo ( "sync.queue.start" , syncJobLogFields ( job ) ) ;
96100
97101 try {
98102 const result = await execute ( job ) ;
99103 job . state = "succeeded" ;
100104 job . result = result ?? { } ;
105+ logInfo ( "sync.queue.finish" , {
106+ ...syncJobLogFields ( job ) ,
107+ ...syncJobResultLogFields ( job . result ) ,
108+ } ) ;
101109 } catch ( error ) {
102110 job . state = "failed" ;
103111 job . error = error instanceof Error ? error . message : String ( error ) ;
112+ logError ( "sync.queue.error" , {
113+ ...syncJobLogFields ( job ) ,
114+ error : job . error ,
115+ } ) ;
104116 } finally {
105117 job . finishedAt = stamp ( ) ;
106118 running = undefined ;
@@ -146,6 +158,11 @@ export function createSyncQueue({
146158 schedule ( request ) {
147159 const duplicate = findDuplicateAutomatic ( request ) ;
148160 if ( duplicate ) {
161+ logInfo ( "sync.queue.coalesce" , {
162+ ...syncJobLogFields ( duplicate ) ,
163+ requestedOrigin : request . origin ,
164+ requestedScope : syncScopeLabel ( request . scope ) ,
165+ } ) ;
149166 return duplicate ;
150167 }
151168
@@ -177,6 +194,7 @@ export function createSyncQueue({
177194 }
178195
179196 pending . push ( job ) ;
197+ logInfo ( "sync.queue.schedule" , syncJobLogFields ( job ) ) ;
180198 startDrain ( ) ;
181199
182200 return job ;
@@ -211,6 +229,40 @@ function sameScope(left: SyncScope, right: SyncScope) {
211229 return "days" in left && "days" in right && left . days === right . days ;
212230}
213231
232+ function syncJobLogFields ( job : SyncJob ) {
233+ return {
234+ jobId : job . id ,
235+ accountId : job . accountId ,
236+ origin : job . origin ,
237+ scope : syncScopeLabel ( job . scope ) ,
238+ state : job . state ,
239+ } ;
240+ }
241+
242+ function syncJobResultLogFields ( result : SyncJobResult | undefined ) {
243+ if ( ! result ) {
244+ return { } ;
245+ }
246+
247+ return {
248+ mailboxCount : result . mailboxCount ,
249+ scannedMailboxCount : result . scannedMailboxCount ,
250+ skippedMailboxCount : result . skippedMailboxCount ,
251+ fetchedMessageCount : result . fetchedMessageCount ,
252+ storedMessageCount : result . storedMessageCount ,
253+ removedMailboxEntryCount : result . removedMailboxEntryCount ,
254+ durationMs : result . durationMs ,
255+ } ;
256+ }
257+
258+ function syncScopeLabel ( scope : SyncScope ) : string {
259+ if ( scope . type === "regular" ) {
260+ return "regular" ;
261+ }
262+
263+ return `${ scope . type } :${ scope . days } ` ;
264+ }
265+
214266function isSmallerCustomRange ( left : SyncScope , right : Extract < SyncScope , { type : "customRange" } > ) {
215267 return left . type === "customRange" && left . days < right . days ;
216268}
0 commit comments