@@ -32,6 +32,13 @@ export class DynamicFlushScheduler<T> {
32
32
this . callback = config . callback ;
33
33
this . isShuttingDown = false ;
34
34
this . failedBatchCount = 0 ;
35
+
36
+ logger . info ( "Initializing DynamicFlushScheduler" , {
37
+ batchSize : this . BATCH_SIZE ,
38
+ flushInterval : this . FLUSH_INTERVAL ,
39
+ maxConcurrency : this . MAX_CONCURRENCY ,
40
+ } ) ;
41
+
35
42
this . startFlushTimer ( ) ;
36
43
this . setupShutdownHandlers ( ) ;
37
44
@@ -60,49 +67,64 @@ export class DynamicFlushScheduler<T> {
60
67
async addToBatch ( items : T [ ] ) : Promise < void > {
61
68
this . currentBatch . push ( ...items ) ;
62
69
logger . debug ( "Adding items to batch" , {
63
- batchSize : this . BATCH_SIZE ,
64
- newSize : this . currentBatch . length ,
70
+ currentBatchSize : this . currentBatch . length ,
71
+ itemsAdded : items . length ,
65
72
} ) ;
66
73
67
74
if ( this . currentBatch . length >= this . BATCH_SIZE ) {
75
+ logger . debug ( "Batch size threshold reached, initiating flush" , {
76
+ batchSize : this . BATCH_SIZE ,
77
+ currentSize : this . currentBatch . length ,
78
+ } ) ;
68
79
await this . flushNextBatch ( ) ;
69
80
this . resetFlushTimer ( ) ;
70
81
}
71
82
}
72
83
73
84
private startFlushTimer ( ) : void {
74
85
this . flushTimer = setInterval ( ( ) => this . checkAndFlush ( ) , this . FLUSH_INTERVAL ) ;
86
+ logger . debug ( "Started flush timer" , { interval : this . FLUSH_INTERVAL } ) ;
75
87
}
76
88
77
89
private setupShutdownHandlers ( ) {
78
90
process . on ( "SIGTERM" , this . shutdown . bind ( this ) ) ;
79
91
process . on ( "SIGINT" , this . shutdown . bind ( this ) ) ;
92
+ logger . debug ( "Shutdown handlers configured" ) ;
80
93
}
81
94
82
95
private async shutdown ( ) : Promise < void > {
83
96
if ( this . isShuttingDown ) return ;
84
97
this . isShuttingDown = true ;
85
- logger . log ( "Shutting down dynamic flush scheduler..." ) ;
98
+ logger . info ( "Initiating shutdown of dynamic flush scheduler" , {
99
+ remainingItems : this . currentBatch . length ,
100
+ } ) ;
86
101
87
102
await this . checkAndFlush ( ) ;
88
103
this . clearTimer ( ) ;
89
104
90
- logger . log ( "All items have been flushed." ) ;
105
+ logger . info ( "Dynamic flush scheduler shutdown complete" , {
106
+ totalFailedBatches : this . failedBatchCount ,
107
+ } ) ;
91
108
}
92
109
93
110
private clearTimer ( ) : void {
94
111
if ( this . flushTimer ) {
95
112
clearInterval ( this . flushTimer ) ;
113
+ logger . debug ( "Flush timer cleared" ) ;
96
114
}
97
115
}
98
116
99
117
private resetFlushTimer ( ) : void {
100
118
this . clearTimer ( ) ;
101
119
this . startFlushTimer ( ) ;
120
+ logger . debug ( "Flush timer reset" ) ;
102
121
}
103
122
104
123
private async checkAndFlush ( ) : Promise < void > {
105
124
if ( this . currentBatch . length > 0 ) {
125
+ logger . debug ( "Periodic flush check triggered" , {
126
+ currentBatchSize : this . currentBatch . length ,
127
+ } ) ;
106
128
await this . flushNextBatch ( ) ;
107
129
}
108
130
}
@@ -115,15 +137,26 @@ export class DynamicFlushScheduler<T> {
115
137
batches . push ( this . currentBatch . splice ( 0 , this . BATCH_SIZE ) ) ;
116
138
}
117
139
140
+ logger . info ( "Starting batch flush" , {
141
+ numberOfBatches : batches . length ,
142
+ totalItems : batches . reduce ( ( sum , batch ) => sum + batch . length , 0 ) ,
143
+ } ) ;
144
+
118
145
const promises = batches . map ( ( batch ) =>
119
146
this . concurrencyLimiter ( async ( ) => {
120
147
const batchId = nanoid ( ) ;
121
148
try {
149
+ logger . debug ( "Processing batch" , {
150
+ batchId,
151
+ batchSize : batch . length ,
152
+ } ) ;
122
153
await this . callback ( batchId , batch ! ) ;
123
154
} catch ( error ) {
124
- logger . error ( "Error inserting batch: " , {
155
+ logger . error ( "Error processing batch" , {
125
156
batchId,
126
157
error,
158
+ batchSize : batch . length ,
159
+ errorMessage : error instanceof Error ? error . message : "Unknown error" ,
127
160
} ) ;
128
161
throw error ;
129
162
}
@@ -134,5 +167,12 @@ export class DynamicFlushScheduler<T> {
134
167
135
168
const failedBatches = results . filter ( ( result ) => result . status === "rejected" ) . length ;
136
169
this . failedBatchCount += failedBatches ;
170
+
171
+ logger . info ( "Batch flush complete" , {
172
+ totalBatches : batches . length ,
173
+ successfulBatches : batches . length - failedBatches ,
174
+ failedBatches,
175
+ totalFailedBatches : this . failedBatchCount ,
176
+ } ) ;
137
177
}
138
178
}
0 commit comments