-
Notifications
You must be signed in to change notification settings - Fork 8
fix(evo-flow): reuse primary app context in action-node activities (EVO-1829) #75
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
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
27 changes: 27 additions & 0 deletions
27
src/modules/temporal/activities/no-app-bootstrap.guard.spec.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,27 @@ | ||
| import { readdirSync, readFileSync, statSync } from 'fs'; | ||
| import { join } from 'path'; | ||
|
|
||
| // EVO-1829 regression guard: Temporal activities must NOT bootstrap a second | ||
| // Nest app via createApplicationContext(AppModule.forRoot()). That boots a | ||
| // redundant worker + Kafka consumers in-process and silently freezes | ||
| // single-mode. Activities resolve DI from the primary context via | ||
| // app-context.holder (getAppContext) instead. | ||
| describe('EVO-1829: activities never bootstrap a second AppModule', () => { | ||
| const ACTIVITIES_DIR = __dirname; | ||
|
|
||
| function walk(dir: string): string[] { | ||
| return readdirSync(dir).flatMap((name) => { | ||
| const full = join(dir, name); | ||
| if (statSync(full).isDirectory()) return walk(full); | ||
| return full.endsWith('.ts') && !full.endsWith('.spec.ts') ? [full] : []; | ||
| }); | ||
| } | ||
|
|
||
| it('no activity file calls createApplicationContext(AppModule.forRoot())', () => { | ||
| const pattern = /createApplicationContext\s*\(\s*AppModule\.forRoot\(/; | ||
| const offenders = walk(ACTIVITIES_DIR).filter((file) => | ||
| pattern.test(readFileSync(file, 'utf8')), | ||
| ); | ||
| expect(offenders).toEqual([]); | ||
| }); | ||
| }); |
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
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,31 @@ | ||
| import type { INestApplicationContext } from '@nestjs/common'; | ||
|
|
||
| /** | ||
| * Primary Nest application context, stashed at boot (main.ts) so Temporal | ||
| * activities can resolve DI services WITHOUT bootstrapping a second AppModule. | ||
| * | ||
| * EVO-1829: action-node activities run outside the Nest container. Each used to | ||
| * call `NestFactory.createApplicationContext(AppModule.forRoot())`, which boots | ||
| * a full second app (Kafka consumers, a redundant Temporal worker, scheduled | ||
| * jobs) in-process and silently freezes single-mode. The primary context built | ||
| * by `NestFactory.create()` already provides every service those activities | ||
| * need and is guaranteed to exist before any activity dispatch, so they reuse | ||
| * it from here. | ||
| * | ||
| * Out of scope: `journey-tracking.activities.ts` deliberately does | ||
| * `new KafkaService()` (a producer-only force-init) — do NOT route it here. | ||
| */ | ||
| let appContext: INestApplicationContext | null = null; | ||
|
|
||
| export function setAppContext(context: INestApplicationContext): void { | ||
| appContext = context; | ||
| } | ||
|
|
||
| export function getAppContext(): INestApplicationContext { | ||
| if (!appContext) { | ||
| throw new Error( | ||
| 'App context not initialized: setAppContext() must run at boot before any Temporal activity', | ||
| ); | ||
| } | ||
| return appContext; | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (bug_risk): Consider guarding against multiple calls to
setAppContextto detect unintended re-bootstrap/overwrites.As written, a second Nest bootstrap calling
setAppContextwill silently replace the existing context, potentially masking bugs by swapping the DI graph at runtime. Consider guarding against this by throwing or at least logging whenappContextis already set, e.g.: