-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Use child_process.spawn instead of child_process.exec
- Loading branch information
1 parent
2c78703
commit 746affd
Showing
6 changed files
with
162 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,106 @@ | ||
import { exec } from 'child_process'; | ||
import { promisify } from 'util'; | ||
import { ChildProcess, spawn } from 'child_process'; | ||
import type { ExecResult, RawExecOptions } from './types'; | ||
|
||
// https://man7.org/linux/man-pages/man7/signal.7.html#NAME | ||
// Non TERM/CORE signals | ||
const NONTERM = [ | ||
'SIGCHLD', | ||
'SIGCLD', | ||
'SIGCONT', | ||
'SIGSTOP', | ||
'SIGTSTP', | ||
'SIGTTIN', | ||
'SIGTTOU', | ||
'SIGURG', | ||
'SIGWINCH', | ||
]; | ||
|
||
function stringify(stream: Buffer[], encoding: BufferEncoding): string { | ||
return Buffer.concat(stream).toString(encoding); | ||
} | ||
|
||
function initStreamListeners( | ||
cp: ChildProcess, | ||
opts: RawExecOptions & { maxBuffer: number; encoding: BufferEncoding } | ||
): [Buffer[], Buffer[]] { | ||
const stdout: Buffer[] = []; | ||
const stderr: Buffer[] = []; | ||
let stdoutLen = 0; | ||
let stderrLen = 0; | ||
|
||
cp.stdout?.on('data', (data: Buffer) => { | ||
// process.stdout.write(data.toString()); | ||
const len = Buffer.byteLength(data, opts.encoding); | ||
stdoutLen += len; | ||
if (stdoutLen > opts.maxBuffer) { | ||
cp.emit('error', new Error('exceeded max buffer size for stdout')); | ||
} else { | ||
stdout.push(data); | ||
} | ||
}); | ||
cp.stderr?.on('data', (data: Buffer) => { | ||
// process.stderr.write(data.toString()); | ||
const len = Buffer.byteLength(data, opts.encoding); | ||
stderrLen += len; | ||
if (stderrLen > opts.maxBuffer) { | ||
cp.emit('error', new Error('exceeded max buffer size for stderr')); | ||
} else { | ||
stderr.push(data); | ||
} | ||
}); | ||
return [stdout, stderr]; | ||
} | ||
|
||
function promisifySpawn( | ||
cmd: string, | ||
opts: RawExecOptions | ||
): Promise<ExecResult> { | ||
return new Promise((resolve, reject) => { | ||
const encoding = opts.encoding as BufferEncoding; | ||
const [command, ...args] = cmd.split(/\s+/); | ||
const maxBuffer = opts.maxBuffer ?? 10 * 1024 * 1024; // Set default max buffer size to 10MB | ||
const cp = spawn(command, args, { ...opts, detached: true }); // PID range hack; force detached | ||
const [stdout, stderr] = initStreamListeners(cp, { | ||
...opts, | ||
maxBuffer, | ||
encoding, | ||
}); // handle streams | ||
|
||
// handle process events | ||
cp.on('error', (error) => { | ||
reject(error.message); | ||
}); | ||
|
||
cp.on('exit', (code: number, signal: string) => { | ||
if (signal && !NONTERM.includes(signal)) { | ||
try { | ||
process.kill(-(cp.pid as number), signal); // PID range hack; signal process tree | ||
} catch (err) { | ||
// cp is a single node tree, therefore -pid is invalid, | ||
} | ||
stderr.push( | ||
Buffer.from( | ||
`PID= ${cp.pid as number}\n` + | ||
`COMMAND= "${cp.spawnargs.join(' ')}"\n` + | ||
`Signaled with "${signal}"` | ||
) | ||
); | ||
reject(stringify(stderr, encoding)); | ||
return; | ||
} | ||
if (code !== 0) { | ||
reject(stringify(stderr, encoding)); | ||
return; | ||
} | ||
resolve({ | ||
stderr: stringify(stderr, encoding), | ||
stdout: stringify(stdout, encoding), | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
export const rawExec: ( | ||
cmd: string, | ||
opts: RawExecOptions | ||
) => Promise<ExecResult> = promisify(exec); | ||
) => Promise<ExecResult> = promisifySpawn; |
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 { logger } from '../../logger'; | ||
import { rawExec } from './common'; | ||
// import { rawExec } from './common'; | ||
import type { RawExecOptions } from './types'; | ||
|
||
void (async () => { | ||
const cmds: [string, RawExecOptions][] = []; | ||
const opts: RawExecOptions = { | ||
encoding: 'utf8', | ||
shell: true, | ||
timeout: 2000, | ||
}; | ||
logger.info('driver function - START'); | ||
cmds.push(['npm run non-existent-script', opts]); | ||
cmds.push(['docker', { ...opts, shell: false }]); | ||
cmds.push(['docker image rm alpine', { ...opts, timeout: 0 }]); | ||
cmds.push(['docker images', opts]); | ||
cmds.push(['docker pull alpine', { ...opts, timeout: 0 }]); | ||
cmds.push(['docker images', opts]); | ||
cmds.push(['npm run spawn-testing-script', opts]); | ||
cmds.push(['npm run spawn-testing-script', { ...opts, shell: false }]); | ||
cmds.push(['sleep 900', opts]); | ||
cmds.push(['sleep 900', { ...opts, shell: false }]); | ||
cmds.push(['sleep 900', { ...opts, shell: '/bin/bash' }]); | ||
cmds.push(['ls -l /', { ...opts, timeout: 0, maxBuffer: 100 }]); | ||
cmds.push(['ps -auxf', opts]); | ||
|
||
for (const [cmd, opts] of cmds) { | ||
logger.info('-------------------------------------------------------'); | ||
logger.info({ opts }, `Run rawSpawn() - START - "${cmd}"`); | ||
try { | ||
const { stdout, stderr } = await rawExec(cmd, opts); | ||
// const { stdout, stderr } = await rawExec(cmd, {encoding: 'utf8', timeout: 0}); | ||
if (stdout) { | ||
logger.info(stdout); | ||
} | ||
if (stderr) { | ||
logger.warn(stderr); | ||
} | ||
} catch (err) { | ||
logger.error(err as string); | ||
} | ||
logger.info(`run cmd - END - "${cmd}"`); | ||
} | ||
logger.info('driver function - END'); | ||
})(); |
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
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