-
Notifications
You must be signed in to change notification settings - Fork 0
feat(api): integrate NATS for project slug-to-ID resolution #53
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 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b81d519
docs(chore): remove subagent system and simplify claude instructions
asithade ddbcafa
feat(ui): refactor meeting-create to meeting-manage with edit support
asithade 287e10a
fix: update shared package deps
asithade 643d308
fix(shared): correct MS_IN_DAY calculation and add comprehensive JSDoc
asithade b8f1c6f
Merge branch 'main' into feat/LFXV2-286
asithade 0cf9db9
feat(shared): standardize file naming and add comprehensive JSDoc
asithade 3c052de
fix(meeting): implement timezone-aware datetime handling
asithade afb3e15
fix(validation): prevent NaN duration and improve form validation
asithade 407aec9
feat(api): integrate NATS for project slug-to-ID resolution
asithade 11410b5
Merge branch 'main' into feat/LFXV2-337
asithade f6471fe
refactor(api): improve NATS service architecture and thread safety
asithade 1715d4c
feat(test): add playwright API mocking for NATS-dependent endpoints
asithade 57294a5
fix(api): correct project query parameter from uid to tags
asithade 1e85bfe
refactor(api): implement self-contained dependency injection pattern
asithade 336cb90
refactor(api): move NATS config to shared package
asithade 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
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
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,22 @@ | ||
| // Copyright The Linux Foundation and each contributor to LFX. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| /** | ||
| * NATS configuration constants | ||
| */ | ||
| export const NATS_CONFIG = { | ||
| /** | ||
| * Default NATS server URL for Kubernetes cluster | ||
| */ | ||
| DEFAULT_SERVER_URL: 'nats://lfx-platform-nats.lfx.svc.cluster.local:4222', | ||
|
|
||
| /** | ||
| * Connection timeout in milliseconds | ||
| */ | ||
| CONNECTION_TIMEOUT: 5000, | ||
|
|
||
| /** | ||
| * Request timeout in milliseconds | ||
| */ | ||
| REQUEST_TIMEOUT: 5000, | ||
| } as const; |
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
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,141 @@ | ||
| // Copyright The Linux Foundation and each contributor to LFX. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| import { NatsSubjects, ProjectSlugToIdResponse } from '@lfx-pcc/shared/interfaces'; | ||
| import { connect, NatsConnection, StringCodec } from 'nats'; | ||
|
|
||
| import { NATS_CONFIG } from '../config/nats.config'; | ||
| import { serverLogger } from '../server'; | ||
|
|
||
| export class NatsService { | ||
| private connection: NatsConnection | null = null; | ||
| private connectionPromise: Promise<NatsConnection> | null = null; | ||
| private codec = StringCodec(); | ||
|
|
||
| /** | ||
| * Get project ID by slug using NATS request-reply pattern | ||
| */ | ||
| public async getProjectIdBySlug(slug: string): Promise<ProjectSlugToIdResponse> { | ||
| const connection = await this.ensureConnection(); | ||
|
|
||
| try { | ||
| const response = await connection.request(NatsSubjects.PROJECT_SLUG_TO_UID, this.codec.encode(slug), { timeout: NATS_CONFIG.REQUEST_TIMEOUT }); | ||
|
|
||
| const projectId = this.codec.decode(response.data); | ||
|
|
||
| // Check if we got a valid project ID | ||
| if (!projectId || projectId.trim() === '') { | ||
| serverLogger.info({ slug }, 'Project slug not found via NATS'); | ||
| return { | ||
| projectId: '', | ||
| slug, | ||
| exists: false, | ||
| }; | ||
| } | ||
|
|
||
| serverLogger.info({ slug, project_id: projectId }, 'Successfully resolved project slug to ID'); | ||
|
|
||
| return { | ||
| projectId: projectId.trim(), | ||
| slug, | ||
| exists: true, | ||
| }; | ||
| } catch (error) { | ||
| serverLogger.error({ error: error instanceof Error ? error.message : error, slug }, 'Failed to resolve project slug via NATS'); | ||
|
|
||
| // If it's a timeout or no responder error, treat as not found | ||
| if (error instanceof Error && (error.message.includes('timeout') || error.message.includes('503'))) { | ||
| return { | ||
| projectId: '', | ||
| slug, | ||
| exists: false, | ||
| }; | ||
| } | ||
|
|
||
| throw error; | ||
| } | ||
asithade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
asithade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Check if NATS connection is active | ||
| */ | ||
| public isConnected(): boolean { | ||
| return this.connection !== null && !this.connection.isClosed(); | ||
| } | ||
|
|
||
| /** | ||
| * Gracefully shutdown NATS connection | ||
| */ | ||
| public async shutdown(): Promise<void> { | ||
| if (this.connection && !this.connection.isClosed()) { | ||
| serverLogger.info('Shutting down NATS connection'); | ||
|
|
||
| try { | ||
| await this.connection.drain(); | ||
| serverLogger.info('NATS connection closed successfully'); | ||
| } catch (error) { | ||
| serverLogger.error({ error: error instanceof Error ? error.message : error }, 'Error during NATS shutdown'); | ||
| } | ||
| } | ||
| this.connection = null; | ||
| } | ||
|
|
||
| /** | ||
| * Ensure NATS connection with thread safety (lazy initialization) | ||
| */ | ||
| private async ensureConnection(): Promise<NatsConnection> { | ||
| // Return existing connection if valid | ||
| if (this.connection && !this.connection.isClosed()) { | ||
| return this.connection; | ||
| } | ||
|
|
||
| // If already connecting, wait for that connection | ||
| if (this.connectionPromise) { | ||
| return this.connectionPromise; | ||
| } | ||
|
|
||
| // Create new connection | ||
| this.connectionPromise = this.createConnection(); | ||
|
|
||
| try { | ||
| this.connection = await this.connectionPromise; | ||
| return this.connection; | ||
| } catch (error) { | ||
| // Reset connection promise on failure | ||
| this.connectionPromise = null; | ||
| throw error; | ||
| } finally { | ||
| // Reset connection promise after completion | ||
| this.connectionPromise = null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create a new NATS connection | ||
| */ | ||
| private async createConnection(): Promise<NatsConnection> { | ||
| const natsUrl = process.env['NATS_URL'] || NATS_CONFIG.DEFAULT_SERVER_URL; | ||
|
|
||
| try { | ||
| serverLogger.info({ url: natsUrl }, 'Connecting to NATS server on demand'); | ||
|
|
||
| const connection = await connect({ | ||
| servers: [natsUrl], | ||
| timeout: NATS_CONFIG.CONNECTION_TIMEOUT, | ||
| }); | ||
|
|
||
| serverLogger.info('Successfully connected to NATS server'); | ||
| return connection; | ||
| } catch (error) { | ||
| serverLogger.error( | ||
| { | ||
| error: error instanceof Error ? error.message : error, | ||
| url: natsUrl, | ||
| suggestion: 'If running locally, you may need to port-forward NATS: kubectl port-forward -n lfx svc/lfx-platform-nats 4222:4222', | ||
| }, | ||
| 'Failed to connect to NATS server' | ||
| ); | ||
| throw error; | ||
| } | ||
asithade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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.