From 373e44f42c294b3bbed78a8d861573d01645cb08 Mon Sep 17 00:00:00 2001 From: Aditya Pandey Date: Wed, 9 Oct 2024 18:27:35 -0500 Subject: [PATCH] Properly kill dev api-server --- packages/api-server/src/buildManager.ts | 10 +++---- packages/api-server/src/serverManager.ts | 35 +++++++++++++++++++++--- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/packages/api-server/src/buildManager.ts b/packages/api-server/src/buildManager.ts index bc53b679eb2a..8ebdecdeeea8 100644 --- a/packages/api-server/src/buildManager.ts +++ b/packages/api-server/src/buildManager.ts @@ -5,11 +5,6 @@ export type BuildAndRestartOptions = { clean?: boolean } -// We want to delay execution when multiple files are modified on the filesystem, -// this usually happens when running RedwoodJS generator commands. -// Local writes are very fast, but writes in e2e environments are not, -// so allow the default to be adjusted with an env-var. -// class BuildManager { private shouldRebuild: boolean private shouldClean: boolean @@ -34,6 +29,11 @@ class BuildManager { this.shouldClean = false } }, + // We want to delay execution when multiple files are modified on the filesystem, + // this usually happens when running RedwoodJS generator commands. + // Local writes are very fast, but writes in e2e environments are not, + // so allow the default to be adjusted with an env-var. + // process.env.RWJS_DELAY_RESTART ? parseInt(process.env.RWJS_DELAY_RESTART, 10) : 500, diff --git a/packages/api-server/src/serverManager.ts b/packages/api-server/src/serverManager.ts index 526c303b3ed4..55b3a90175aa 100644 --- a/packages/api-server/src/serverManager.ts +++ b/packages/api-server/src/serverManager.ts @@ -3,6 +3,7 @@ import { fork } from 'child_process' import fs from 'fs' import path from 'path' +import chalk from 'chalk' import yargs from 'yargs' import { hideBin } from 'yargs/helpers' @@ -82,13 +83,39 @@ export class ServerManager { } async restartApiServer() { - this.killApiServer() + await this.killApiServer() await this.startApiServer() } - killApiServer() { - this.httpServerProcess?.emit('exit') - this.httpServerProcess?.kill() + async killApiServer() { + if (!this.httpServerProcess) { + return + } + + // Try to gracefully close the server + // If it doesn't close within 2 seconds, forcefully close it + await Promise.race([ + new Promise((resolve) => { + console.log( + chalk.yellow( + 'API server did not exit within 2 seconds, forcefully closing it.', + ), + ) + this.httpServerProcess!.on('exit', () => resolve()) + this.httpServerProcess!.kill() + }), + new Promise((resolve) => + setTimeout(() => { + console.log( + chalk.yellow( + 'API server did not exit within 2 seconds, forcefully closing it.', + ), + ) + this.httpServerProcess!.kill('SIGKILL') + resolve() + }, 2000), + ), + ]) } }