Save files on project exit

I am running a handler that should save my files when the app exits but it isn’t succeeding in doing so and my files remain unchanged.

setInterval(() => {
  fs.writeFileSync('grid.json', JSON.stringify(grid));
  fs.writeFileSync('timeouts.json', JSON.stringify(timeouts));
}, 300000)
setInterval(() => {
  fs.writeFileSync('pixelCounts.json', JSON.stringify(pixelCounts));
}, 10000)

//do something when app is closing
process.on('exit', exitHandler.bind(null));

//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null));

//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null));

I’m not certain, but I think that interval timers are cleared on exit, so they won’t be run.

What code have you got in your exitHandler function?

This is discussed here, its worth reading the criticisms of this approach in the comments. https://stackoverflow.com/questions/14031763/doing-a-cleanup-action-just-before-node-js-exits

It’s possible the container is going to sleep. The 300000 ms timer will be going off right around glitch’s five minute sleep window: https://glitch.com/help/restrictions/

Edit: I’m also not sure how glitch handles processes at the end of that window. Maybe the exit callbacks are never hit? not sure…