feat: Implement waitForNodemonExit method and enhance restart logic in NodemonService

- Added waitForNodemonExit method to ensure nodemon processes are fully terminated before restarting.
- Updated restart method to call waitForNodemonExit, improving process management during restarts.
- Introduced a new unit test to validate the behavior of the restart method, ensuring proper sequence of stop, wait, and start operations.
This commit is contained in:
Eli Bosley
2025-11-21 22:16:32 -05:00
parent 9253250dc5
commit a5e9b83374
2 changed files with 36 additions and 0 deletions

View File

@@ -360,4 +360,22 @@ describe('NodemonService', () => {
);
expect(result).toBe('');
});
it('waits for nodemon to exit during restart before starting again', async () => {
const service = new NodemonService(logger);
const stopSpy = vi.spyOn(service, 'stop').mockResolvedValue();
const startSpy = vi.spyOn(service, 'start').mockResolvedValue();
const waitSpy = vi
.spyOn(
service as unknown as { waitForNodemonExit: () => Promise<void> },
'waitForNodemonExit'
)
.mockResolvedValue();
await service.restart({ env: { LOG_LEVEL: 'DEBUG' } });
expect(stopSpy).toHaveBeenCalledWith({ quiet: true });
expect(waitSpy).toHaveBeenCalled();
expect(startSpy).toHaveBeenCalledWith({ env: { LOG_LEVEL: 'DEBUG' } });
});
});

View File

@@ -144,6 +144,23 @@ export class NodemonService {
}
}
private async waitForNodemonExit(timeoutMs = 5000, pollIntervalMs = 100) {
const deadline = Date.now() + timeoutMs;
// Poll for any remaining nodemon processes that match our config file
while (Date.now() < deadline) {
const pids = await this.findMatchingNodemonPids();
if (pids.length === 0) return;
const runningFlags = await Promise.all(pids.map((pid) => this.isPidRunning(pid)));
if (!runningFlags.some(Boolean)) return;
await new Promise((resolve) => setTimeout(resolve, pollIntervalMs));
}
this.logger.debug?.('Timed out waiting for nodemon to exit; continuing restart anyway.');
}
async start(options: StartOptions = {}) {
try {
await this.ensureNodemonDependencies();
@@ -256,6 +273,7 @@ export class NodemonService {
async restart(options: StartOptions = {}) {
await this.stop({ quiet: true });
await this.waitForNodemonExit();
await this.start(options);
}