From b84d0fc1a307f6103ea2c2620a106db821696434 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 2 Apr 2019 01:11:22 +0800 Subject: Add proper process teminate method --- editors/code/src/commands/cargo_watch.ts | 17 +++++++------ editors/code/src/commands/watch_status.ts | 1 - editors/code/src/utils/processes.ts | 40 ++++++++++++++++++++++++++++++ editors/code/src/utils/terminateProcess.sh | 12 +++++++++ 4 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 editors/code/src/utils/processes.ts create mode 100644 editors/code/src/utils/terminateProcess.sh (limited to 'editors/code/src') diff --git a/editors/code/src/commands/cargo_watch.ts b/editors/code/src/commands/cargo_watch.ts index 037f1e302..e51cac78a 100644 --- a/editors/code/src/commands/cargo_watch.ts +++ b/editors/code/src/commands/cargo_watch.ts @@ -1,9 +1,10 @@ import * as child_process from 'child_process'; import * as path from 'path'; -import * as timers from 'timers'; import * as vscode from 'vscode'; +import { terminate } from '../utils/processes'; import { StatusDisplay } from './watch_status'; + export class CargoWatchProvider { private diagnosticCollection?: vscode.DiagnosticCollection; private cargoProcess?: child_process.ChildProcess; @@ -21,24 +22,25 @@ export class CargoWatchProvider { // Start the cargo watch with json message this.cargoProcess = child_process.spawn( 'cargo', - ['watch', '-x', '"check --message-format json"'], + ['watch', '-x', '\"check --message-format json\"'], { - // stdio: ['ignore', 'pipe', 'ignore'], - shell: true, - cwd: vscode.workspace.rootPath + stdio: ['ignore', 'pipe', 'pipe'], + cwd: vscode.workspace.rootPath, + windowsVerbatimArguments: true, } ); this.cargoProcess.stdout.on('data', (s: string) => { this.processOutput(s); + console.log(s); }); this.cargoProcess.stderr.on('data', (s: string) => { - // console.error('Error on cargo watch : ' + s); + console.error('Error on cargo watch : ' + s); }); this.cargoProcess.on('error', (err: Error) => { - // console.error('Error on spawn cargo process : ' + err); + console.error('Error on spawn cargo process : ' + err); }); } @@ -50,6 +52,7 @@ export class CargoWatchProvider { if (this.cargoProcess) { this.cargoProcess.kill(); + terminate(this.cargoProcess); } } diff --git a/editors/code/src/commands/watch_status.ts b/editors/code/src/commands/watch_status.ts index 1b0611ce3..f027d7bbc 100644 --- a/editors/code/src/commands/watch_status.ts +++ b/editors/code/src/commands/watch_status.ts @@ -1,4 +1,3 @@ -import * as timers from 'timers'; import * as vscode from 'vscode'; const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']; diff --git a/editors/code/src/utils/processes.ts b/editors/code/src/utils/processes.ts new file mode 100644 index 000000000..09fdf6e24 --- /dev/null +++ b/editors/code/src/utils/processes.ts @@ -0,0 +1,40 @@ +'use strict'; + +import * as cp from 'child_process'; +import ChildProcess = cp.ChildProcess; + +import { join } from 'path'; + +const isWindows = (process.platform === 'win32'); +const isMacintosh = (process.platform === 'darwin'); +const isLinux = (process.platform === 'linux'); +export function terminate(process: ChildProcess, cwd?: string): boolean { + if (isWindows) { + try { + // This we run in Atom execFileSync is available. + // Ignore stderr since this is otherwise piped to parent.stderr + // which might be already closed. + const options: any = { + stdio: ['pipe', 'pipe', 'ignore'] + }; + if (cwd) { + options.cwd = cwd + } + (cp).execFileSync('taskkill', ['/T', '/F', '/PID', process.pid.toString()], options); + return true; + } catch (err) { + return false; + } + } else if (isLinux || isMacintosh) { + try { + const cmd = join(__dirname, 'terminateProcess.sh'); + const result = cp.spawnSync(cmd, [process.pid.toString()]); + return result.error ? false : true; + } catch (err) { + return false; + } + } else { + process.kill('SIGKILL'); + return true; + } +} \ No newline at end of file diff --git a/editors/code/src/utils/terminateProcess.sh b/editors/code/src/utils/terminateProcess.sh new file mode 100644 index 000000000..2ec9e1c2e --- /dev/null +++ b/editors/code/src/utils/terminateProcess.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +terminateTree() { + for cpid in $(pgrep -P $1); do + terminateTree $cpid + done + kill -9 $1 > /dev/null 2>&1 +} + +for pid in $*; do + terminateTree $pid +done \ No newline at end of file -- cgit v1.2.3