Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capturing Ctrl+C (SIGTERM) in a subcommand #2109

Closed
gvensan opened this issue Dec 18, 2023 · 5 comments
Closed

Capturing Ctrl+C (SIGTERM) in a subcommand #2109

gvensan opened this issue Dec 18, 2023 · 5 comments

Comments

@gvensan
Copy link

gvensan commented Dec 18, 2023

Hello, This may have been discussed before - I could not find a way for this.

I am not using executable for the subcommand, hence cannot use exitHandler where I can catch the signals at process level. In a classic use of commander, subcommand execution - is there anyway to capture the SIGTERM in order to do some housekeeping work. An example would be of great help.

If this is already answered/resolved, my apologies - please point me to a sample.

@shadowspawn
Copy link
Collaborator

Not sure about your subcommand setup. Are you using subcommands with action handlers?

@gvensan
Copy link
Author

gvensan commented Dec 18, 2023

Yes, have action handlers. sorry, If I was not clear.

@shadowspawn
Copy link
Collaborator

Ok, so the action handlers are just running javascript callbacks. Node supports listening for signals. So nothing Commander specific, just the normal signal handling?

const { Command } = require('commander');
const process = require('node:process');

function sleep(ms) {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}

const program = new Command();
program.command('sleep')
  .action(async () => {
    await sleep(5000);
  });

process.on('SIGINT', () => {
  console.log('Received SIGINT.');
  // Should we exit here?
});

console.log('Before parse');
program.parseAsync().then(() => {
  console.log('After parse');  
});
% node index.js sleep
Before parse
After parse
% node index.js sleep
Before parse
^CReceived SIGINT.
After parse

@gvensan
Copy link
Author

gvensan commented Dec 18, 2023

Thank you very much. The snippet helped!

I was running on a older version of commander (npm) and had the experimental warning suppressed as recommended by
nodejs/node#30810 (comment)

const { emit: originalEmit } = process;
process.emit = (event, error) => event === 'warning' && error.name === 'ExperimentalWarning' ? false : originalEmit.apply(process, arguments);

I realize now, this is not required as I had upgraded to latest commander version. This was preventing the signal handling.

Again, thanks a lot for a quick turn around. Much appreciated!

BTW, Love commander - well structured, friendly and covers all my scenarios.

@shadowspawn
Copy link
Collaborator

(Thanks for update.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants