Answer the question
In order to leave comments, you need to log in
How to write a handler in NodeJS that sends a request before the process terminates?
Hello.
There is an external module that logs messages to an array on the server and periodically sends them via http to another server. It is necessary to force the entire contents of the array to be sent before the process terminates. Something like:
var request = require('request');
var drainArray = function() {
var options = {...};
request(options, callback)
}
process.stdin.resume();
process.on('exit', drainArray);
Answer the question
In order to leave comments, you need to log in
Watching how the process ends, if by signals, then try:
//do something when app is closing
process.on('SIGINT', drainArray); // выход по Ctrl-C из консоли
process.on('SIGTERM', drainArray); // завершение через kill
process.on('exit', drainArray); // выход по собственному желанию
process.on('uncaughtException', drainArray); // если свалился
var drainArray = function() {
if (!process.flagExit) {
process.flagExit = true;
// отправка данных
}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question