Skip to content

Commit 4bb1e1d

Browse files
committed
add logging
1 parent 6944cda commit 4bb1e1d

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

apps/webapp/app/v3/dynamicFlushScheduler.server.ts

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ export class DynamicFlushScheduler<T> {
3232
this.callback = config.callback;
3333
this.isShuttingDown = false;
3434
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+
3542
this.startFlushTimer();
3643
this.setupShutdownHandlers();
3744

@@ -60,49 +67,64 @@ export class DynamicFlushScheduler<T> {
6067
async addToBatch(items: T[]): Promise<void> {
6168
this.currentBatch.push(...items);
6269
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,
6572
});
6673

6774
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+
});
6879
await this.flushNextBatch();
6980
this.resetFlushTimer();
7081
}
7182
}
7283

7384
private startFlushTimer(): void {
7485
this.flushTimer = setInterval(() => this.checkAndFlush(), this.FLUSH_INTERVAL);
86+
logger.debug("Started flush timer", { interval: this.FLUSH_INTERVAL });
7587
}
7688

7789
private setupShutdownHandlers() {
7890
process.on("SIGTERM", this.shutdown.bind(this));
7991
process.on("SIGINT", this.shutdown.bind(this));
92+
logger.debug("Shutdown handlers configured");
8093
}
8194

8295
private async shutdown(): Promise<void> {
8396
if (this.isShuttingDown) return;
8497
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+
});
86101

87102
await this.checkAndFlush();
88103
this.clearTimer();
89104

90-
logger.log("All items have been flushed.");
105+
logger.info("Dynamic flush scheduler shutdown complete", {
106+
totalFailedBatches: this.failedBatchCount,
107+
});
91108
}
92109

93110
private clearTimer(): void {
94111
if (this.flushTimer) {
95112
clearInterval(this.flushTimer);
113+
logger.debug("Flush timer cleared");
96114
}
97115
}
98116

99117
private resetFlushTimer(): void {
100118
this.clearTimer();
101119
this.startFlushTimer();
120+
logger.debug("Flush timer reset");
102121
}
103122

104123
private async checkAndFlush(): Promise<void> {
105124
if (this.currentBatch.length > 0) {
125+
logger.debug("Periodic flush check triggered", {
126+
currentBatchSize: this.currentBatch.length,
127+
});
106128
await this.flushNextBatch();
107129
}
108130
}
@@ -115,15 +137,26 @@ export class DynamicFlushScheduler<T> {
115137
batches.push(this.currentBatch.splice(0, this.BATCH_SIZE));
116138
}
117139

140+
logger.info("Starting batch flush", {
141+
numberOfBatches: batches.length,
142+
totalItems: batches.reduce((sum, batch) => sum + batch.length, 0),
143+
});
144+
118145
const promises = batches.map((batch) =>
119146
this.concurrencyLimiter(async () => {
120147
const batchId = nanoid();
121148
try {
149+
logger.debug("Processing batch", {
150+
batchId,
151+
batchSize: batch.length,
152+
});
122153
await this.callback(batchId, batch!);
123154
} catch (error) {
124-
logger.error("Error inserting batch:", {
155+
logger.error("Error processing batch", {
125156
batchId,
126157
error,
158+
batchSize: batch.length,
159+
errorMessage: error instanceof Error ? error.message : "Unknown error",
127160
});
128161
throw error;
129162
}
@@ -134,5 +167,12 @@ export class DynamicFlushScheduler<T> {
134167

135168
const failedBatches = results.filter((result) => result.status === "rejected").length;
136169
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+
});
137177
}
138178
}

0 commit comments

Comments
 (0)