refactor(core): Decouple leadership-change handlers using decorators (#15009)

This commit is contained in:
Iván Ovejero
2025-04-30 12:59:57 +02:00
committed by GitHub
parent 9541b5bb07
commit 9c0e0f0d2e
16 changed files with 49 additions and 127 deletions
@@ -18,6 +18,7 @@ import type { TypeUnit } from '@/modules/insights/database/entities/insights-sha
import { InsightsMetadataRepository } from '@/modules/insights/database/repositories/insights-metadata.repository';
import { InsightsRawRepository } from '@/modules/insights/database/repositories/insights-raw.repository';
import type { IWorkflowDb } from '@/types-db';
import { mockLogger } from '@test/mocking';
import { createTeamProject } from '@test-integration/db/projects';
import { createWorkflow } from '@test-integration/db/workflows';
import * as testDb from '@test-integration/test-db';
@@ -284,7 +285,7 @@ describe('workflowExecuteAfterHandler - cacheMetadata', () => {
insightsCollectionService = new InsightsCollectionService(
sharedWorkflowRepositoryMock,
Container.get(InsightsConfig),
mock<Logger>(),
mockLogger(),
);
});
@@ -2,7 +2,6 @@ import type { InsightsDateRange } from '@n8n/api-types';
import { Container } from '@n8n/di';
import { mock } from 'jest-mock-extended';
import { DateTime } from 'luxon';
import type { Logger } from 'n8n-core';
import type { Project } from '@/databases/entities/project';
import type { WorkflowEntity } from '@/databases/entities/workflow-entity';
@@ -502,7 +501,6 @@ describe('getAvailableDateRanges', () => {
mock<InsightsCompactionService>(),
mock<InsightsCollectionService>(),
licenseMock,
mock<Logger>(),
);
});
@@ -603,7 +601,6 @@ describe('getMaxAgeInDaysAndGranularity', () => {
mock<InsightsCompactionService>(),
mock<InsightsCollectionService>(),
licenseMock,
mock<Logger>(),
);
});
@@ -77,12 +77,14 @@ export class InsightsCollectionService {
async () => await this.flushEvents(),
this.insightsConfig.flushIntervalSeconds * 1000,
);
this.logger.debug('Started flushing timer');
}
stopFlushingTimer() {
if (this.flushInsightsRawBufferTimer !== undefined) {
clearTimeout(this.flushInsightsRawBufferTimer);
this.flushInsightsRawBufferTimer = undefined;
this.logger.debug('Stopped flushing timer');
}
}
@@ -1,4 +1,5 @@
import { Service } from '@n8n/di';
import { Logger } from 'n8n-core';
import { InsightsByPeriodRepository } from './database/repositories/insights-by-period.repository';
import { InsightsRawRepository } from './database/repositories/insights-raw.repository';
@@ -16,7 +17,10 @@ export class InsightsCompactionService {
private readonly insightsByPeriodRepository: InsightsByPeriodRepository,
private readonly insightsRawRepository: InsightsRawRepository,
private readonly insightsConfig: InsightsConfig,
) {}
private readonly logger: Logger,
) {
this.logger = this.logger.scoped('insights');
}
startCompactionTimer() {
this.stopCompactionTimer();
@@ -24,12 +28,14 @@ export class InsightsCompactionService {
async () => await this.compactInsights(),
this.insightsConfig.compactionIntervalMinutes * 60 * 1000,
);
this.logger.debug('Started compaction timer');
}
stopCompactionTimer() {
if (this.compactInsightsTimer !== undefined) {
clearInterval(this.compactInsightsTimer);
this.compactInsightsTimer = undefined;
this.logger.debug('Stopped compaction timer');
}
}
@@ -5,7 +5,6 @@ import {
} from '@n8n/api-types';
import { OnShutdown } from '@n8n/decorators';
import { Service } from '@n8n/di';
import { Logger } from 'n8n-core';
import { UserError } from 'n8n-workflow';
import { License } from '@/license';
@@ -33,19 +32,16 @@ export class InsightsService {
private readonly compactionService: InsightsCompactionService,
private readonly collectionService: InsightsCollectionService,
private readonly license: License,
private readonly logger: Logger,
) {}
startBackgroundProcess() {
this.compactionService.startCompactionTimer();
this.collectionService.startFlushingTimer();
this.logger.debug('Started compaction and flushing schedulers');
}
stopBackgroundProcess() {
this.compactionService.stopCompactionTimer();
this.collectionService.stopFlushingTimer();
this.logger.debug('Stopped compaction and flushing schedulers');
}
@OnShutdown()