chore(core): Refactor logging for insights flushing (#15978)

This commit is contained in:
Andreas Fitzek
2025-06-03 16:49:55 +02:00
committed by GitHub
parent 4e2229752b
commit b87d04e33b

View File

@@ -75,22 +75,30 @@ export class InsightsCollectionService {
startFlushingTimer() {
this.isAsynchronouslySavingInsights = true;
this.stopFlushingTimer();
this.scheduleFlushing();
this.logger.debug('Started flushing timer');
}
scheduleFlushing() {
this.cancelScheduledFlushing();
this.flushInsightsRawBufferTimer = setTimeout(
async () => await this.flushEvents(),
this.insightsConfig.flushIntervalSeconds * 1000,
);
this.logger.debug('Started flushing timer');
}
stopFlushingTimer() {
cancelScheduledFlushing() {
if (this.flushInsightsRawBufferTimer !== undefined) {
clearTimeout(this.flushInsightsRawBufferTimer);
this.flushInsightsRawBufferTimer = undefined;
this.logger.debug('Stopped flushing timer');
}
}
stopFlushingTimer() {
this.cancelScheduledFlushing();
this.logger.debug('Stopped flushing timer');
}
async shutdown() {
this.stopFlushingTimer();
@@ -228,12 +236,12 @@ export class InsightsCollectionService {
// Prevent flushing if there are no events to flush
if (this.bufferedInsights.size === 0) {
// reschedule the timer to flush again
this.startFlushingTimer();
this.scheduleFlushing();
return;
}
// Stop timer to prevent concurrent flush from timer
this.stopFlushingTimer();
this.cancelScheduledFlushing();
// Copy the buffer to a new set to avoid concurrent modification
// while we are flushing the events
@@ -250,7 +258,7 @@ export class InsightsCollectionService {
this.bufferedInsights.add(event);
}
} finally {
this.startFlushingTimer();
this.scheduleFlushing();
this.flushesInProgress.delete(flushPromise!);
}
})();