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