-1

I'm trying to write code that will return whether the server is already running or not. However, my tests are not working.

I want on process.exit to write a file that says the server is no longer running, and for when the server starts up, it to write that the server is running:

const { readdir, writeFile, writeFileSync, readFileSync } = require('fs')                                                                     
const IS_RUNNING_LOCATION = `${__dirname}/running.txt`                                                                                        
if (readFileSync(IS_RUNNING_LOCATION).toString() ===  'true')                                                                                 
  return                                                                                                                                      
process.on('exit', () => {                                                                                                                    
  writeFileSync(IS_RUNNING_LOCATION, 'false', {}, (err) => {                                                                                  
    if (!err) return console.log('written')                                                                                                                    
    console.log(err)                                                                                                                          
  })                                                                                                                                          
})                                                                                                                                            
writeFileSync(IS_RUNNING_LOCATION, 'true')             

setInterval(()=> console.log('running'), 1000)   

However, with this code, when I exit with ^C, the on exit code is not run.

Can anyone tell me why this is the case or how I can fix it?

Zane Hitchcox
  • 936
  • 1
  • 9
  • 23

1 Answers1

0

Found the answer here: doing a cleanup action just before node.js exits

This is the code I ended up using:

const { writeFileSync, readFileSync } = require('fs')                                                                     
const IS_RUNNING_LOCATION = `${__dirname}/.running`                                                                                           
if (readFileSync(IS_RUNNING_LOCATION).toString() ===  'true')                                                                                 
  process.exit(0)                                                                                                                             

writeFileSync(IS_RUNNING_LOCATION, 'true')                                                                                                    
function exitHandler(options, err) {                                                                                                          
  writefilesync(is_running_location, 'false', {}, (err) => {                                                                                  
    if (!err) return if (!err) return console.log('written')                                                                                  
    console.log(err)                                                                                                                          
  })                                                                                                                                          
  if (options.exit) process.exit();                                                                                                           
}                                                                                                                                             

process.on('exit', exitHandler.bind(null,{cleanup:true}))                                                                                     
process.on('SIGINT', exitHandler.bind(null, {exit:true}))                                                                                     
process.on('uncaughtException', exitHandler.bind(null, {exit:true}))                                                                          
Zane Hitchcox
  • 936
  • 1
  • 9
  • 23