Add proper process teminate method

This commit is contained in:
Edwin Cheng 2019-04-02 01:11:22 +08:00
parent c894a3e19b
commit b84d0fc1a3
6 changed files with 116 additions and 10 deletions

View file

@ -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);
}
}

View file

@ -1,4 +1,3 @@
import * as timers from 'timers';
import * as vscode from 'vscode';
const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];

View file

@ -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;
}
}

View file

@ -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