forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproc.ts
More file actions
78 lines (69 loc) · 2.77 KB
/
proc.ts
File metadata and controls
78 lines (69 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { EventEmitter } from 'events';
import { traceError } from '../../logging';
import { IDisposable } from '../types';
import { EnvironmentVariables } from '../variables/types';
import { execObservable, killPid, plainExec, shellExec } from './rawProcessApis';
import { ExecutionResult, IProcessService, ObservableExecutionResult, ShellOptions, SpawnOptions } from './types';
import { workerPlainExec, workerShellExec } from './worker/rawProcessApiWrapper';
export class ProcessService extends EventEmitter implements IProcessService {
private processesToKill = new Set<IDisposable>();
constructor(private readonly env?: EnvironmentVariables) {
super();
}
public static isAlive(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
public static kill(pid: number): void {
killPid(pid);
}
public dispose(): void {
this.removeAllListeners();
this.processesToKill.forEach((p) => {
try {
p.dispose();
} catch {
// ignore.
}
});
}
public execObservable(file: string, args: string[], options: SpawnOptions = {}): ObservableExecutionResult<string> {
const execOptions = { ...options, doNotLog: true };
const result = execObservable(file, args, execOptions, this.env, this.processesToKill);
this.emit('exec', file, args, options);
return result;
}
public exec(file: string, args: string[], options: SpawnOptions = {}): Promise<ExecutionResult<string>> {
this.emit('exec', file, args, options);
if (options.useWorker) {
return workerPlainExec(file, args, options);
}
const execOptions = { ...options, doNotLog: true };
const promise = plainExec(file, args, execOptions, this.env, this.processesToKill);
return promise;
}
public shellExec(command: string, options: ShellOptions = {}): Promise<ExecutionResult<string>> {
this.emit('exec', command, undefined, options);
if (options.useWorker) {
return workerShellExec(command, options);
}
const disposables = new Set<IDisposable>();
const shellOptions = { ...options, doNotLog: true };
return shellExec(command, shellOptions, this.env, disposables).finally(() => {
// Ensure the process we started is cleaned up.
disposables.forEach((p) => {
try {
p.dispose();
} catch {
traceError(`Unable to kill process for ${command}`);
}
});
});
}
}