-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
55 additions
and
47 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,52 @@ | ||
import { sleep } from '@fuel-ts/utils'; | ||
import { execSync } from 'child_process'; | ||
|
||
export function findChildProcessPid( | ||
parentPid: number, | ||
childProcessName: string | ||
): number | undefined { | ||
const childProcesses = execSync(`ps --ppid ${parentPid} -o pid,cmd --no-headers || true`) | ||
.toString() | ||
.split('\n') | ||
.map((s) => s.trim()) | ||
.filter((s) => s !== ''); | ||
|
||
for (const cp of childProcesses) { | ||
const [pid, name] = cp.split(' '); | ||
if (name.indexOf(childProcessName) !== -1) { | ||
return +pid; | ||
} | ||
const childPid = findChildProcessPid(+pid, childProcessName); | ||
if (childPid) { | ||
return childPid; | ||
} | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
function isProcessRunning(pid: number) { | ||
try { | ||
// Check if the process exists | ||
process.kill(pid, 0); | ||
return true; // If no error, the process is running | ||
} catch (e) { | ||
const error = e as Error & { code: string }; | ||
// Error codes: | ||
// ESRCH: No such process | ||
// EPERM: Permission denied (you don't have permissions to check) | ||
if (error.code === 'ESRCH') { | ||
return false; // No such process | ||
} | ||
if (error.code === 'EPERM') { | ||
return true; // Process exists, but we don't have permission to send a signal | ||
} | ||
throw error; // Some other unexpected error | ||
} | ||
} | ||
|
||
export async function waitProcessEnd(pid: number) { | ||
while (isProcessRunning(pid)) { | ||
await sleep(100); | ||
} | ||
} |