How to save node application?

I finally completed the conversion of one of my projects to using Node.JS, but now I have problems maintaining my application on the server: / The problem is that if I close the Node putty session, it just stops.

I searched a lot for this problem, and it seems like creating an upstart script and using the Forever module is the way to go.

I started googling and created this upstart script:

 #!upstart description "Loner NodeJS app launcher" author " me@me.com " start on startup stop on shutdown script export HOME="/root" exec sudo node /home/jjmpsp/server.js >> /home/jjmpsp/server.sys.log 2>&1 end script 

Then I launched the start app on the server last night, and the server stayed up when I closed the putty session. Things are good.

However, I came this morning and found that the Node application stopped, so I checked the server.sys.log file to see what was happening. The application seems to be working fine until it comes across this exception:

 debug: client authorized info: handshake authorized fziLHZA3Vo9i55eubvOq events.js:48 throw arguments[1]; // Unhandled 'error' event ^ Error: Connection lost: The server closed the connection. at Protocol.end (/home/jjmpsp/node_modules/mysql/lib/protocol/Protocol.js:73:13) at Socket.onend (stream.js:80:10) at Socket.emit (events.js:88:20) at TCP.onread (net.js:348:51) 

Today I googling even more and found that Forever will actually restart the NodeJS application if it unexpectedly exits. I tried installing the module with npm install forever , but I get this huge list of errors:

 jjmpsp@alex :~$ npm install forever npm ERR! error installing forever@0.9.2 Error: No compatible version found: node-fork@ '>=0.4.0- <0.5.0-' npm ERR! error installing forever@0.9.2 No valid targets found. npm ERR! error installing forever@0.9.2 Perhaps not compatible with your version of node? npm ERR! error installing forever@0.9.2 at installTargetsError (/usr/local/lib/node_modules/npm/lib/cache.js:424:10) npm ERR! error installing forever@0.9.2 at /usr/local/lib/node_modules/npm/lib/cache.js:406:17 npm ERR! error installing forever@0.9.2 at saved (/usr/local/lib/node_modules/npm/lib/utils/npm-registry-client/get.js:136:7) npm ERR! error installing forever@0.9.2 at Object.cb [as oncomplete] (/usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:36:9) npm ERR! Error: No compatible version found: node-fork@ '>=0.4.0- <0.5.0-' npm ERR! No valid targets found. npm ERR! Perhaps not compatible with your version of node? npm ERR! at installTargetsError (/usr/local/lib/node_modules/npm/lib/cache.js:424:10) npm ERR! at /usr/local/lib/node_modules/npm/lib/cache.js:406:17 npm ERR! at saved (/usr/local/lib/node_modules/npm/lib/utils/npm-registry-client/get.js:136:7) npm ERR! at Object.cb [as oncomplete] (/usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:36:9) npm ERR! Report this *entire* log at: npm ERR! <http://github.com/isaacs/npm/issues> npm ERR! or email it to: npm ERR! < npm-@googlegroups.com > npm ERR! npm ERR! System Linux 3.8.4-x86_64-linode31 npm ERR! command "node" "/usr/local/bin/npm" "install" "forever" npm ERR! cwd /home/jjmpsp npm ERR! node -v v0.5.11-pre npm ERR! npm -v 1.0.106 npm ERR! Error: EACCESS, Permission denied 'npm-debug.log' npm ERR! Report this *entire* log at: npm ERR! <http://github.com/isaacs/npm/issues> npm ERR! or email it to: npm ERR! < npm-@googlegroups.com > npm ERR! npm ERR! System Linux 3.8.4-x86_64-linode31 npm ERR! command "node" "/usr/local/bin/npm" "install" "forever" npm ERR! cwd /home/jjmpsp npm ERR! node -v v0.5.11-pre npm ERR! npm -v 1.0.106 npm ERR! path npm-debug.log npm ERR! code EACCESS npm ERR! npm ERR! Additional logging details can be found in: npm ERR! /home/jjmpsp/npm-debug.log npm not ok 

What steps should I take to fix this? I have absolutely no ideas. I was looking for all kinds of technical details, and I just don't get anything.

Any help is appreciated :)

+6
source share
7 answers

After a painful few hours of reconfiguring everything, I think I finally solved my problem! It looks like I might have had 2 versions of node or something like that! For future reference: If you are new to Node, be sure to install nvm to simplify node versioning and you will not run into this problem :)

+5
source
  • First, you should focus on what kills your node server. Forever is not going to "fix" the problem. Every time a node exits / restarts, it will cause problems and your users may lose data. upstart or forever are just stripes. (In fact, Upstart will restart your server, but it will refuse if your server does not work.)

    The only long-term solution for finding / fixing each source of errors and recording a set of regression tests to verify that all previous problems have been fixed.

  • start on startup will not work

  • Your forever installation has broken due to permissions. Try sudo npm install -g forever Optional: All server configuration must be scripted. This way you can set up a test / staging server that reflects production. One easy way to do this is Vagrant . You can develop "locally" on the same OS as on your server. And you can check things like "does node on reboot?" or "if the server is destroyed, can I re-create my server from the base OS?"

+6
source

After much research and poor decisions, I found pm2 (part of the Keymetrics recommended by Karlos) is by far the best solution. You can find it here:

http://pm2.keymetrics.io/

+2
source

I think it will never exit the nodejs application unexpectedly, possibly due to the mysql parameter 'wait_timeout', which is set to 28800 seconds by default (i.e. 8 hours). If the nodejs application does not interact with the mysql database for 8 hours, the connection is closed. You must set the wait_timeout parameter to "MAX" (for example, 365 days) or any other value as you need in the mysql db settings.

Link to wait_timeout settings:

http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_wait_timeout

or

Add the code to your code. This code will ping mysql db once per hour and your connection will not break.

// Running MYSQL ping code

 function keepalive() { conn.query('select 1', [], function(err, result) { if(err) return console.log(err); // Successul keepalive console.log("keepalive: "+ result); }); } setInterval(keepalive, 1000*60*60); 

// MySQL ping code has ended

You can also use handledisconnect () code and handle a specific connection error in the code, as shown in the example below.

/// Start of code

function handledisconnect () {

  log.info("handledisconnect()"); // Reading config.json file for database connection var db_config = JSON.parse(fs.readFileSync(appRoot + "/conf.json")); conn = mysql.createConnection(db_config); conn.connect(function(err) { if (err) { log.error('error when connecting to db : ',err); // We introduce a delay before attempting to reconnect setTimeout(handledisconnect, 200); } else { log.info("database connected"); } }); conn.on('error', function(err) { log.error('db error : ',err); // handling the reconnection for 'PROTOCOL_CONNECTION_LOST' error if (err.code === 'PROTOCOL_CONNECTION_LOST') { log.error(err); setTimeout(handledisconnect, 200); handledisconnect(); } else { log.error(err); } // handling the reconnection for 'EADDRINUSE' error if (err.code === 'EADDRINUSE') { setTimeout(handledisconnect, 200); handledisconnect(); } else { log.error(err); } // handling the reconnection for 'ECONNREFUSED' error if (err.code === 'ECONNREFUSED') { log.error(err); setTimeout(handledisconnect, 200); handledisconnect(); } else { log.error(err); } }); ///**** ADD YOUR CODE HERE****//// } handledisconnect(); /// Code ends 
+1
source
0
source

Of the logs that actually seems to be the problem in your case, is your mysql connection. Whenever the mysql server closes the connection, the node server dies. You can use keealive or use the connection pool.

0
source

Use keymetrics.io, it will automatically restart your node application if a problem occurs.

https://keymetrics.io

0
source

Source: https://habr.com/ru/post/944766/


All Articles