microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/childProcess.ts
196lines · 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 | |
| 62 | (error: Error, stdout: string, stderr: string) => { | |
ce5e88eeYuri Skorokhodov5 years ago | 63 | if (error) { |
34472878RedMickey5 years ago | 64 | reject( |
| 65 | ErrorHelper.getNestedError( | |
| 66 | error, | |
| 67 | InternalErrorCode.CommandFailed, | |
| 68 | command, | |
| 69 | ), | |
| 70 | ); | |
ce5e88eeYuri Skorokhodov5 years ago | 71 | } else { |
| 72 | resolve(stdout); | |
| 73 | } | |
34472878RedMickey5 years ago | 74 | }, |
| 75 | ); | |
| 76 | }); | |
09f6024fHeniker4 years ago | 77 | resolveRes({ process, outcome }); |
3fb37ad5unknown10 years ago | 78 | }); |
| 79 | } | |
| 80 | | |
0d77292aJiglioNero4 years ago | 81 | public async execToString(command: string, options: IExecOptions = {}): Promise<string> { |
| 82 | const execResult = await this.exec(command, options); | |
| 83 | const stdout = await execResult.outcome; | |
| 84 | return stdout.toString(); | |
3fb37ad5unknown10 years ago | 85 | } |
| 86 | | |
34472878RedMickey5 years ago | 87 | public execFileSync( |
| 88 | command: string, | |
| 89 | args: string[] = [], | |
| 90 | options: IExecOptions = {}, | |
| 91 | ): Buffer | string { | |
b3753d4eRedMickey6 years ago | 92 | return this.childProcess.execFileSync(command, args, options); |
| 93 | } | |
| 94 | | |
34472878RedMickey5 years ago | 95 | public spawn( |
| 96 | command: string, | |
| 97 | args: string[] = [], | |
| 98 | options: ISpawnOptions = {}, | |
| 99 | showStdOutputsOnError: boolean = false, | |
| 100 | ): ISpawnResult { | |
77e86943lexie0111 years ago | 101 | const spawnedProcess = this.childProcess.spawn( |
| 102 | command, | |
| 103 | args, | |
| 104 | Object.assign({}, options, { shell: true }), | |
| 105 | ); | |
09f6024fHeniker4 years ago | 106 | const outcome: Promise<void> = new Promise((resolve, reject) => { |
ce5e88eeYuri Skorokhodov5 years ago | 107 | spawnedProcess.once("error", (error: any) => { |
| 108 | reject(error); | |
| 109 | }); | |
| 110 | | |
dd8375caJiglioNero5 years ago | 111 | const stderrChunks: string[] = []; |
| 112 | const stdoutChunks: string[] = []; | |
| 113 | | |
34472878RedMickey5 years ago | 114 | spawnedProcess.stderr.on("data", data => { |
dd8375caJiglioNero5 years ago | 115 | stderrChunks.push(data.toString()); |
| 116 | }); | |
| 117 | | |
34472878RedMickey5 years ago | 118 | spawnedProcess.stdout.on("data", data => { |
dd8375caJiglioNero5 years ago | 119 | stdoutChunks.push(data.toString()); |
| 120 | }); | |
| 121 | | |
ce5e88eeYuri Skorokhodov5 years ago | 122 | spawnedProcess.once("exit", (code: number) => { |
| 123 | if (code === 0) { | |
| 124 | resolve(); | |
| 125 | } else { | |
09f6024fHeniker4 years ago | 126 | const commandWithArgs = `${command} ${args.join(" ")}`; |
dd8375caJiglioNero5 years ago | 127 | if (showStdOutputsOnError) { |
| 128 | let details = ""; | |
| 129 | if (stdoutChunks.length > 0) { | |
34472878RedMickey5 years ago | 130 | details = details.concat( |
| 131 | `\n\tSTDOUT: ${stdoutChunks[stdoutChunks.length - 1]}`, | |
| 132 | ); | |
dd8375caJiglioNero5 years ago | 133 | } |
| 134 | if (stderrChunks.length > 0) { | |
| 135 | details = details.concat(`\n\tSTDERR: ${stderrChunks.join("\n\t")}`); | |
| 136 | } | |
| 137 | if (details === "") { | |
| 138 | details = "STDOUT and STDERR are empty!"; | |
| 139 | } | |
34472878RedMickey5 years ago | 140 | reject( |
| 141 | ErrorHelper.getInternalError( | |
| 142 | InternalErrorCode.CommandFailedWithDetails, | |
| 143 | commandWithArgs, | |
| 144 | details, | |
| 145 | ), | |
| 146 | ); | |
| 147 | } else { | |
| 148 | reject( | |
| 149 | ErrorHelper.getInternalError( | |
| 150 | InternalErrorCode.CommandFailed, | |
| 151 | commandWithArgs, | |
| 152 | code, | |
| 153 | ), | |
| 154 | ); | |
dd8375caJiglioNero5 years ago | 155 | } |
ce5e88eeYuri Skorokhodov5 years ago | 156 | } |
| 157 | }); | |
3fb37ad5unknown10 years ago | 158 | }); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 159 | return { |
09f6024fHeniker4 years ago | 160 | spawnedProcess, |
ce5e88eeYuri Skorokhodov5 years ago | 161 | stdin: spawnedProcess.stdin, |
| 162 | stdout: spawnedProcess.stdout, | |
| 163 | stderr: spawnedProcess.stderr, | |
09f6024fHeniker4 years ago | 164 | outcome, |
34472878RedMickey5 years ago | 165 | }; |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 166 | } |
4bb0956eRedMickey5 years ago | 167 | |
| 168 | // Kills any orphaned Instruments processes belonging to the user. | |
| 169 | // | |
| 170 | // In some cases, we've seen interactions between Instruments and the iOS | |
| 171 | // simulator that cause hung instruments and DTServiceHub processes. If | |
| 172 | // enough instances pile up, the host machine eventually becomes | |
| 173 | // unresponsive. Until the underlying issue is resolved, manually kill any | |
| 174 | // orphaned instances (where the parent process has died and PPID is 1) | |
| 175 | // before launching another instruments run. | |
| 176 | public async killOrphanedInstrumentsProcesses(): Promise<void> { | |
| 177 | const result = await this.execToString("ps -e -o user,ppid,pid,comm"); | |
| 178 | if (result) { | |
| 179 | result | |
| 180 | .split("\n") | |
| 181 | .filter(notNullOrUndefined) | |
| 182 | .map(a => /^(\S+)\s+1\s+(\d+)\s+(.+)$/.exec(a)) | |
| 183 | .filter(notNullOrUndefined) | |
| 184 | .filter(m => m[1] === process.env.USER) | |
| 185 | .filter( | |
| 186 | m => | |
| 187 | m[3] && ["/instruments", "/DTServiceHub"].some(name => m[3].endsWith(name)), | |
| 188 | ) | |
| 189 | .forEach(m => { | |
| 190 | const pid = m[2]; | |
| 191 | console.debug(`Killing orphaned Instruments process: ${pid}`); | |
| 192 | kill(parseInt(pid, 10), "SIGKILL"); | |
| 193 | }); | |
| 194 | } | |
| 195 | } | |
3fb37ad5unknown10 years ago | 196 | } |