Merge pull request #569 from HaveAGitGat/kill_thread

Kill flow subthread on worker exit
make-only-subtitle-default
HaveAGitGat 2 years ago committed by GitHub
commit fec677b37e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 () { this.runCli = function () { return __awaiter(_this, void 0, void 0, function () {
var childProcess, errorLogFull, cliExitCode; var childProcess, errorLogFull, thread, exitHandler, cliExitCode;
var _this = this; var _this = this;
return __generator(this, function (_a) { return __generator(this, function (_a) {
switch (_a.label) { switch (_a.label) {
case 0: case 0:
childProcess = require('child_process'); childProcess = require('child_process');
errorLogFull = []; errorLogFull = [];
// eslint-disable-next-line no-console
this.config.jobLog("Running ".concat(this.config.cli, " ").concat(this.config.spawnArgs.join(' '))); 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) { return [4 /*yield*/, new Promise(function (resolve) {
try { try {
var opts = _this.config.spawnOpts || {}; var opts = _this.config.spawnOpts || {};
var spawnArgs = _this.config.spawnArgs.map(function (row) { return row.trim(); }).filter(function (row) { return row !== ''; }); 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) { thread.stdout.on('data', function (data) {
// eslint-disable-next-line no-console
// console.log(data.toString());
errorLogFull.push(data.toString()); errorLogFull.push(data.toString());
_this.parseOutput(data); _this.parseOutput(data);
}); });
thread.stderr.on('data', function (data) { thread.stderr.on('data', function (data) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
// console.log(data.toString());
errorLogFull.push(data.toString()); errorLogFull.push(data.toString());
_this.parseOutput(data); _this.parseOutput(data);
}); });
@ -276,6 +311,8 @@ var CLI = /** @class */ (function () {
})]; })];
case 1: case 1:
cliExitCode = _a.sent(); cliExitCode = _a.sent();
process.removeListener('exit', exitHandler);
thread = undefined;
if (!this.config.logFullCliOutput) { if (!this.config.logFullCliOutput) {
this.config.jobLog(errorLogFull.slice(-1000).join('')); this.config.jobLog(errorLogFull.slice(-1000).join(''));
} }

@ -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<{ runCli = async (): Promise<{
cliExitCode: number, cliExitCode: number,
errorLogFull: string[], errorLogFull: string[],
@ -249,24 +273,41 @@ class CLI {
const errorLogFull: string[] = []; const errorLogFull: string[] = [];
// eslint-disable-next-line no-console
this.config.jobLog(`Running ${this.config.cli} ${this.config.spawnArgs.join(' ')}`); 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) => { const cliExitCode: number = await new Promise((resolve) => {
try { try {
const opts = this.config.spawnOpts || {}; const opts = this.config.spawnOpts || {};
const spawnArgs = this.config.spawnArgs.map((row) => row.trim()).filter((row) => row !== ''); 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) => { thread.stdout.on('data', (data: string) => {
// eslint-disable-next-line no-console
// console.log(data.toString());
errorLogFull.push(data.toString()); errorLogFull.push(data.toString());
this.parseOutput(data); this.parseOutput(data);
}); });
thread.stderr.on('data', (data: string) => { thread.stderr.on('data', (data: string) => {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
// console.log(data.toString());
errorLogFull.push(data.toString()); errorLogFull.push(data.toString());
this.parseOutput(data); this.parseOutput(data);
}); });
@ -295,6 +336,10 @@ class CLI {
} }
}); });
process.removeListener('exit', exitHandler);
thread = undefined;
if (!this.config.logFullCliOutput) { if (!this.config.logFullCliOutput) {
this.config.jobLog(errorLogFull.slice(-1000).join('')); this.config.jobLog(errorLogFull.slice(-1000).join(''));
} }

Loading…
Cancel
Save