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');
});
});
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');
});
});
Share
Improve this question
asked Apr 14, 2016 at 17:32
XCSXCS
28.2k28 gold badges104 silver badges153 bronze badges
6
-
Did you try github./remy/nodemon/blob/master/doc/events.md
nodemon.on('restart',...)
? – crackmigg Commented Apr 14, 2016 at 17:38 -
@migg No, didn't have the
nodemon
package included, will take a look. – XCS Commented Apr 14, 2016 at 17:41 - @migg Nope, that event is not called. – XCS Commented Apr 14, 2016 at 17:54
- So again, I start nodemon from the console mand. – XCS Commented Apr 14, 2016 at 18:02
- 1 @AnandUndavia I don't really remember, I think I didn't manage to make it work on Windows, and what I did was just having saves/snapshopts at a regular interval, if the server failed/restarted I would just load the latest available snapshopt. – XCS Commented Jun 26, 2019 at 8:34
2 Answers
Reset to default 3Below 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();
});