Skip to content

Commit 586b62c

Browse files
Load direct queue handlers during framework boot
1 parent 15f7971 commit 586b62c

13 files changed

Lines changed: 188 additions & 41 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@workflow/nest': patch
3+
'@workflow/nitro': patch
4+
---
5+
6+
Load generated workflow bundles during server boot so direct-delivery queue handlers register before background workers start.

‎packages/nest/src/workflow.controller.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ function getOutDir(): string {
9191
return controllerConfig.outDir;
9292
}
9393

94+
export async function loadWorkflowHandler(): Promise<
95+
(request: Request) => Promise<Response>
96+
> {
97+
const outDir = getOutDir();
98+
await import(pathToFileURL(join(outDir, 'steps.mjs')).href);
99+
const { POST } = await import(
100+
pathToFileURL(join(outDir, 'workflows.mjs')).href
101+
);
102+
return POST;
103+
}
104+
94105
/**
95106
* Controller that handles the well-known workflow endpoints.
96107
* Dynamically imports the generated bundles and handles request/response conversion.
@@ -99,12 +110,7 @@ function getOutDir(): string {
99110
export class WorkflowController {
100111
@Post('flow')
101112
async handleFlow(@Req() req: any, @Res() res: any) {
102-
const outDir = getOutDir();
103-
// Import step registrations (side effects) before the combined handler
104-
await import(pathToFileURL(join(outDir, 'steps.mjs')).href);
105-
const { POST } = await import(
106-
pathToFileURL(join(outDir, 'workflows.mjs')).href
107-
);
113+
const POST = await loadWorkflowHandler();
108114
const webRequest = toWebRequest(req);
109115
const webResponse = await POST(webRequest);
110116
await sendWebResponse(res, webResponse);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
2+
import { tmpdir } from 'node:os';
3+
import { join } from 'node:path';
4+
import { afterEach, describe, expect, it } from 'vitest';
5+
import { WorkflowModule } from './workflow.module.js';
6+
7+
const temporaryDirectories: string[] = [];
8+
9+
describe('WorkflowModule direct queue bootstrap', () => {
10+
afterEach(async () => {
11+
delete process.env.WORKFLOW_TARGET_WORLD;
12+
await new WorkflowModule().onModuleDestroy();
13+
await Promise.all(
14+
temporaryDirectories
15+
.splice(0)
16+
.map((directory) => rm(directory, { force: true, recursive: true }))
17+
);
18+
});
19+
20+
it('loads generated bundles during Postgres module initialization', async () => {
21+
const outDir = await mkdtemp(join(tmpdir(), 'workflow-nest-bootstrap-'));
22+
temporaryDirectories.push(outDir);
23+
const registrationKey = `__workflowNestRegistration${Date.now()}`;
24+
await writeFile(
25+
join(outDir, 'steps.mjs'),
26+
`globalThis.${registrationKey} = 'steps';`
27+
);
28+
await writeFile(
29+
join(outDir, 'workflows.mjs'),
30+
`globalThis.${registrationKey} += ':workflows'; export async function POST() { return new Response(); }`
31+
);
32+
process.env.WORKFLOW_TARGET_WORLD = '@workflow/world-postgres';
33+
WorkflowModule.forRoot({ outDir, skipBuild: true });
34+
35+
await new WorkflowModule().onModuleInit();
36+
37+
expect((globalThis as Record<string, unknown>)[registrationKey]).toBe(
38+
'steps:workflows'
39+
);
40+
delete (globalThis as Record<string, unknown>)[registrationKey];
41+
});
42+
43+
it('does not load generated bundles for HTTP-delivery Worlds', async () => {
44+
process.env.WORKFLOW_TARGET_WORLD = 'local';
45+
WorkflowModule.forRoot({
46+
outDir: '/missing/workflow-output',
47+
skipBuild: true,
48+
});
49+
50+
await expect(new WorkflowModule().onModuleInit()).resolves.toBeUndefined();
51+
});
52+
});

‎packages/nest/src/workflow.module.ts‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { join } from 'pathe';
99
import type { NestBuilderOptions } from './builder.js';
1010
import {
1111
configureWorkflowController,
12+
loadWorkflowHandler,
1213
WorkflowController,
1314
} from './workflow.controller.js';
1415

@@ -86,19 +87,25 @@ export class WorkflowModule implements OnModuleInit, OnModuleDestroy {
8687

8788
async onModuleInit() {
8889
const options = WorkflowModule.state.options;
89-
if (!options || options.skipBuild) {
90+
const outDir = WorkflowModule.state.outDir;
91+
if (!options || !outDir) {
9092
return;
9193
}
92-
// Lazy-load the toolchain so it never enters the runtime bundle.
93-
const [{ NestLocalBuilder }, { createBuildQueue }] = await Promise.all([
94-
import('./builder.js'),
95-
import('@workflow/builders'),
96-
]);
97-
const builder = new NestLocalBuilder({
98-
...options,
99-
outDir: WorkflowModule.state.outDir ?? undefined,
100-
});
101-
await createBuildQueue()(() => builder.build());
94+
if (!options.skipBuild) {
95+
// Lazy-load the toolchain so it never enters the runtime bundle.
96+
const [{ NestLocalBuilder }, { createBuildQueue }] = await Promise.all([
97+
import('./builder.js'),
98+
import('@workflow/builders'),
99+
]);
100+
const builder = new NestLocalBuilder({ ...options, outDir });
101+
await createBuildQueue()(() => builder.build());
102+
}
103+
104+
if (process.env.WORKFLOW_TARGET_WORLD === '@workflow/world-postgres') {
105+
// Postgres consumes deliveries in-process, so register its generated
106+
// handler before the application's startup hook starts the World.
107+
await loadWorkflowHandler();
108+
}
102109
}
103110

104111
async onModuleDestroy() {

‎packages/nitro/src/index.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,25 @@ describe('@workflow/nitro virtual handlers', () => {
114114
);
115115
}
116116
});
117+
118+
it('loads the development flow bundle at boot with generation-scoped retries', async () => {
119+
const nitro = createNitroStub({ routing: true, dev: true });
120+
121+
await nitroModule.setup(nitro);
122+
123+
const flowSource = nitro.options.virtual['#workflow/workflows.mjs'];
124+
expect(flowSource).toContain('void (async () =>');
125+
expect(flowSource).toContain('await loadPOST()');
126+
expect(flowSource).toContain(
127+
'while (registrationState.generation === registrationGeneration)'
128+
);
129+
expect(flowSource).toContain(
130+
"Symbol.for('@workflow/nitro//directQueueRegistration')"
131+
);
132+
133+
const webhookSource = nitro.options.virtual['#workflow/webhook.mjs'];
134+
expect(webhookSource).not.toContain('registrationGeneration');
135+
});
117136
});
118137

119138
describe('@workflow/nitro builder lifecycle', () => {

‎packages/nitro/src/index.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,34 @@ function addVirtualHandler(
476476
'workflow/webhook.mjs': '',
477477
'workflow/workflows.mjs': `await import(/* @vite-ignore */ pathToFileURL(${stepsImportPath}).href + "?t=" + version);`,
478478
};
479+
const bootLoad: Record<VirtualHandlerPath, string> = {
480+
'workflow/webhook.mjs': '',
481+
'workflow/workflows.mjs': /* js */ `
482+
if (process.env.WORKFLOW_TARGET_WORLD === '@workflow/world-postgres') {
483+
const registrationKey = Symbol.for('@workflow/nitro//directQueueRegistration');
484+
const registrationState = globalThis[registrationKey] ??= { generation: 0 };
485+
const registrationGeneration = ++registrationState.generation;
486+
void (async () => {
487+
let attempt = 0;
488+
while (registrationState.generation === registrationGeneration) {
489+
try {
490+
await loadPOST();
491+
return;
492+
} catch (error) {
493+
attempt += 1;
494+
if (attempt === 60 || attempt % 120 === 0) {
495+
console.warn(
496+
'[workflow/nitro] The generated flow bundle is not available at boot; queue handler registration is still retrying:',
497+
error
498+
);
499+
}
500+
}
501+
const delayMs = Math.min(100 + attempt * 100, 5000);
502+
await new Promise((resolve) => setTimeout(resolve, delayMs).unref?.());
503+
}
504+
})();
505+
}`,
506+
};
479507

480508
if (nitro.options.dev) {
481509
// Dev mode: load generated workflow bundles from disk at request time.
@@ -501,6 +529,7 @@ function addVirtualHandler(
501529
}
502530
return (await import(currentImportPath)).POST;
503531
}
532+
${bootLoad[buildPath]}
504533
505534
export default fromWebHandler(async (request, context) => {
506535
const POST = await loadPOST();
@@ -525,6 +554,7 @@ function addVirtualHandler(
525554
}
526555
return (await import(currentImportPath)).POST;
527556
}
557+
${bootLoad[buildPath]}
528558
529559
export default async ({ req }) => {
530560
try {

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎workbench/astro/scripts/start-with-pg.mjs‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
#!/usr/bin/env node
22

3-
// Start the Postgres World before starting the Astro server
4-
// Needed since we test this in CI
5-
// Astro doesn't have a hook for starting the Postgres World in production
3+
// Start the Postgres World before starting the Astro server.
4+
// Needed since we test this in CI and Astro has no production startup hook.
65

76
async function main() {
8-
if (process.env.WORKFLOW_TARGET_WORLD === '@workflow/world-postgres') {
9-
console.log('Starting Postgres World...');
7+
const usePostgres =
8+
process.env.WORKFLOW_TARGET_WORLD === '@workflow/world-postgres';
9+
10+
if (usePostgres) {
11+
// Astro lazily loads endpoint modules. Import the generated source module
12+
// directly so it can register the in-process queue handler before start().
13+
await import('../src/pages/.well-known/workflow/v1/flow.js');
14+
1015
const { getWorld } = await import('workflow/runtime');
11-
const world = await getWorld();
12-
if (world.start) {
13-
console.log('Starting World workers...');
14-
await world.start();
15-
}
16+
console.log('Starting World workers...');
17+
await (await getWorld()).start?.();
1618
}
1719

18-
// Now start the Astro server
1920
await import('../dist/server/entry.mjs');
2021
}
2122

‎workbench/nest/src/main.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,6 @@ import type { NestExpressApplication } from '@nestjs/platform-express';
44
import { AppModule } from './app.module.js';
55

66
async function bootstrap() {
7-
// Start the Postgres World if configured
8-
if (process.env.WORKFLOW_TARGET_WORLD === '@workflow/world-postgres') {
9-
const { getWorld } = await import('workflow/runtime');
10-
const world = await getWorld();
11-
if (world.start) {
12-
console.log('Starting World workers...');
13-
await world.start();
14-
}
15-
}
16-
177
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
188
bodyParser: false,
199
});
@@ -25,6 +15,15 @@ async function bootstrap() {
2515
app.use(expressModule.text({ type: 'text/*' }));
2616
app.use(expressModule.raw({ type: 'application/octet-stream' }));
2717

18+
// Module initialization imports the generated flow bundle, which registers
19+
// the in-process Postgres consumer before the worker starts.
20+
await app.init();
21+
if (process.env.WORKFLOW_TARGET_WORLD === '@workflow/world-postgres') {
22+
const { getWorld } = await import('workflow/runtime');
23+
console.log('Starting World workers...');
24+
await (await getWorld()).start?.();
25+
}
26+
2827
const port = process.env.PORT ?? 3000;
2928
await app.listen(port);
3029
console.log(`Application is running on: http://localhost:${port}`);

‎workbench/nextjs-turbopack/instrumentation.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { registerOTel } from '@vercel/otel';
22

3-
export function register() {
3+
export async function register() {
44
registerOTel({
55
serviceName: 'nextjs-turbopack',
66
instrumentationConfig: {
@@ -17,4 +17,13 @@ export function register() {
1717
},
1818
},
1919
});
20+
21+
if (
22+
process.env.NEXT_RUNTIME === 'nodejs' &&
23+
process.env.WORKFLOW_TARGET_WORLD === '@workflow/world-postgres'
24+
) {
25+
await import('./app/.well-known/workflow/v1/flow/route');
26+
const { getWorld } = await import('workflow/runtime');
27+
await (await getWorld()).start?.();
28+
}
2029
}

0 commit comments

Comments
 (0)