Skip to content

Commit

Permalink
Properly kill dev api-server
Browse files Browse the repository at this point in the history
  • Loading branch information
callingmedic911 committed Oct 9, 2024
1 parent cd4c8bc commit 373e44f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
10 changes: 5 additions & 5 deletions packages/api-server/src/buildManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
35 changes: 31 additions & 4 deletions packages/api-server/src/serverManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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<void>((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<void>((resolve) =>
setTimeout(() => {
console.log(
chalk.yellow(
'API server did not exit within 2 seconds, forcefully closing it.',
),
)
this.httpServerProcess!.kill('SIGKILL')
resolve()
}, 2000),
),
])
}
}

Expand Down

0 comments on commit 373e44f

Please sign in to comment.