microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/childProcess.ts
200lines · modeblame
a31b007cunknown10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
6f8b71fedigeff10 years ago | 4 | import * as nodeChildProcess from "child_process"; |
09f6024fHeniker4 years ago | 5 | import { kill } from "process"; |
ce5e88eeYuri Skorokhodov5 years ago | 6 | import { ErrorHelper } from "../error/errorHelper"; |
| 7 | import { InternalErrorCode } from "../error/internalErrorCode"; | |
4bb0956eRedMickey5 years ago | 8 | import { notNullOrUndefined } from "../utils"; |
3fb37ad5unknown10 years ago | 9 | |
c7f1165cdigeff10 years ago | 10 | // Uncomment the following lines to record all spawned processes executions |
3c172a05Artem Egorov8 years ago | 11 | // import {Recorder} from "../../../test/resources/processExecution/recorder"; |
c7f1165cdigeff10 years ago | 12 | // Recorder.installGlobalRecorder(); |
| 13 | | |
3fb37ad5unknown10 years ago | 14 | export interface IExecResult { |
6f8b71fedigeff10 years ago | 15 | process: nodeChildProcess.ChildProcess; |
ce5e88eeYuri Skorokhodov5 years ago | 16 | outcome: Promise<string>; |
3fb37ad5unknown10 years ago | 17 | } |
| 18 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 19 | export interface ISpawnResult { |
6f8b71fedigeff10 years ago | 20 | spawnedProcess: nodeChildProcess.ChildProcess; |
5b0582f3digeff10 years ago | 21 | stdin: NodeJS.WritableStream; |
| 22 | stdout: NodeJS.ReadableStream; | |
| 23 | stderr: NodeJS.ReadableStream; | |
ce5e88eeYuri Skorokhodov5 years ago | 24 | outcome: Promise<void>; |
45944d15Meena Kunnathur Balakrishnan10 years ago | 25 | } |
| 26 | | |
3fb37ad5unknown10 years ago | 27 | interface IExecOptions { |
| 28 | cwd?: string; | |
| 29 | stdio?: any; | |
| 30 | env?: any; | |
| 31 | encoding?: string; | |
| 32 | timeout?: number; | |
| 33 | maxBuffer?: number; | |
| 34 | killSignal?: string; | |
| 35 | } | |
| 36 | | |
| 37 | interface ISpawnOptions { | |
| 38 | cwd?: string; | |
| 39 | stdio?: any; | |
| 40 | env?: any; | |
| 41 | detached?: boolean; | |
77e86943lexie0111 years ago | 42 | shell?: boolean; |
3fb37ad5unknown10 years ago | 43 | } |
| 44 | | |
| 45 | export class ChildProcess { | |
88246fe9Meena Kunnathur Balakrishnan10 years ago | 46 | public static ERROR_TIMEOUT_MILLISECONDS = 300; |
6f8b71fedigeff10 years ago | 47 | private childProcess: typeof nodeChildProcess; |
aab2095edigeff10 years ago | 48 | |
ce5e88eeYuri Skorokhodov5 years ago | 49 | constructor({ childProcess = nodeChildProcess } = {}) { |
aab2095edigeff10 years ago | 50 | this.childProcess = childProcess; |
| 51 | } | |
| 52 | | |
ce5e88eeYuri Skorokhodov5 years ago | 53 | public exec(command: string, options: IExecOptions = {}): Promise<IExecResult> { |
| 54 | let outcome: Promise<string>; | |
| 55 | let process: nodeChildProcess.ChildProcess; | |
34472878RedMickey5 years ago | 56 | return new Promise<IExecResult>(resolveRes => { |
ce5e88eeYuri Skorokhodov5 years ago | 57 | outcome = new Promise<string>((resolve, reject) => { |
34472878RedMickey5 years ago | 58 | process = this.childProcess.exec( |
| 59 | command, | |
| 60 | options, | |
| 61 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
176f99c8ConnorQi013 months ago | 62 | ( |
| 63 | error: nodeChildProcess.ExecException | null, | |
| 64 | stdout: string | Buffer, | |
| 65 | stderr: string | Buffer, | |
| 66 | ) => { | |
ce5e88eeYuri Skorokhodov5 years ago | 67 | if (error) { |
34472878RedMickey5 years ago | 68 | reject( |
| 69 | ErrorHelper.getNestedError( | |
| 70 | error, | |
| 71 | InternalErrorCode.CommandFailed, | |
| 72 | command, | |
| 73 | ), | |
| 74 | ); | |
ce5e88eeYuri Skorokhodov5 years ago | 75 | } else { |
176f99c8ConnorQi013 months ago | 76 | resolve(stdout.toString()); |
ce5e88eeYuri Skorokhodov5 years ago | 77 | } |
34472878RedMickey5 years ago | 78 | }, |
| 79 | ); | |
| 80 | }); | |
09f6024fHeniker4 years ago | 81 | resolveRes({ process, outcome }); |
3fb37ad5unknown10 years ago | 82 | }); |
| 83 | } | |
| 84 | | |
0d77292aJiglioNero4 years ago | 85 | public async execToString(command: string, options: IExecOptions = {}): Promise<string> { |
| 86 | const execResult = await this.exec(command, options); | |
| 87 | const stdout = await execResult.outcome; | |
| 88 | return stdout.toString(); | |
3fb37ad5unknown10 years ago | 89 | } |
| 90 | | |
34472878RedMickey5 years ago | 91 | public execFileSync( |
| 92 | command: string, | |
| 93 | args: string[] = [], | |
| 94 | options: IExecOptions = {}, | |
| 95 | ): Buffer | string { | |
b3753d4eRedMickey6 years ago | 96 | return this.childProcess.execFileSync(command, args, options); |
| 97 | } | |
| 98 | | |
34472878RedMickey5 years ago | 99 | public spawn( |
| 100 | command: string, | |
| 101 | args: string[] = [], | |
| 102 | options: ISpawnOptions = {}, | |
| 103 | showStdOutputsOnError: boolean = false, | |
| 104 | ): ISpawnResult { | |
77e86943lexie0111 years ago | 105 | const spawnedProcess = this.childProcess.spawn( |
| 106 | command, | |
| 107 | args, | |
| 108 | Object.assign({}, options, { shell: true }), | |
| 109 | ); | |
09f6024fHeniker4 years ago | 110 | const outcome: Promise<void> = new Promise((resolve, reject) => { |
ce5e88eeYuri Skorokhodov5 years ago | 111 | spawnedProcess.once("error", (error: any) => { |
| 112 | reject(error); | |
| 113 | }); | |
| 114 | | |
dd8375caJiglioNero5 years ago | 115 | const stderrChunks: string[] = []; |
| 116 | const stdoutChunks: string[] = []; | |
| 117 | | |
34472878RedMickey5 years ago | 118 | spawnedProcess.stderr.on("data", data => { |
dd8375caJiglioNero5 years ago | 119 | stderrChunks.push(data.toString()); |
| 120 | }); | |
| 121 | | |
34472878RedMickey5 years ago | 122 | spawnedProcess.stdout.on("data", data => { |
dd8375caJiglioNero5 years ago | 123 | stdoutChunks.push(data.toString()); |
| 124 | }); | |
| 125 | | |
ce5e88eeYuri Skorokhodov5 years ago | 126 | spawnedProcess.once("exit", (code: number) => { |
| 127 | if (code === 0) { | |
| 128 | resolve(); | |
| 129 | } else { | |
09f6024fHeniker4 years ago | 130 | const commandWithArgs = `${command} ${args.join(" ")}`; |
dd8375caJiglioNero5 years ago | 131 | if (showStdOutputsOnError) { |
| 132 | let details = ""; | |
| 133 | if (stdoutChunks.length > 0) { | |
34472878RedMickey5 years ago | 134 | details = details.concat( |
| 135 | `\n\tSTDOUT: ${stdoutChunks[stdoutChunks.length - 1]}`, | |
| 136 | ); | |
dd8375caJiglioNero5 years ago | 137 | } |
| 138 | if (stderrChunks.length > 0) { | |
| 139 | details = details.concat(`\n\tSTDERR: ${stderrChunks.join("\n\t")}`); | |
| 140 | } | |
| 141 | if (details === "") { | |
| 142 | details = "STDOUT and STDERR are empty!"; | |
| 143 | } | |
34472878RedMickey5 years ago | 144 | reject( |
| 145 | ErrorHelper.getInternalError( | |
| 146 | InternalErrorCode.CommandFailedWithDetails, | |
| 147 | commandWithArgs, | |
| 148 | details, | |
| 149 | ), | |
| 150 | ); | |
| 151 | } else { | |
| 152 | reject( | |
| 153 | ErrorHelper.getInternalError( | |
| 154 | InternalErrorCode.CommandFailed, | |
| 155 | commandWithArgs, | |
| 156 | code, | |
| 157 | ), | |
| 158 | ); | |
dd8375caJiglioNero5 years ago | 159 | } |
ce5e88eeYuri Skorokhodov5 years ago | 160 | } |
| 161 | }); | |
3fb37ad5unknown10 years ago | 162 | }); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 163 | return { |
09f6024fHeniker4 years ago | 164 | spawnedProcess, |
ce5e88eeYuri Skorokhodov5 years ago | 165 | stdin: spawnedProcess.stdin, |
| 166 | stdout: spawnedProcess.stdout, | |
| 167 | stderr: spawnedProcess.stderr, | |
09f6024fHeniker4 years ago | 168 | outcome, |
34472878RedMickey5 years ago | 169 | }; |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 170 | } |
4bb0956eRedMickey5 years ago | 171 | |
| 172 | // Kills any orphaned Instruments processes belonging to the user. | |
| 173 | // | |
| 174 | // In some cases, we've seen interactions between Instruments and the iOS | |
| 175 | // simulator that cause hung instruments and DTServiceHub processes. If | |
| 176 | // enough instances pile up, the host machine eventually becomes | |
| 177 | // unresponsive. Until the underlying issue is resolved, manually kill any | |
| 178 | // orphaned instances (where the parent process has died and PPID is 1) | |
| 179 | // before launching another instruments run. | |
| 180 | public async killOrphanedInstrumentsProcesses(): Promise<void> { | |
| 181 | const result = await this.execToString("ps -e -o user,ppid,pid,comm"); | |
| 182 | if (result) { | |
| 183 | result | |
| 184 | .split("\n") | |
| 185 | .filter(notNullOrUndefined) | |
| 186 | .map(a => /^(\S+)\s+1\s+(\d+)\s+(.+)$/.exec(a)) | |
| 187 | .filter(notNullOrUndefined) | |
| 188 | .filter(m => m[1] === process.env.USER) | |
| 189 | .filter( | |
| 190 | m => | |
| 191 | m[3] && ["/instruments", "/DTServiceHub"].some(name => m[3].endsWith(name)), | |
| 192 | ) | |
| 193 | .forEach(m => { | |
| 194 | const pid = m[2]; | |
| 195 | console.debug(`Killing orphaned Instruments process: ${pid}`); | |
| 196 | kill(parseInt(pid, 10), "SIGKILL"); | |
| 197 | }); | |
| 198 | } | |
| 199 | } | |
3fb37ad5unknown10 years ago | 200 | } |