-
-
Notifications
You must be signed in to change notification settings - Fork 685
/
Copy pathdynamicFlushScheduler.server.ts
180 lines (156 loc) · 5.42 KB
/
dynamicFlushScheduler.server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { nanoid } from "nanoid";
import pLimit from "p-limit";
import { Gauge } from "prom-client";
import { metricsRegister } from "~/metrics.server";
import { logger } from "~/services/logger.server";
export type DynamicFlushSchedulerConfig<T> = {
batchSize: number;
flushInterval: number;
maxConcurrency?: number;
callback: (flushId: string, batch: T[]) => Promise<void>;
};
export class DynamicFlushScheduler<T> {
private currentBatch: T[]; // Adjust the type according to your data structure
private readonly BATCH_SIZE: number;
private readonly FLUSH_INTERVAL: number;
private readonly MAX_CONCURRENCY: number;
private readonly concurrencyLimiter: ReturnType<typeof pLimit>;
private flushTimer: NodeJS.Timeout | null;
private readonly callback: (flushId: string, batch: T[]) => Promise<void>;
private isShuttingDown;
private failedBatchCount;
constructor(config: DynamicFlushSchedulerConfig<T>) {
this.currentBatch = [];
this.BATCH_SIZE = config.batchSize;
this.FLUSH_INTERVAL = config.flushInterval;
this.MAX_CONCURRENCY = config.maxConcurrency || 1;
this.concurrencyLimiter = pLimit(this.MAX_CONCURRENCY);
this.flushTimer = null;
this.callback = config.callback;
this.isShuttingDown = false;
this.failedBatchCount = 0;
logger.info("Initializing DynamicFlushScheduler", {
batchSize: this.BATCH_SIZE,
flushInterval: this.FLUSH_INTERVAL,
maxConcurrency: this.MAX_CONCURRENCY,
});
this.startFlushTimer();
this.setupShutdownHandlers();
if (!process.env.VITEST) {
const scheduler = this;
new Gauge({
name: "dynamic_flush_scheduler_batch_size",
help: "Number of items in the current dynamic flush scheduler batch",
collect() {
this.set(scheduler.currentBatch.length);
},
registers: [metricsRegister],
});
new Gauge({
name: "dynamic_flush_scheduler_failed_batches",
help: "Number of failed batches",
collect() {
this.set(scheduler.failedBatchCount);
},
registers: [metricsRegister],
});
}
}
/**
*
* If you want to fire and forget, don't await this method.
*/
async addToBatch(items: T[]): Promise<void> {
// TODO: consider using concat. spread is not performant
this.currentBatch.push(...items);
logger.debug("Adding items to batch", {
currentBatchSize: this.currentBatch.length,
itemsAdded: items.length,
});
if (this.currentBatch.length >= this.BATCH_SIZE) {
logger.debug("Batch size threshold reached, initiating flush", {
batchSize: this.BATCH_SIZE,
currentSize: this.currentBatch.length,
});
await this.flushNextBatch();
this.resetFlushTimer();
}
}
private startFlushTimer(): void {
this.flushTimer = setInterval(() => this.checkAndFlush(), this.FLUSH_INTERVAL);
logger.debug("Started flush timer", { interval: this.FLUSH_INTERVAL });
}
private setupShutdownHandlers() {
process.on("SIGTERM", this.shutdown.bind(this));
process.on("SIGINT", this.shutdown.bind(this));
logger.debug("Shutdown handlers configured");
}
private async shutdown(): Promise<void> {
if (this.isShuttingDown) return;
this.isShuttingDown = true;
logger.info("Initiating shutdown of dynamic flush scheduler", {
remainingItems: this.currentBatch.length,
});
await this.checkAndFlush();
this.clearTimer();
logger.info("Dynamic flush scheduler shutdown complete", {
totalFailedBatches: this.failedBatchCount,
});
}
private clearTimer(): void {
if (this.flushTimer) {
clearInterval(this.flushTimer);
logger.debug("Flush timer cleared");
}
}
private resetFlushTimer(): void {
this.clearTimer();
this.startFlushTimer();
logger.debug("Flush timer reset");
}
private async checkAndFlush(): Promise<void> {
if (this.currentBatch.length > 0) {
logger.debug("Periodic flush check triggered", {
currentBatchSize: this.currentBatch.length,
});
await this.flushNextBatch();
}
}
private async flushNextBatch(): Promise<void> {
if (this.currentBatch.length === 0) return;
const batches: T[][] = [];
while (this.currentBatch.length > 0) {
batches.push(this.currentBatch.splice(0, this.BATCH_SIZE));
}
logger.info("Starting batch flush", {
numberOfBatches: batches.length,
totalItems: batches.reduce((sum, batch) => sum + batch.length, 0),
});
// TODO: report plimit.activeCount and pLimit.pendingCount and pLimit.concurrency to /metrics
const promises = batches.map((batch) =>
this.concurrencyLimiter(async () => {
const batchId = nanoid();
try {
await this.callback(batchId, batch!);
} catch (error) {
logger.error("Error processing batch", {
batchId,
error,
batchSize: batch.length,
errorMessage: error instanceof Error ? error.message : "Unknown error",
});
throw error;
}
})
);
const results = await Promise.allSettled(promises);
const failedBatches = results.filter((result) => result.status === "rejected").length;
this.failedBatchCount += failedBatches;
logger.info("Batch flush complete", {
totalBatches: batches.length,
successfulBatches: batches.length - failedBatches,
failedBatches,
totalFailedBatches: this.failedBatchCount,
});
}
}