Skip to content

Commit f93bd1d

Browse files
committed
fix(dev): keep warming the pool once the app is served by a fork
1 parent dfef70f commit f93bd1d

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

packages/nuxt-cli/src/dev/pool.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ export class ForkPool {
6060
}
6161

6262
async getFork(context: NuxtDevContext, onMessage?: (message: NuxtDevIPCMessage) => void): Promise<() => Promise<void>> {
63+
// Once the app is served by a fork, file changes are no longer visible to
64+
// this process, so a restart is the only signal left that more may follow.
65+
this.warming = true
66+
6367
// Try to get a ready fork from the pool
6468
const readyFork = this.pool.find(f => f.state === 'ready')
6569

@@ -70,10 +74,7 @@ export class ForkPool {
7074
}
7175
await this.sendContext(readyFork.process, context)
7276

73-
// Start warming a replacement fork
74-
if (this.warming) {
75-
this.warmFork()
76-
}
77+
this.warmFork()
7778

7879
return () => this.killFork(readyFork)
7980
}
@@ -88,10 +89,7 @@ export class ForkPool {
8889
}
8990
await this.sendContext(warmingFork.process, context)
9091

91-
// Start warming a replacement fork
92-
if (this.warming) {
93-
this.warmFork()
94-
}
92+
this.warmFork()
9593

9694
return () => this.killFork(warmingFork)
9795
}
@@ -107,6 +105,8 @@ export class ForkPool {
107105
}
108106
await this.sendContext(coldFork.process, context)
109107

108+
this.warmFork()
109+
110110
return () => this.killFork(coldFork)
111111
}
112112

@@ -120,6 +120,11 @@ export class ForkPool {
120120
}
121121

122122
private warmFork(): void {
123+
const idle = this.pool.filter(f => f.state === 'warming' || f.state === 'ready').length
124+
if (idle >= this.poolSize) {
125+
return
126+
}
127+
123128
const fork = this.createFork()
124129
fork.ready.then(() => {
125130
if (fork.state === 'warming') {

packages/nuxt-cli/test/unit/fork-pool.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ describe('forkPool', () => {
4747
}, { timeout: 10_000, interval: 25 })
4848
})
4949

50+
it('should keep the pool warm after handing out a fork', { timeout: 30_000 }, async () => {
51+
const pool = createPool()
52+
await pool.getFork({ cwd: '/some/project', args: {} })
53+
await waitForReady(pool, 1)
54+
expect(pool.getStats().active).toBe(1)
55+
})
56+
57+
it('should never warm a fork when the pool is disabled', { timeout: 30_000 }, async () => {
58+
const pool = createPool(0)
59+
pool.startWarming()
60+
await pool.getFork({ cwd: '/some/project', args: {} })
61+
await new Promise(resolve => setTimeout(resolve, 1000))
62+
expect(pool.getStats()).toMatchObject({ total: 1, active: 1, ready: 0, warming: 0 })
63+
})
64+
5065
it('should kill every fork on shutdown', { timeout: 30_000 }, async () => {
5166
const pool = createPool(3)
5267
pool.startWarming()

0 commit comments

Comments
 (0)