Skip to content

Commit 465b28e

Browse files
authored
Remove containers on CTRL-C (#111)
1 parent b72ef62 commit 465b28e

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/default-cmd.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {assert} from "./asserts";
99
import * as dotenv from "dotenv";
1010
import * as camelCase from "camelcase";
1111

12+
let parser: Parser|null = null;
1213
const checkFolderAndFile = (cwd: string, file?: string) => {
1314
assert(fs.pathExistsSync(cwd), `${cwd} is not a directory`);
1415

@@ -44,18 +45,18 @@ export async function handler(argv: any) {
4445
} else if (argv.list != null) {
4546
checkFolderAndFile(cwd, argv.file);
4647
const pipelineIid = await state.getPipelineIid(cwd);
47-
const parser = await Parser.create(cwd, pipelineIid, false, argv.file, argv.home);
48+
parser = await Parser.create(cwd, pipelineIid, false, argv.file, argv.home);
4849
Commander.runList(parser);
4950
} else if (argv.job) {
5051
checkFolderAndFile(cwd, argv.file);
5152
const pipelineIid = await state.getPipelineIid(cwd);
52-
const parser = await Parser.create(cwd, pipelineIid, false, argv.file, argv.home);
53+
parser = await Parser.create(cwd, pipelineIid, false, argv.file, argv.home);
5354
await Commander.runSingleJob(parser, argv.job, argv.needs, argv.privileged);
5455
} else {
5556
checkFolderAndFile(cwd, argv.file);
5657
await state.incrementPipelineIid(cwd);
5758
const pipelineIid = await state.getPipelineIid(cwd);
58-
const parser = await Parser.create(cwd, pipelineIid, false, argv.file, argv.home);
59+
parser = await Parser.create(cwd, pipelineIid, false, argv.file, argv.home);
5960
await Commander.runPipeline(parser, argv.manual || [], argv.privileged);
6061
}
6162
}
@@ -71,3 +72,15 @@ exports.handler = async (argv: any) => {
7172
throw e;
7273
}
7374
};
75+
76+
process.on('SIGINT', async(_: string, code: number) => {
77+
if (!parser) {
78+
return process.exit(code);
79+
}
80+
const promises = [];
81+
for (const job of parser.getJobs()) {
82+
promises.push(job.removeContainer());
83+
}
84+
await Promise.all(promises);
85+
process.exit(code);
86+
});

src/job.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export class Job {
262262
this.finished = finished;
263263
}
264264

265-
private async removeContainer() {
265+
public async removeContainer() {
266266
if (this.containerId) {
267267
await Utils.spawn(`docker rm -f ${this.containerId}`);
268268
}

0 commit comments

Comments
 (0)