-
Notifications
You must be signed in to change notification settings - Fork 57.6k
feat(core): Add cluster check interface and decorator (no-changelog) #28724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
packages/@n8n/decorators/src/cluster-check/__tests__/cluster-check-metadata.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import { Container, Service } from '@n8n/di'; | ||
|
|
||
| import type { ClusterCheckContext, ClusterCheckResult, IClusterCheck } from '../cluster-check'; | ||
| import { ClusterCheck, ClusterCheckMetadata } from '../cluster-check-metadata'; | ||
|
|
||
| describe('ClusterCheckMetadata', () => { | ||
| let metadata: ClusterCheckMetadata; | ||
|
|
||
| beforeEach(() => { | ||
| metadata = new ClusterCheckMetadata(); | ||
| }); | ||
|
|
||
| it('should register check classes', () => { | ||
| class TestCheck implements IClusterCheck { | ||
| checkDescription = { name: 'test.check' }; | ||
| async run(_context: ClusterCheckContext): Promise<ClusterCheckResult> { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| metadata.register({ class: TestCheck }); | ||
|
|
||
| expect(metadata.getClasses()).toContain(TestCheck); | ||
| }); | ||
|
|
||
| it('should return registered classes in registration order', () => { | ||
| class FirstCheck implements IClusterCheck { | ||
| checkDescription = { name: 'first.check' }; | ||
| async run(_context: ClusterCheckContext): Promise<ClusterCheckResult> { | ||
| return {}; | ||
| } | ||
| } | ||
| class SecondCheck implements IClusterCheck { | ||
| checkDescription = { name: 'second.check' }; | ||
| async run(_context: ClusterCheckContext): Promise<ClusterCheckResult> { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| metadata.register({ class: FirstCheck }); | ||
| metadata.register({ class: SecondCheck }); | ||
|
|
||
| expect(metadata.getClasses()).toEqual([FirstCheck, SecondCheck]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('@ClusterCheck decorator', () => { | ||
| let metadata: ClusterCheckMetadata; | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetAllMocks(); | ||
|
|
||
| metadata = new ClusterCheckMetadata(); | ||
| Container.set(ClusterCheckMetadata, metadata); | ||
| }); | ||
|
|
||
| it('should register the decorated class in ClusterCheckMetadata', () => { | ||
| @ClusterCheck() | ||
| class TestCheck implements IClusterCheck { | ||
| checkDescription = { name: 'cluster.test' }; | ||
| async run(_context: ClusterCheckContext): Promise<ClusterCheckResult> { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| const registered = metadata.getClasses(); | ||
|
|
||
| expect(registered).toContain(TestCheck); | ||
| expect(registered).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('should register multiple decorated classes', () => { | ||
| @ClusterCheck() | ||
| class VersionCheck implements IClusterCheck { | ||
| checkDescription = { name: 'cluster.versionMismatch' }; | ||
| async run(_context: ClusterCheckContext): Promise<ClusterCheckResult> { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| @ClusterCheck() | ||
| class LeaderCheck implements IClusterCheck { | ||
| checkDescription = { name: 'cluster.leaderMissing' }; | ||
| async run(_context: ClusterCheckContext): Promise<ClusterCheckResult> { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| const registered = metadata.getClasses(); | ||
|
|
||
| expect(registered).toContain(VersionCheck); | ||
| expect(registered).toContain(LeaderCheck); | ||
| expect(registered).toHaveLength(2); | ||
| }); | ||
|
|
||
| it('should apply @Service() so the class is resolvable via DI', () => { | ||
| @ClusterCheck() | ||
| class TestCheck implements IClusterCheck { | ||
| checkDescription = { name: 'cluster.test' }; | ||
| async run(_context: ClusterCheckContext): Promise<ClusterCheckResult> { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| expect(Container.has(TestCheck)).toBe(true); | ||
|
|
||
| const instance = Container.get(TestCheck); | ||
|
|
||
| expect(instance).toBeInstanceOf(TestCheck); | ||
| expect(instance.checkDescription).toEqual({ name: 'cluster.test' }); | ||
| }); | ||
|
|
||
| it('should support checks with constructor-injected dependencies', () => { | ||
| @Service() | ||
| class Logger { | ||
| log(message: string) { | ||
| return message; | ||
| } | ||
| } | ||
|
|
||
| @ClusterCheck() | ||
| class TestCheck implements IClusterCheck { | ||
| checkDescription = { name: 'cluster.test' }; | ||
| constructor(private readonly logger: Logger) {} | ||
| async run(_context: ClusterCheckContext): Promise<ClusterCheckResult> { | ||
| this.logger.log('run'); | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| const instance = Container.get(TestCheck); | ||
|
|
||
| expect(instance).toBeInstanceOf(TestCheck); | ||
| }); | ||
|
|
||
| it('should expose different description names for different checks', () => { | ||
| @ClusterCheck() | ||
| class VersionCheck implements IClusterCheck { | ||
| checkDescription = { name: 'cluster.versionMismatch', displayName: 'Version Mismatch' }; | ||
| async run(_context: ClusterCheckContext): Promise<ClusterCheckResult> { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| @ClusterCheck() | ||
| class LeaderCheck implements IClusterCheck { | ||
| checkDescription = { name: 'cluster.leaderMissing', displayName: 'Leader Missing' }; | ||
| async run(_context: ClusterCheckContext): Promise<ClusterCheckResult> { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| const versionInstance = Container.get(VersionCheck); | ||
| const leaderInstance = Container.get(LeaderCheck); | ||
|
|
||
| expect(versionInstance.checkDescription.name).toBe('cluster.versionMismatch'); | ||
| expect(leaderInstance.checkDescription.name).toBe('cluster.leaderMissing'); | ||
| }); | ||
| }); |
143 changes: 143 additions & 0 deletions
143
packages/@n8n/decorators/src/cluster-check/cluster-check-metadata.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import { Container, Service } from '@n8n/di'; | ||
|
|
||
| import { ClusterCheckClass } from './cluster-check'; | ||
|
|
||
| /** | ||
| * Registry entry for a cluster check. | ||
| * | ||
| * Lightweight wrapper around the check class constructor, modelled on | ||
| * `ContextEstablishmentHookEntry`. Kept as a wrapper (rather than storing the | ||
| * class directly) so the entry shape can grow — e.g. module source, feature | ||
| * flag, license flag — without breaking the registration API. | ||
| * | ||
| * @internal | ||
| */ | ||
| type ClusterCheckEntry = { | ||
| /** The check class constructor for DI container instantiation. */ | ||
| class: ClusterCheckClass; | ||
| }; | ||
|
|
||
| /** | ||
| * Low-level metadata registry for cluster checks. | ||
| * | ||
| * `ClusterCheckMetadata` is a simple collection of every class decorated with | ||
| * `@ClusterCheck()`. It is populated automatically at module load time, before | ||
| * the Leader Service starts, so checks are discoverable without any manual | ||
| * registration code. | ||
| * | ||
| * **Architecture:** | ||
| * ``` | ||
| * @ClusterCheck() → ClusterCheckMetadata → Cluster Check Registry → Leader Service | ||
| * (registration) (collection) (instantiation) (execution) | ||
| * ``` | ||
| * | ||
| * **Responsibilities kept here:** | ||
| * - Storing registered check classes. | ||
| * - Exposing them for downstream consumers. | ||
| * | ||
| * **Responsibilities explicitly NOT here:** | ||
| * - Instantiating checks (DI container). | ||
| * - Validating name uniqueness (higher-level registry). | ||
| * - Executing checks (Leader Service). | ||
| * | ||
| * @see ClusterCheck decorator for automatic registration. | ||
| * @see IClusterCheck for the check contract. | ||
| */ | ||
| @Service() | ||
| export class ClusterCheckMetadata { | ||
| /** | ||
| * Internal collection of registered cluster check entries. | ||
| * | ||
| * Uses `Set` for efficient deduplication (though duplicate registration | ||
| * should not occur with correct decorator usage). | ||
| */ | ||
| private readonly clusterChecks: Set<ClusterCheckEntry> = new Set(); | ||
|
|
||
| /** | ||
| * Registers a cluster check class in the metadata collection. | ||
| * | ||
| * Called automatically by the `@ClusterCheck()` decorator at module load | ||
| * time. Should not be called directly by application code. | ||
| * | ||
| * Name uniqueness and any other semantic validation is the responsibility | ||
| * of the higher-level Cluster Check Registry, not this service. | ||
| * | ||
| * @param entry - The check class entry to register. | ||
| * @internal Called by decorator only. | ||
| */ | ||
| register(entry: ClusterCheckEntry) { | ||
| this.clusterChecks.add(entry); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves all registered check class constructors in registration order. | ||
| * | ||
| * This is the primary method consumed by the Cluster Check Registry to | ||
| * instantiate checks via the DI container. | ||
| * | ||
| * @returns Array of check class constructors ready for DI instantiation. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * @Service() | ||
| * export class ClusterCheckRegistry { | ||
| * constructor(private readonly metadata: ClusterCheckMetadata) { | ||
| * this.checks = this.metadata.getClasses().map((cls) => Container.get(cls)); | ||
| * } | ||
| * } | ||
| * ``` | ||
| */ | ||
| getClasses() { | ||
| return [...this.clusterChecks.values()].map((entry) => entry.class); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Class decorator that registers a cluster check for auto-discovery. | ||
| * | ||
| * Performs two distinct operations at class-definition time: | ||
| * 1. **Registers** the class in {@link ClusterCheckMetadata} so the higher-level | ||
| * registry can discover it without manual wiring. | ||
| * 2. **Enables DI** by applying `@Service()`, making the class constructor | ||
| * resolvable via `Container.get()` (including dependencies). | ||
| * | ||
| * The decorator takes no arguments — all check metadata lives on the check | ||
| * instance itself via {@link IClusterCheck.checkDescription}. | ||
| * | ||
| * **Requirements:** | ||
| * - Decorated class MUST implement {@link IClusterCheck}. | ||
| * - Decorated class MUST expose a `checkDescription` with a unique `name`. | ||
| * | ||
| * **Notes:** | ||
| * - Registration happens eagerly at module load, not lazily. | ||
| * - Duplicate decoration of the same class is safe (`Set` deduplicates). | ||
| * - Checks are registered as singletons via `@Service()`. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * @ClusterCheck() | ||
| * export class VersionMismatchCheck implements IClusterCheck { | ||
| * checkDescription = { | ||
| * name: 'cluster.versionMismatch', | ||
| * displayName: 'Cluster Version Mismatch', | ||
| * }; | ||
| * | ||
| * async run({ currentState }: ClusterCheckContext): Promise<ClusterCheckResult> { | ||
| * // ... check logic | ||
| * return {}; | ||
| * } | ||
| * } | ||
| * ``` | ||
| * | ||
| * @returns A class decorator that registers the check and enables DI for it. | ||
| */ | ||
| export const ClusterCheck = | ||
| <T extends ClusterCheckClass>() => | ||
| (target: T) => { | ||
| Container.get(ClusterCheckMetadata).register({ | ||
| class: target, | ||
| }); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
| return Service()(target); | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.