From 3333d2a10cb0ca52b60bad787115962cd6cbd832 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Thu, 7 Dec 2023 06:43:03 +0000 Subject: [PATCH 1/6] Kill flow subthread on worker exit --- FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts | 41 +++++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts b/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts index 9d2e6d5..0b08f19 100644 --- a/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts +++ b/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts @@ -241,6 +241,30 @@ class CLI { } }; + // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/explicit-module-boundary-types + killThread = (thread:any): void => { + const killArray = [ + 'SIGKILL', + 'SIGHUP', + 'SIGTERM', + 'SIGINT', + ]; + + try { + thread.kill(); + } catch (err) { + // err + } + + killArray.forEach((com: string) => { + try { + thread.kill(com); + } catch (err) { + // err + } + }); + } + runCli = async (): Promise<{ cliExitCode: number, errorLogFull: string[], @@ -249,7 +273,6 @@ class CLI { const errorLogFull: string[] = []; - // eslint-disable-next-line no-console this.config.jobLog(`Running ${this.config.cli} ${this.config.spawnArgs.join(' ')}`); const cliExitCode: number = await new Promise((resolve) => { try { @@ -257,16 +280,26 @@ class CLI { const spawnArgs = this.config.spawnArgs.map((row) => row.trim()).filter((row) => row !== ''); const thread = childProcess.spawn(this.config.cli, spawnArgs, opts); + process.on('exit', () => { + try { + // eslint-disable-next-line no-console + console.log('Main thread exiting, cleaning up running CLI'); + this.killThread(thread); + } catch (err) { + // eslint-disable-next-line no-console + console.log('Error running cliUtils on Exit function'); + // eslint-disable-next-line no-console + console.log(err); + } + }); + thread.stdout.on('data', (data: string) => { - // eslint-disable-next-line no-console - // console.log(data.toString()); errorLogFull.push(data.toString()); this.parseOutput(data); }); thread.stderr.on('data', (data: string) => { // eslint-disable-next-line no-console - // console.log(data.toString()); errorLogFull.push(data.toString()); this.parseOutput(data); }); From 167700352740a65469f3bee5c7616b783ad88c51 Mon Sep 17 00:00:00 2001 From: HaveAGitGat Date: Thu, 7 Dec 2023 06:45:08 +0000 Subject: [PATCH 2/6] Apply auto-build changes --- FlowPlugins/FlowHelpers/1.0.0/cliUtils.js | 50 +++++++++++++++++++---- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/FlowPlugins/FlowHelpers/1.0.0/cliUtils.js b/FlowPlugins/FlowHelpers/1.0.0/cliUtils.js index 17f4464..a8f8ce8 100644 --- a/FlowPlugins/FlowHelpers/1.0.0/cliUtils.js +++ b/FlowPlugins/FlowHelpers/1.0.0/cliUtils.js @@ -224,6 +224,29 @@ var CLI = /** @class */ (function () { } } }; + // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/explicit-module-boundary-types + this.killThread = function (thread) { + var killArray = [ + 'SIGKILL', + 'SIGHUP', + 'SIGTERM', + 'SIGINT', + ]; + try { + thread.kill(); + } + catch (err) { + // err + } + killArray.forEach(function (com) { + try { + thread.kill(com); + } + catch (err) { + // err + } + }); + }; this.runCli = function () { return __awaiter(_this, void 0, void 0, function () { var childProcess, errorLogFull, cliExitCode; var _this = this; @@ -232,26 +255,35 @@ var CLI = /** @class */ (function () { case 0: childProcess = require('child_process'); errorLogFull = []; - // eslint-disable-next-line no-console this.config.jobLog("Running ".concat(this.config.cli, " ").concat(this.config.spawnArgs.join(' '))); return [4 /*yield*/, new Promise(function (resolve) { try { var opts = _this.config.spawnOpts || {}; var spawnArgs = _this.config.spawnArgs.map(function (row) { return row.trim(); }).filter(function (row) { return row !== ''; }); - var thread = childProcess.spawn(_this.config.cli, spawnArgs, opts); - thread.stdout.on('data', function (data) { - // eslint-disable-next-line no-console - // console.log(data.toString()); + var thread_1 = childProcess.spawn(_this.config.cli, spawnArgs, opts); + process.on('exit', function () { + try { + // eslint-disable-next-line no-console + console.log('Main thread exiting, cleaning up running CLI'); + _this.killThread(thread_1); + } + catch (err) { + // eslint-disable-next-line no-console + console.log('Error running cliUtils on Exit function'); + // eslint-disable-next-line no-console + console.log(err); + } + }); + thread_1.stdout.on('data', function (data) { errorLogFull.push(data.toString()); _this.parseOutput(data); }); - thread.stderr.on('data', function (data) { + thread_1.stderr.on('data', function (data) { // eslint-disable-next-line no-console - // console.log(data.toString()); errorLogFull.push(data.toString()); _this.parseOutput(data); }); - thread.on('error', function () { + thread_1.on('error', function () { // catches execution error (bad file) // eslint-disable-next-line no-console console.log(1, "Error executing binary: ".concat(_this.config.cli)); @@ -259,7 +291,7 @@ var CLI = /** @class */ (function () { }); // thread.stdout.pipe(process.stdout); // thread.stderr.pipe(process.stderr); - thread.on('close', function (code) { + thread_1.on('close', function (code) { if (code !== 0) { // eslint-disable-next-line no-console console.log(code, 'CLI error'); From d1ca9eeb9333e7e616fb0782b4c40780dd704efc Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Thu, 7 Dec 2023 06:49:21 +0000 Subject: [PATCH 3/6] Refactor thread kill --- FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts | 36 +++++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts b/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts index 0b08f19..925b498 100644 --- a/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts +++ b/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts @@ -274,24 +274,30 @@ class CLI { const errorLogFull: string[] = []; this.config.jobLog(`Running ${this.config.cli} ${this.config.spawnArgs.join(' ')}`); + + // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/explicit-module-boundary-types + let thread: any; + + process.on('exit', () => { + if (thread) { + try { + // eslint-disable-next-line no-console + console.log('Main thread exiting, cleaning up running CLI'); + this.killThread(thread); + } catch (err) { + // eslint-disable-next-line no-console + console.log('Error running cliUtils on Exit function'); + // eslint-disable-next-line no-console + console.log(err); + } + } + }); + const cliExitCode: number = await new Promise((resolve) => { try { const opts = this.config.spawnOpts || {}; const spawnArgs = this.config.spawnArgs.map((row) => row.trim()).filter((row) => row !== ''); - const thread = childProcess.spawn(this.config.cli, spawnArgs, opts); - - process.on('exit', () => { - try { - // eslint-disable-next-line no-console - console.log('Main thread exiting, cleaning up running CLI'); - this.killThread(thread); - } catch (err) { - // eslint-disable-next-line no-console - console.log('Error running cliUtils on Exit function'); - // eslint-disable-next-line no-console - console.log(err); - } - }); + thread = childProcess.spawn(this.config.cli, spawnArgs, opts); thread.stdout.on('data', (data: string) => { errorLogFull.push(data.toString()); @@ -328,6 +334,8 @@ class CLI { } }); + thread = undefined; + if (!this.config.logFullCliOutput) { this.config.jobLog(errorLogFull.slice(-1000).join('')); } From 40ed93c89b23271227c2ad6b6e26a612c9a07c38 Mon Sep 17 00:00:00 2001 From: HaveAGitGat Date: Thu, 7 Dec 2023 06:51:40 +0000 Subject: [PATCH 4/6] Apply auto-build changes --- FlowPlugins/FlowHelpers/1.0.0/cliUtils.js | 41 ++++++++++++----------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/FlowPlugins/FlowHelpers/1.0.0/cliUtils.js b/FlowPlugins/FlowHelpers/1.0.0/cliUtils.js index a8f8ce8..e5e0c92 100644 --- a/FlowPlugins/FlowHelpers/1.0.0/cliUtils.js +++ b/FlowPlugins/FlowHelpers/1.0.0/cliUtils.js @@ -248,7 +248,7 @@ var CLI = /** @class */ (function () { }); }; this.runCli = function () { return __awaiter(_this, void 0, void 0, function () { - var childProcess, errorLogFull, cliExitCode; + var childProcess, errorLogFull, thread, cliExitCode; var _this = this; return __generator(this, function (_a) { switch (_a.label) { @@ -256,34 +256,36 @@ var CLI = /** @class */ (function () { childProcess = require('child_process'); errorLogFull = []; this.config.jobLog("Running ".concat(this.config.cli, " ").concat(this.config.spawnArgs.join(' '))); + process.on('exit', function () { + if (thread) { + try { + // eslint-disable-next-line no-console + console.log('Main thread exiting, cleaning up running CLI'); + _this.killThread(thread); + } + catch (err) { + // eslint-disable-next-line no-console + console.log('Error running cliUtils on Exit function'); + // eslint-disable-next-line no-console + console.log(err); + } + } + }); return [4 /*yield*/, new Promise(function (resolve) { try { var opts = _this.config.spawnOpts || {}; var spawnArgs = _this.config.spawnArgs.map(function (row) { return row.trim(); }).filter(function (row) { return row !== ''; }); - var thread_1 = childProcess.spawn(_this.config.cli, spawnArgs, opts); - process.on('exit', function () { - try { - // eslint-disable-next-line no-console - console.log('Main thread exiting, cleaning up running CLI'); - _this.killThread(thread_1); - } - catch (err) { - // eslint-disable-next-line no-console - console.log('Error running cliUtils on Exit function'); - // eslint-disable-next-line no-console - console.log(err); - } - }); - thread_1.stdout.on('data', function (data) { + thread = childProcess.spawn(_this.config.cli, spawnArgs, opts); + thread.stdout.on('data', function (data) { errorLogFull.push(data.toString()); _this.parseOutput(data); }); - thread_1.stderr.on('data', function (data) { + thread.stderr.on('data', function (data) { // eslint-disable-next-line no-console errorLogFull.push(data.toString()); _this.parseOutput(data); }); - thread_1.on('error', function () { + thread.on('error', function () { // catches execution error (bad file) // eslint-disable-next-line no-console console.log(1, "Error executing binary: ".concat(_this.config.cli)); @@ -291,7 +293,7 @@ var CLI = /** @class */ (function () { }); // thread.stdout.pipe(process.stdout); // thread.stderr.pipe(process.stderr); - thread_1.on('close', function (code) { + thread.on('close', function (code) { if (code !== 0) { // eslint-disable-next-line no-console console.log(code, 'CLI error'); @@ -308,6 +310,7 @@ var CLI = /** @class */ (function () { })]; case 1: cliExitCode = _a.sent(); + thread = undefined; if (!this.config.logFullCliOutput) { this.config.jobLog(errorLogFull.slice(-1000).join('')); } From 5f5d3c1b0437a8c9506b2af6ee6267d4a7e84379 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Thu, 7 Dec 2023 07:03:29 +0000 Subject: [PATCH 5/6] Clean up listener after cli run --- FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts b/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts index 925b498..fcd074a 100644 --- a/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts +++ b/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts @@ -278,20 +278,22 @@ class CLI { // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/explicit-module-boundary-types let thread: any; - process.on('exit', () => { + const exitHandler = () => { if (thread) { try { - // eslint-disable-next-line no-console + // eslint-disable-next-line no-console console.log('Main thread exiting, cleaning up running CLI'); this.killThread(thread); } catch (err) { - // eslint-disable-next-line no-console + // eslint-disable-next-line no-console console.log('Error running cliUtils on Exit function'); // eslint-disable-next-line no-console console.log(err); } } - }); + }; + + process.on('exit', exitHandler); const cliExitCode: number = await new Promise((resolve) => { try { @@ -334,6 +336,8 @@ class CLI { } }); + process.removeListener('exit', exitHandler); + thread = undefined; if (!this.config.logFullCliOutput) { From 1d58a8c0ae1442cc82fb03714996c7fa495a1849 Mon Sep 17 00:00:00 2001 From: HaveAGitGat Date: Thu, 7 Dec 2023 07:05:43 +0000 Subject: [PATCH 6/6] Apply auto-build changes --- FlowPlugins/FlowHelpers/1.0.0/cliUtils.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/FlowPlugins/FlowHelpers/1.0.0/cliUtils.js b/FlowPlugins/FlowHelpers/1.0.0/cliUtils.js index e5e0c92..22b228a 100644 --- a/FlowPlugins/FlowHelpers/1.0.0/cliUtils.js +++ b/FlowPlugins/FlowHelpers/1.0.0/cliUtils.js @@ -248,7 +248,7 @@ var CLI = /** @class */ (function () { }); }; this.runCli = function () { return __awaiter(_this, void 0, void 0, function () { - var childProcess, errorLogFull, thread, cliExitCode; + var childProcess, errorLogFull, thread, exitHandler, cliExitCode; var _this = this; return __generator(this, function (_a) { switch (_a.label) { @@ -256,7 +256,7 @@ var CLI = /** @class */ (function () { childProcess = require('child_process'); errorLogFull = []; this.config.jobLog("Running ".concat(this.config.cli, " ").concat(this.config.spawnArgs.join(' '))); - process.on('exit', function () { + exitHandler = function () { if (thread) { try { // eslint-disable-next-line no-console @@ -270,7 +270,8 @@ var CLI = /** @class */ (function () { console.log(err); } } - }); + }; + process.on('exit', exitHandler); return [4 /*yield*/, new Promise(function (resolve) { try { var opts = _this.config.spawnOpts || {}; @@ -310,6 +311,7 @@ var CLI = /** @class */ (function () { })]; case 1: cliExitCode = _a.sent(); + process.removeListener('exit', exitHandler); thread = undefined; if (!this.config.logFullCliOutput) { this.config.jobLog(errorLogFull.slice(-1000).join(''));