1.0.0 • Published 6 years ago

@queuehammer/signal v1.0.0

Weekly downloads
1
License
ISC
Repository
github
Last release
6 years ago

Signal

TLDR;

Use standard handlers to respond to the multitude of system signals.

new Signal({
  // Shutdown express, and it's about time, literally.
  // We were just signaled
  onTerm: () => expressApp.close()
});

Three callbacks are provided (Maybe more in the future); onTerm, onCore, and onIgn. This gives you a little more control over how they are handled. Stop and Continue are caught by Node.js, so they are not provided.

Suggestions on how you can try to handel these errors come from the signal(7) man page... | Category | General Response | |--|--| | Term | Default action is to terminate the process. | | Ign | Default action is to ignore the signal. | | Core | Default action is to terminate the process and dump core (see core(5)). |

Slightly More Copy

Things happen, systems send signals to your app, and whether or not your listening to them they expect things to happen. Sometimes this means letting you stop something you started gracefully. It could be you just want it to shutdown faster when the time comes. So your not waiting for your Docker containers for the full timeout every time. It's only 10 seconds, but imagine that hundreds of times. The standard signals have standard responses and this library lets you set a callback for the ones we can handle.

Caviots in Node

  • SIGUSR1 is reserved by Node.js to start the debugger. They say this causes problems so it's ignored
  • SIGTERM and SIGINT have default handlers, but i've noticed they dont seem to stop express, so you should use the Term handler to do that or kill other processes you have started. Once you do... you still need to shutdown the app like the original handler would. Maybe not a surprise here, this library does that for you.
  • SIGHUP is SIGTERM for Windows.
  • SIGTERM is not supported on Windows, it can be listened on, so it gets another footnote.
  • SIGBREAK not in the original POSIX set, cause windows, but is delivered on Windows when + is pressed, on non-Windows platforms it can be listened on, but there is no way to send or generate it. A listener is created, and it's grouped under the Ign handler.
  • SIGWINCH also no in the original POSIX is delivered when the console has been resized. On Windows, this will only happen on write to the console when the cursor is being moved, or when a readable tty is used in raw mode.
  • SIGKILL Adding a listener will cause a runtime error in newer version of Node.js so this library does not do that.
  • SIGSTOP like SIGKILL.
  • SIGBUS, SIGFPE, SIGSEGV and SIGILL, when not raised artificially using kill(2), inherently leave the process in a state from which it is not safe to attempt to call JS listeners. Doing so might lead to the process hanging in an endless loop, since listeners attached using process.on() are called asynchronously and therefore unable to correct the underlying problem.

Signal - Overview of Signals

Good additional reading on the signals that exist, just not the windows ones.

SignalValueActionComment
SIGHUP1TermHangup detected on controlling terminal or death of controlling process
SIGINT2TermInterrupt from keyboard
SIGQUIT3CoreQuit from keyboard
SIGILL4CoreIllegal Instruction
SIGABRT6CoreAbort signal from abort(3)
SIGFPE8CoreFloating-point exception
SIGKILL9TermKill signal
SIGSEGV11CoreInvalid memory reference
SIGPIPE13TermBroken pipe: write to pipe with no readers; see pipe(7)
SIGALRM14TermTimer signal from alarm(2)
SIGTERM15TermTermination signal
SIGUSR130,10,16TermUser-defined signal 1
SIGUSR231,12,17TermUser-defined signal 2
SIGCHLD20,17,18IgnChild stopped or terminated
SIGCONT19,18,25ContContinue if stopped
SIGSTOP17,19,23StopStop process
SIGTSTP18,20,24StopStop typed at terminal
SIGTTIN21,21,26StopTerminal input for background process
SIGTTOU22,22,27StopTerminal output for background process