|
1 | | -import { ChildProcess, spawn as spawnCallback, execFile as execFileCallback} from "child_process"; |
2 | | -import { Logger } from "../logger/logger"; |
| 1 | +import { exec, execFile} from "child_process"; |
3 | 2 | import { platform } from "os"; |
4 | 3 | import { promisify } from "util"; |
| 4 | +import { logger } from "../extension"; |
| 5 | +import { |
| 6 | + ProgressLocation, |
| 7 | + window |
| 8 | +} from "vscode"; |
| 9 | + |
| 10 | +const execPromisify = promisify(exec); |
| 11 | +const execFilePromisify = promisify(execFile); |
5 | 12 |
|
6 | | -const spawn = promisify(spawnCallback); |
7 | | -const execFile = promisify(execFileCallback); |
8 | 13 |
|
9 | 14 | export class ApamaRunner { |
10 | 15 | stdout = ""; |
11 | 16 | stderr = ""; |
12 | 17 |
|
13 | 18 | constructor( |
| 19 | + /** Display name for this process */ |
14 | 20 | public name: string, |
| 21 | + /** e.g. [XXX/apama_env, apama_project] or [XXX/apama_project] */ |
15 | 22 | public command: string[], |
16 | 23 | ) {} |
17 | 24 |
|
18 | 25 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
19 | 26 | async run(workingDir: string, args: string[]): Promise<any> { |
20 | | - //if fails returns promise.reject including err |
21 | | - if (platform() === "win32") { |
22 | | - return await spawn( |
23 | | - this.command[0], |
24 | | - [...args.slice(1), ...args], |
25 | | - {cwd: workingDir} |
26 | | - ) |
27 | | - } else { |
28 | | - return await execFile( |
29 | | - this.command[0], |
30 | | - [...this.command.slice(1), ...args], |
31 | | - {cwd: workingDir} |
32 | | - ) |
33 | | - } |
34 | | - } |
35 | | -} |
36 | | - |
37 | | -export class ApamaAsyncRunner { |
38 | | - stdout = ""; |
39 | | - stderr = ""; |
40 | | - child?: ChildProcess; |
41 | | - |
42 | | - constructor( |
43 | | - public name: string, |
44 | | - public command: string, |
45 | | - private logger: Logger, |
46 | | - ) {} |
47 | | - |
48 | | - // |
49 | | - // If you call this withShell = true it will run the command under that shell |
50 | | - // this means the process may need to be managed separately as it may get detached |
51 | | - // when you kill the parent (correlator behaves that way) |
52 | | - // I use engine_management to control the running correlator |
53 | | - // |
54 | | - // TODO: pipes configuration might be worth passing as an argument |
55 | | - // |
56 | | - public start( |
57 | | - args: string[], |
58 | | - withShell: boolean, |
59 | | - defaultHandlers: boolean, |
60 | | - ): ChildProcess { |
61 | | - //N.B. this potentially will leave the correlator running - future work required... |
62 | | - if (this.child && !this.child.killed) { |
63 | | - this.logger.appendLine(this.name + " already started, stopping..."); |
64 | | - this.child.kill("SIGKILL"); |
65 | | - } |
66 | | - |
67 | | - this.logger.appendLine("Starting " + this.name); |
68 | | - this.child = spawnCallback(this.command[0], [...this.command.slice(1), ...args], { |
69 | | - shell: withShell, |
70 | | - stdio: ["pipe", "pipe", "pipe"], |
71 | | - }); |
72 | | - |
73 | | - //Running with process Id |
74 | | - this.logger.appendLine(this.name + " started, PID:" + this.child.pid); |
75 | | - |
76 | | - //Notify the logger if it stopped.... |
77 | | - this.child.once("exit", (exitCode) => |
78 | | - this.logger.appendLine(this.name + " stopped, exit code: " + exitCode), |
79 | | - ); |
80 | | - |
81 | | - if (defaultHandlers) { |
82 | | - this.child.stdout?.setEncoding("utf8"); |
83 | | - this.child.stdout?.on("data", (data: string) => { |
84 | | - if (this.logger) { |
85 | | - this.logger.appendLine(data); |
86 | | - } |
87 | | - }); |
88 | | - } |
89 | | - |
90 | | - return this.child; |
91 | | - } |
92 | | - |
93 | | - public stop(): Promise<void> { |
94 | | - return new Promise((resolve) => { |
95 | | - if (this.child && !this.child.killed) { |
96 | | - this.child.once("exit", () => { |
97 | | - resolve(); |
98 | | - }); |
99 | | - |
100 | | - this.logger.appendLine("Process " + this.name + " stopping..."); |
101 | | - this.child.kill("SIGINT"); |
102 | | - const attemptedToKill = this.child; |
103 | | - setTimeout(() => { |
104 | | - if (!attemptedToKill.killed) { |
105 | | - this.logger.appendLine( |
106 | | - "Failed to stop shell in 5 seconds, killing...", |
| 27 | + return await window.withProgress( |
| 28 | + { |
| 29 | + // since sometimes (e.g. apama_project) the command takes a long time, giving a progress notification is helpful |
| 30 | + location: ProgressLocation.Notification, |
| 31 | + title: `Running ${this.name}...`, |
| 32 | + cancellable: false, |
| 33 | + }, |
| 34 | + async () => |
| 35 | + { |
| 36 | + try { |
| 37 | + if (platform() === "win32") { |
| 38 | + logger.debug("Running command: " + this.command[0] + " " + [...this.command.slice(1), ...args].join(" ")); |
| 39 | + return await execPromisify([...this.command, ...args].join(" "), { cwd: workingDir }); |
| 40 | + } else { |
| 41 | + return await execFilePromisify( |
| 42 | + this.command[0], |
| 43 | + [...this.command.slice(1), ...args], |
| 44 | + { cwd: workingDir } |
107 | 45 | ); |
108 | | - attemptedToKill.kill("SIGKILL"); |
109 | 46 | } |
110 | | - }, 5000); |
111 | | - } else { |
112 | | - resolve(); |
| 47 | + } catch (error) { |
| 48 | + const enhancedError = new Error( |
| 49 | + `Error running command '${this.command[0]} ${[...this.command.slice(1), ...args].join(" ")}': ${error}` |
| 50 | + ); |
| 51 | + if (error instanceof Error) enhancedError.stack = error.stack; |
| 52 | + throw enhancedError; |
| 53 | + } |
113 | 54 | } |
114 | | - }); |
| 55 | + ); |
115 | 56 | } |
116 | 57 | } |
| 58 | + |
0 commit comments