Skip to content

Commit

Permalink
Better handling of SIGINT and SIGTERM
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Feb 5, 2019
1 parent 6d71401 commit b03d1b3
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 30 deletions.
45 changes: 35 additions & 10 deletions dist/ansible-interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -24463,14 +24463,42 @@ async function execute(command) {
env: process$1.env,
encoding: "utf8",
shell: true,
detached: false,
stdio: "inherit"
};

const binary = command.shift();
const proc = spawn$1(binary, command, opts);

const interrupt = () => proc.kill('SIGINT');
const terminate = () => proc.kill('SIGTERM');

process$1.on('SIGINT', interrupt);
process$1.on('SIGTERM', terminate);

const cleanup = () => {
process$1.off('SIGINT', interrupt);
process$1.off('SIGTERM', terminate);
};

proc.on('exit', resolve);
proc.on('error', reject);
proc.on('exit', (code) => {
cleanup();

if (0 === code) {
resolve(code);
} else {
reject(`Ansible exited with code ${code}`);
}
});

proc.on('error', (err) => {
if (proc.connected) {
proc.kill();
}

cleanup();
reject(err);
});
});
}

Expand All @@ -24480,21 +24508,18 @@ var runCommand = async function(command) {
await confirm$1(command_line);
await saveHistory$1(command);

try {
await execute(command);
console.log('');
process$1.exit(0);
} catch (e) {
console.log('');
process$1.exit(1);
}
await execute(command);

console.log('');
process$1.exit(0);
};

var ansibleInteractive = async function() {
try {
const replay_command = await replay();
if (replay_command) {
await runCommand(replay_command); // FIXME
return;
}

const inventory = await loadInventory(args_1.inventory);
Expand Down
Binary file modified dist/bin/ansible-interactive
Binary file not shown.
45 changes: 35 additions & 10 deletions dist/js/ansible-interactive
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24464,14 +24464,42 @@ async function execute(command) {
env: process$1.env,
encoding: "utf8",
shell: true,
detached: false,
stdio: "inherit"
};

const binary = command.shift();
const proc = spawn$1(binary, command, opts);

const interrupt = () => proc.kill('SIGINT');
const terminate = () => proc.kill('SIGTERM');

process$1.on('SIGINT', interrupt);
process$1.on('SIGTERM', terminate);

const cleanup = () => {
process$1.off('SIGINT', interrupt);
process$1.off('SIGTERM', terminate);
};

proc.on('exit', resolve);
proc.on('error', reject);
proc.on('exit', (code) => {
cleanup();

if (0 === code) {
resolve(code);
} else {
reject(`Ansible exited with code ${code}`);
}
});

proc.on('error', (err) => {
if (proc.connected) {
proc.kill();
}

cleanup();
reject(err);
});
});
}

Expand All @@ -24481,21 +24509,18 @@ var runCommand = async function(command) {
await confirm$1(command_line);
await saveHistory$1(command);

try {
await execute(command);
console.log('');
process$1.exit(0);
} catch (e) {
console.log('');
process$1.exit(1);
}
await execute(command);

console.log('');
process$1.exit(0);
};

var ansibleInteractive = async function() {
try {
const replay_command = await replay();
if (replay_command) {
await runCommand(replay_command); // FIXME
return;
}

const inventory = await loadInventory(args_1.inventory);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"make-dist-dirs": "mkdir -p dist/js && mkdir -p dist/bin",
"dist-js": "echo '#!/usr/bin/env node' | cat - dist/ansible-interactive.js > dist/js/ansible-interactive && chmod +x dist/js/ansible-interactive",
"dist-bin": "nexe -i dist/ansible-interactive.js -o dist/bin/ansible-interactive",
"dist-bin-fresh": "nexe --build -i dist/ansible-interactive.js -o dist/bin/ansible-interactive",
"dist": "yarn run clean-dist && yarn run make-dist-dirs && yarn run build && yarn run dist-js"
}
}
1 change: 1 addition & 0 deletions src/ansible-interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = async function() {
const replay_command = await replay();
if (replay_command) {
await runCommand(replay_command); // FIXME
return;
}

const inventory = await loadInventory(args.inventory);
Expand Down
44 changes: 34 additions & 10 deletions src/run-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,42 @@ async function execute(command) {
env: process.env,
encoding: "utf8",
shell: true,
detached: false,
stdio: "inherit"
};

const binary = command.shift();
const proc = spawn(binary, command, opts);

const interrupt = () => proc.kill('SIGINT');
const terminate = () => proc.kill('SIGTERM');

process.on('SIGINT', interrupt);
process.on('SIGTERM', terminate);

const cleanup = () => {
process.off('SIGINT', interrupt);
process.off('SIGTERM', terminate);
};

proc.on('exit', resolve);
proc.on('error', reject);
proc.on('exit', (code) => {
cleanup();

if (0 === code) {
resolve(code);
} else {
reject(`Ansible exited with code ${code}`);
}
});

proc.on('error', (err) => {
if (proc.connected) {
proc.kill();
}

cleanup();
reject(err);
});
});
}

Expand All @@ -56,12 +84,8 @@ module.exports = async function(command) {
await confirm(command_line);
await saveHistory(command);

try {
await execute(command);
console.log('');
process.exit(0);
} catch (e) {
console.log('');
process.exit(1);
}
await execute(command);

console.log('');
process.exit(0);
};

0 comments on commit b03d1b3

Please sign in to comment.