-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dev): wait until all app server descendants have been killed
- Loading branch information
Showing
4 changed files
with
57 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import execa from "execa"; | ||
import pidtree from "pidtree"; | ||
|
||
let isWindows = process.platform === "win32"; | ||
|
||
export let kill = async (pid: number) => { | ||
try { | ||
let cmd = isWindows | ||
? ["taskkill", "/F", "/PID", pid.toString()] | ||
: ["kill", "-9", pid.toString()]; | ||
await execa(cmd[0], cmd.slice(1)); | ||
} catch (error) { | ||
throw new Error(`Failed to kill process ${pid}: ${error}`); | ||
} | ||
}; | ||
|
||
let isAlive = (pid: number) => { | ||
try { | ||
process.kill(pid, 0); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
}; | ||
|
||
export let killtree = async (pid: number) => { | ||
let descendants = await pidtree(pid); | ||
let pids = [pid, ...descendants]; | ||
await kill(pid); | ||
return new Promise<void>((resolve, reject) => { | ||
let check = setInterval(() => { | ||
let alive = pids.filter(isAlive); | ||
if (alive.length === 0) { | ||
clearInterval(check); | ||
resolve(); | ||
} | ||
}, 50); | ||
|
||
setTimeout(() => { | ||
clearInterval(check); | ||
reject( | ||
new Error("Timeout: Processes did not exit within the specified time.") | ||
); | ||
}, 2000); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters