Skip to content

Commit 742a1e6

Browse files
Start insights collection timer for webhook instances
1 parent e76c45d commit 742a1e6

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed

packages/cli/src/modules/insights/__tests__/insights.module.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ describe('InsightsModule', () => {
2121
instanceSettings = mockInstance(InstanceSettings, { instanceType: 'main', isLeader: true });
2222
const insightsModule = new InsightsModule(logger, insightsService, instanceSettings);
2323
insightsModule.initialize();
24-
expect(insightsService.startTimers).toHaveBeenCalled();
24+
expect(insightsService.startTimers).toHaveBeenCalledWith();
25+
});
26+
27+
it('should start background process if instance is webhook', () => {
28+
instanceSettings = mockInstance(InstanceSettings, {
29+
instanceType: 'webhook',
30+
isLeader: false,
31+
});
32+
const insightsModule = new InsightsModule(logger, insightsService, instanceSettings);
33+
insightsModule.initialize();
34+
expect(insightsService.startTimers).toHaveBeenCalledWith({ onlyCollection: true });
2535
});
2636

2737
it('should not start background process if instance is main but not leader', () => {

packages/cli/src/modules/insights/__tests__/insights.service.test.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import type { InsightsRaw } from '../database/entities/insights-raw';
2323
import type { InsightsByPeriodRepository } from '../database/repositories/insights-by-period.repository';
2424
import { InsightsCollectionService } from '../insights-collection.service';
2525
import { InsightsCompactionService } from '../insights-compaction.service';
26-
import type { InsightsPruningService } from '../insights-pruning.service';
26+
import { InsightsPruningService } from '../insights-pruning.service';
2727
import { InsightsConfig } from '../insights.config';
2828
import { InsightsService } from '../insights.service';
2929

@@ -47,6 +47,47 @@ afterAll(async () => {
4747
await testDb.terminate();
4848
});
4949

50+
describe.only('startTimers', () => {
51+
let insightsService: InsightsService;
52+
let compactionService: InsightsCompactionService;
53+
let collectionService: InsightsCollectionService;
54+
let pruningService: InsightsPruningService;
55+
56+
beforeAll(() => {
57+
compactionService = mock<InsightsCompactionService>();
58+
collectionService = mock<InsightsCollectionService>();
59+
pruningService = mock<InsightsPruningService>({ isPruningEnabled: true });
60+
insightsService = new InsightsService(
61+
mock<InsightsByPeriodRepository>(),
62+
compactionService,
63+
collectionService,
64+
pruningService,
65+
mock<LicenseState>(),
66+
mockLogger(),
67+
);
68+
});
69+
70+
beforeEach(() => {
71+
jest.clearAllMocks();
72+
});
73+
74+
test('starts compaction, flushing and pruning timers', () => {
75+
insightsService.startTimers();
76+
77+
expect(collectionService.startFlushingTimer).toHaveBeenCalled();
78+
expect(compactionService.startCompactionTimer).toHaveBeenCalled();
79+
expect(pruningService.startPruningTimer).toHaveBeenCalled();
80+
});
81+
82+
test('starts only collection flushing timer when onlyCollection is true', () => {
83+
insightsService.startTimers({ onlyCollection: true });
84+
85+
expect(collectionService.startFlushingTimer).toHaveBeenCalled();
86+
expect(compactionService.startCompactionTimer).not.toHaveBeenCalled();
87+
expect(pruningService.startPruningTimer).not.toHaveBeenCalled();
88+
});
89+
});
90+
5091
describe('getInsightsSummary', () => {
5192
let insightsService: InsightsService;
5293
beforeAll(async () => {

packages/cli/src/modules/insights/insights.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ export class InsightsModule implements BaseN8nModule {
1919

2020
initialize() {
2121
// We want to initialize the insights background process (schedulers) for the main leader instance
22-
// to have only one main instance saving the insights data
2322
if (this.instanceSettings.isLeader) {
2423
this.insightsService.startTimers();
24+
} else if (this.instanceSettings.instanceType === 'webhook') {
25+
// Webhook instances collect insights data independently
26+
// So we only start the collection timers, compaction and pruning are done by the main instance
27+
this.insightsService.startTimers({ onlyCollection: true });
2528
}
2629
}
2730

packages/cli/src/modules/insights/insights.service.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,19 @@ export class InsightsService {
3838
this.logger = this.logger.scoped('insights');
3939
}
4040

41-
startTimers() {
42-
this.compactionService.startCompactionTimer();
41+
startTimers({ onlyCollection } = { onlyCollection: false }) {
4342
this.collectionService.startFlushingTimer();
44-
if (this.pruningService.isPruningEnabled) {
45-
this.pruningService.startPruningTimer();
43+
if (!onlyCollection) {
44+
this.compactionService.startCompactionTimer();
45+
if (this.pruningService.isPruningEnabled) {
46+
this.pruningService.startPruningTimer();
47+
}
4648
}
47-
this.logger.debug('Started compaction, flushing and pruning schedulers');
49+
this.logger.debug(
50+
onlyCollection
51+
? 'Starting collection flushing schedulers'
52+
: 'Starting compaction, flushing and pruning schedulers',
53+
);
4854
}
4955

5056
stopTimers() {

0 commit comments

Comments
 (0)