Can I run a function when I quit my Node App? - javascript

I'm working on a project for school in Node.js on a Raspberry Pi. I have a script that is running and will run for long periods of time controlling some LEDs. Is there a way that I can run a function when I quit the program to turn off all the LEDs I'm using (Stop power to the GPIO)?

You can handle the SIGINT (CTRL-C) signal. Something like:
function cleanup() {
// do clean up here
console.log("clean up");
}
process.on("SIGINT", () => {
cleanup();
process.exit(0);
});

Related

How to wait an arbitrary amount of time in nightwatch.js without web browser interaction

I'm migrating my tests from Intern.js to Nightwatch.js and I'm sure it's not the recommended way of writing tests, but my tests asynchronously make browser command calls (my tests use a class that has its own command queue). The Nightwatch command queue can be empty sometimes depending on what's executing, but will eventually be populated. I need a way to manually tell Nightwatch when it's done, and just wait until then, otherwise the browser will close too early.
I can do something like this:
finished() {
let done = false;
//will run at the end of my class's internal command queue
this.command
.then(() => {
done = true;
});
//will cause nightwatch to wait until my command queue is done
this.browser.perform(doneFn => {
const check = () => {
if (done) {
return doneFn();
}
setTimeout(check, 10);
}
check();
});
}
But in that case perform() will time out after waiting 10 seconds, and I don't seem to be able to configure that. I could otherwise run a sequence of ~9 second perform() calls but it just sounds too hacky. I could rewrite to depend on nightwatch command queue instead, but that'd also be a lot more work to rewrite everything.
In the end it was easier to just switch to depending on using nightwatch's command queue instead. Anything that was just a .then() with Intern.js got changed to a .perform() in its various forms.

How to get event when system is about to go to sleep

Is it possible to get notified when the system is about to change its power state to sleep?
Something like
process.on('sleep', () => foo())
When going to sleep, the process does not get killed or exit, so answers from
doing a cleanup action just before node.js exits do not suffer.
Your programm receive informations from your Operating System in linux from signals. I guess the signal you are looking for is from the following list:
To handle signals in node.js here is how you do it :
// Begin reading from stdin so the process does not exit.
process.stdin.resume();
process.on('SIGINT', () => {
console.log('Received SIGINT. Press Control-D to exit.');
});
// Using a single function to handle multiple signals
function handle(signal) {
console.log(`Received ${signal}`);
}
process.on('SIGINT', handle);
process.on('SIGTERM', handle);

how to perform a set of task before the process gets killed using node js

How to handle signal from external file in java script file.
i have a shell script which stops all the running process. when my node process is getting stopped or killed i want to perform some task and then allow this node to stop/kill.
i have a java script file in which i want to write this handler.
How can i catch this stop signal and perform some task before getting
killed
i have tried:
function method1(){
logger.info("this is our method to be executed before getting killed");
}
process.on('SIGTERM',method1());
you can register handle on exit
process.stdin.resume();
function exitHandler(options, err) {
//exit logic
}
process.on('exit', exitHandler.bind());
process.on('SIGINT', exitHandler.bind());
process.on('uncaughtException', exitHandler.bind());

Nodemon execute function before every restart

I have a Node.js game server and I start it by running nodemon app.js. Now, every time I edit a file the server restarts. I have implemented save and load functions and I want every time the game server restarts (due to the file chages) the game to be saved before restarting so that I can load the previous state after the restart.
Something like this is what I want:
process.on('restart', function(doneCallback) {
saveGame(doneCallback);
// The save game is async because it is writing toa file
}
I have tried using the SIGUR2 event but it was never triggered. This is what I tried, but the function was never called.
// Save game before restarting
process.once('SIGUSR2', function () {
console.log('SIGUR2');
game.saveGame(function() {
process.kill(process.pid, 'SIGUSR2');
});
});
Below code works properly in Unix machine. Now, As your saveGame is asynchronous you have to call process.kill from within the callback.
process.once('SIGUSR2', function() {
setTimeout(()=>{
console.log('Shutting Down!');
process.kill(process.pid, 'SIGUSR2');
},3000);
});
So, your code looks fine as long as you execute your callback function from within the game.saveGame() function.
// Save game before restarting
process.once('SIGUSR2', function () {
console.log('SIGUR2');
game.saveGame(function() {
process.kill(process.pid, 'SIGUSR2');
});
});
on Windows
run app like this:
nodemon --signal SIGINT app.js
in app.js add code
let process = require('process');
process.once('SIGINT', function () {
console.log('SIGINT received');
your_func();
});

setInterval runs node function once and continues to run but does not run function

I have a node process running that calls a node module
var monitor = require('./monitor.js');
var monitorInterval = setInterval(function () {
monitor();
console.log('running monitor')
}, 10000);
The first time this runs the monitor module but it then seems to run the interval but not the function on each subsequent 10s interval. The logs look like this.
MONITOR PROCESS STARTING
running monitor
running monitor
database connected
database connected
etc...
running monitor
running monitor
running monitor
running monitor
<10s PASS AND MONITOR DOES NOT RUN HERE>
running monitor
running monitor
<10s PASS MONITOR DOES NOT RUN HERE>
So why is this? Could it be because the JS engine is not idle? I'm pretty sure it is doing nothing during subsequent intervals.
Monitor is initialized as follows:
function Monitor() {
this._initialize();
}
module.exports = function (cb) {
if (monitor === null) {
monitor = new Monitor();
}
};
Monitor.prototype = {
_initialize: function () {
console.log('MONITOR PROCESS STARTING');
var monitor = this;
//etc
}
}

Categories