|
1 | 1 |
|
| 2 | +import * as os from 'os'; |
2 | 3 | import * as vscode from 'vscode'; |
3 | 4 | import * as util from '../common'; |
4 | 5 | import { sleep } from '../Utility/Async/sleep'; |
5 | | -import { PsProcessParser } from './nativeAttach'; |
| 6 | +import { CimAttachItemsProvider, PsAttachItemsProvider, WmicAttachItemsProvider } from './nativeAttach'; |
6 | 7 |
|
7 | | -export class AttachWaitFor { |
| 8 | +export interface WaitForProcessProvider { |
| 9 | + poll(program: string, timeout: number, interval: number, token?: vscode.CancellationToken): Promise<string | undefined> |
| 10 | +} |
| 11 | + |
| 12 | +export class PollProcessProviderFactory { |
| 13 | + static Get(): WaitForProcessProvider { |
| 14 | + if (os.platform() === 'win32') { |
| 15 | + const pwsh: string | undefined = util.findPowerShell(); |
| 16 | + let itemsProvider = pwsh ? new CimAttachItemsProvider(pwsh) : new WmicAttachItemsProvider(); |
| 17 | + return new PollWindowsProvider(itemsProvider); |
| 18 | + } else { |
| 19 | + // Linux and MacOS |
| 20 | + return new PollProcProvider(new PsAttachItemsProvider()); |
| 21 | + } |
| 22 | + } |
| 23 | +} |
8 | 24 |
|
9 | | - constructor() { |
| 25 | +export class PollProcProvider implements WaitForProcessProvider { |
| 26 | + |
| 27 | + constructor(itemsProvider: PsAttachItemsProvider) { |
| 28 | + this.itemsProvider = itemsProvider; |
10 | 29 | this._channel = vscode.window.createOutputChannel('waitfor-attach'); |
11 | | - this.timeout = 30000 |
12 | 30 | } |
13 | 31 |
|
14 | 32 | private _channel: vscode.OutputChannel; |
15 | | - private timeout: number; |
| 33 | + private itemsProvider: PsAttachItemsProvider; |
16 | 34 |
|
17 | | - public async WaitForProcess(program: string, timeout: number): Promise<string | undefined> { |
18 | | - if (timeout) { |
19 | | - this.timeout = timeout |
20 | | - } |
| 35 | + async poll(program: string, timeout: number, interval: number, token?: vscode.CancellationToken): Promise<string | undefined> { |
| 36 | + return new Promise<string | undefined>(async (resolve, reject) => { |
| 37 | + const startTime = Date.now(); // Get the current time in milliseconds |
| 38 | + let process: string | undefined; |
| 39 | + while (true) { |
| 40 | + let elapsedTime = Date.now() - startTime; |
| 41 | + if (elapsedTime >= timeout) { |
| 42 | + reject(new Error('Timeout reached. No process matched the pattern.')); |
| 43 | + } |
21 | 44 |
|
22 | | - return await this.poll(program) |
23 | | - } |
| 45 | + if (token?.isCancellationRequested) { |
| 46 | + reject(new Error('Operation cancelled.')); |
| 47 | + } |
24 | 48 |
|
25 | | - //Naive poll mechanism, parses /proc for a while till a match is found |
26 | | - private async poll(program: string): Promise<string | undefined> { |
27 | | - this._channel.clear() |
28 | | - const startTime = Date.now(); // Get the current time in milliseconds |
29 | | - let seen = new Set<string>(); |
30 | | - let process: string | undefined; |
31 | | - while (true) { |
32 | | - const elapsedTime = Date.now() - startTime; |
33 | | - |
34 | | - if (elapsedTime >= this.timeout) { |
35 | | - console.log('Timeout reached. No process matched pattern.'); |
36 | | - return undefined |
| 49 | + let procs = await this.itemsProvider.getAttachItems(token) |
| 50 | + for (const proc of procs) { |
| 51 | + if (proc.detail?.includes(program)) { |
| 52 | + process = proc.id |
| 53 | + break |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (process) { |
| 58 | + await util.execChildProcess(`kill -STOP ${process}`, undefined, this._channel); |
| 59 | + resolve(process) |
| 60 | + } |
| 61 | + |
| 62 | + sleep(interval) |
37 | 63 | } |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
38 | 67 |
|
39 | | - const output: string = await util.execChildProcess(PsProcessParser.psLinuxCommand, undefined, this._channel) |
40 | | - const lines: string[] = output.split(/\r?\n/); |
41 | | - const processes: string[] = lines.slice(1); |
42 | | - const processAttach = PsProcessParser.ParseProcessFromPsArray(processes) |
43 | | - .sort((a, b) => { |
44 | | - if (a.name === undefined) { |
45 | | - if (b.name === undefined) { |
46 | | - return 0; |
47 | | - } |
48 | | - return 1; |
49 | | - } |
50 | | - if (b.name === undefined) { |
51 | | - return -1; |
52 | | - } |
53 | | - const aLower: string = a.name.toLowerCase(); |
54 | | - const bLower: string = b.name.toLowerCase(); |
55 | | - if (aLower === bLower) { |
56 | | - return 0; |
| 68 | +export class PollWindowsProvider implements WaitForProcessProvider { |
| 69 | + constructor(itemsProvider: CimAttachItemsProvider | WmicAttachItemsProvider) { |
| 70 | + this.itemsProvider = itemsProvider; |
| 71 | + } |
| 72 | + |
| 73 | + private itemsProvider: CimAttachItemsProvider | WmicAttachItemsProvider; |
| 74 | + |
| 75 | + public async poll(program: string, timeout: number, interval: number, token?: vscode.CancellationToken): Promise<string | undefined> { |
| 76 | + return new Promise<string | undefined>(async (resolve, reject) => { |
| 77 | + const startTime = Date.now(); // Get the current time in milliseconds |
| 78 | + let process: string | undefined; |
| 79 | + while (true) { |
| 80 | + const elapsedTime = Date.now() - startTime; |
| 81 | + if (elapsedTime >= timeout) { |
| 82 | + reject(new Error('Timeout reached. No process matched the pattern.')); |
| 83 | + } |
| 84 | + |
| 85 | + // Check for cancellation |
| 86 | + if (token?.isCancellationRequested) { |
| 87 | + reject(new Error('Operation cancelled.')); |
| 88 | + } |
| 89 | + |
| 90 | + let procs = await this.itemsProvider.getAttachItems(token) |
| 91 | + for (const proc of procs) { |
| 92 | + if (proc.detail?.includes(program)) { |
| 93 | + process = proc.id |
| 94 | + break |
57 | 95 | } |
58 | | - return aLower < bLower ? -1 : 1; |
59 | | - }) |
60 | | - .map(p => p.toAttachItem()); |
61 | | - processAttach.forEach(p => { |
62 | | - if (!process && p.detail!.includes(program)) { |
63 | | - console.log("Found program waiting for with pid %s - info %s", p.id!, p.detail!) |
64 | | - process = p.id! |
65 | | - |
66 | | - // Send sigstop by default? |
67 | | - util.execChildProcess(`kill -STOP ${process}`, undefined, this._channel) |
68 | | - return |
69 | 96 | } |
70 | 97 |
|
71 | | - if (seen.has(p.id!) == false && p.label != "ps" && !p.detail!.includes("ps")) { |
72 | | - seen.add(p.id!) |
| 98 | + if (process) { |
| 99 | + // Use pssupend to send SIGSTOP analogous in Windows |
| 100 | + await util.execChildProcess(`pssuspend.exe /accepteula -nobanner ${process}`, undefined, undefined) |
| 101 | + resolve(process) |
73 | 102 | } |
74 | | - }) |
75 | 103 |
|
76 | | - if (process) { |
77 | | - return process |
| 104 | + sleep(interval) |
78 | 105 | } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
79 | 109 |
|
80 | | - sleep(200) |
81 | | - } |
| 110 | + |
| 111 | +export class AttachWaitFor { |
| 112 | + constructor(private poller: WaitForProcessProvider) { |
| 113 | + //this._channel = vscode.window.createOutputChannel('waitfor-attach'); |
| 114 | + this.timeout = 30000 |
82 | 115 | } |
83 | 116 |
|
| 117 | + private timeout: number; |
| 118 | + |
| 119 | + public async WaitForProcess(program: string, timeout: number, interval: number, token?: vscode.CancellationToken): Promise<string | undefined> { |
| 120 | + if (timeout) { |
| 121 | + this.timeout = timeout |
| 122 | + } |
| 123 | + return await this.poller.poll(program, this.timeout, interval, token); |
| 124 | + } |
84 | 125 | } |
0 commit comments