Skip to content

Commit

Permalink
Merge pull request #569 from HaveAGitGat/kill_thread
Browse files Browse the repository at this point in the history
Kill flow subthread on worker exit
  • Loading branch information
HaveAGitGat authored Dec 7, 2023
2 parents c7e22c9 + 1d58a8c commit fec677b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 11 deletions.
49 changes: 43 additions & 6 deletions FlowPlugins/FlowHelpers/1.0.0/cliUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,30 +224,65 @@ 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 childProcess, errorLogFull, thread, exitHandler, cliExitCode;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
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(' ')));
exitHandler = 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);
}
}
};
process.on('exit', exitHandler);
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 = childProcess.spawn(_this.config.cli, spawnArgs, opts);
thread.stdout.on('data', function (data) {
// eslint-disable-next-line no-console
// console.log(data.toString());
errorLogFull.push(data.toString());
_this.parseOutput(data);
});
thread.stderr.on('data', function (data) {
// eslint-disable-next-line no-console
// console.log(data.toString());
errorLogFull.push(data.toString());
_this.parseOutput(data);
});
Expand Down Expand Up @@ -276,6 +311,8 @@ 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(''));
}
Expand Down
55 changes: 50 additions & 5 deletions FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand All @@ -249,24 +273,41 @@ class CLI {

const errorLogFull: string[] = [];

// eslint-disable-next-line no-console
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;

const exitHandler = () => {
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);
}
}
};

process.on('exit', exitHandler);

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);
thread = childProcess.spawn(this.config.cli, spawnArgs, opts);

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);
});
Expand Down Expand Up @@ -295,6 +336,10 @@ class CLI {
}
});

process.removeListener('exit', exitHandler);

thread = undefined;

if (!this.config.logFullCliOutput) {
this.config.jobLog(errorLogFull.slice(-1000).join(''));
}
Expand Down

0 comments on commit fec677b

Please sign in to comment.