refactor(core): Introduce @OnLeaderTakeover and @OnLeaderStepdown (#14940)

This commit is contained in:
Iván Ovejero authored and GitHub committed 2025-04-28 10:53:01 +02:00
1 parent 9f2182568a
commit 2d60e469f3
10 files changed
+326 -51

No files matched your search

@@ -1,9 +1,7 @@
import { Container } from '@n8n/di';
import { mock } from 'jest-mock-extended';
import { InstanceSettings } from 'n8n-core';
import type { Logger } from 'n8n-core';
import { OrchestrationService } from '@/services/orchestration.service';
import { mockInstance } from '@test/mocking';
import { InsightsModule } from '../insights.module';
@@ -13,7 +11,6 @@ describe('InsightsModule', () => {
let logger: Logger;
let insightsService: InsightsService;
let instanceSettings: InstanceSettings;
let orchestrationService: OrchestrationService;
beforeEach(() => {
logger = mock<Logger>({
@@ -24,7 +21,6 @@ describe('InsightsModule', () => {
),
});
insightsService = mockInstance(InsightsService);
orchestrationService = Container.get(OrchestrationService);
});
describe('backgroundProcess', () => {
@@ -41,25 +37,5 @@ describe('InsightsModule', () => {
insightsModule.initialize();
expect(insightsService.startBackgroundProcess).not.toHaveBeenCalled();
});
it('should start background process on leader takeover', () => {
instanceSettings = mockInstance(InstanceSettings, { instanceType: 'main', isLeader: false });
const insightsModule = new InsightsModule(logger, insightsService, instanceSettings);
insightsModule.initialize();
expect(insightsService.startBackgroundProcess).not.toHaveBeenCalled();
insightsModule.registerMultiMainListeners(orchestrationService.multiMainSetup);
orchestrationService.multiMainSetup.emit('leader-takeover');
expect(insightsService.startBackgroundProcess).toHaveBeenCalled();
});
it('should stop background process on leader stepdown', () => {
instanceSettings = mockInstance(InstanceSettings, { instanceType: 'main', isLeader: true });
const insightsModule = new InsightsModule(logger, insightsService, instanceSettings);
insightsModule.initialize();
expect(insightsService.stopBackgroundProcess).not.toHaveBeenCalled();
insightsModule.registerMultiMainListeners(orchestrationService.multiMainSetup);
orchestrationService.multiMainSetup.emit('leader-stepdown');
expect(insightsService.stopBackgroundProcess).toHaveBeenCalled();
});
});
});
@@ -1,10 +1,8 @@
import type { BaseN8nModule } from '@n8n/decorators';
import { N8nModule } from '@n8n/decorators';
import { N8nModule, OnLeaderStepdown, OnLeaderTakeover } from '@n8n/decorators';
import type { ExecutionLifecycleHooks } from 'n8n-core';
import { InstanceSettings, Logger } from 'n8n-core';
import type { MultiMainSetup } from '@/scaling/multi-main-setup.ee';
import { InsightsService } from './insights.service';
import './insights.controller';
@@ -35,8 +33,13 @@ export class InsightsModule implements BaseN8nModule {
});
}
registerMultiMainListeners(multiMainSetup: MultiMainSetup) {
multiMainSetup.on('leader-takeover', () => this.insightsService.startBackgroundProcess());
multiMainSetup.on('leader-stepdown', () => this.insightsService.stopBackgroundProcess());
@OnLeaderTakeover()
startBackgroundProcess() {
this.insightsService.startBackgroundProcess();
}
@OnLeaderStepdown()
stopBackgroundProcess() {
this.insightsService.stopBackgroundProcess();
}
}