Node app stops responding after some time












0















I recently pushed my node js and express server to the production.
After a few days of working on the production, I realized that sometimes with no reason my server stops responding for requests.



[31/Dec/2018:09:55:39 +0000] "GET /vehicles?user_lat=52.458876&user_lng=16.9006237 HTTP/1.1" - - ms


I was looking for an answer here but the only advice I found was to debug server and find out what is causing it crashes. Unfortunately, I am not able to find out where is the problem. I added listeners to the process to find unhandled promises rejection and uncaught exception but nothing was caught.



What I also found is when the server stops responding the CPU usage is
running high. Normally it takes 1-5%, but when it hangs it is around 20%.



The next strange thing for me is the output of pm2.log file. According to it, my server often restarts but I don't understand why.



pm2.log file:



        2018-12-22T10:12:10: PM2 log: pid=28427 msg=process killed
2018-12-22T10:12:10: PM2 log: App [server:0] starting in -fork mode-
2018-12-22T10:12:10: PM2 log: App [server:0] online
2018-12-24T13:33:40: PM2 log: Stopping app:server id:0
2018-12-24T13:33:40: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-24T13:33:41: PM2 log: pid=31380 msg=process killed
2018-12-24T13:33:41: PM2 log: App [server:0] starting in -fork mode-
2018-12-24T13:33:41: PM2 log: App [server:0] online
2018-12-24T13:45:29: PM2 log: Stopping app:server id:0
2018-12-24T13:45:29: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-24T13:45:29: PM2 log: pid=5521 msg=process killed
2018-12-24T13:45:29: PM2 log: App [server:0] starting in -fork mode-
2018-12-24T13:45:29: PM2 log: App [server:0] online
2018-12-25T03:11:40: PM2 log: Stopping app:server id:0
2018-12-25T03:11:40: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-25T03:11:40: PM2 log: pid=6049 msg=process killed
2018-12-25T03:11:40: PM2 log: App [server:0] starting in -fork mode-
2018-12-25T03:11:40: PM2 log: App [server:0] online
2018-12-27T05:07:30: PM2 log: Stopping app:server id:0
2018-12-27T05:07:30: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-27T05:07:30: PM2 log: pid=11531 msg=process killed
2018-12-27T05:07:30: PM2 log: App [server:0] starting in -fork mode-
2018-12-27T05:07:30: PM2 log: App [server:0] online
2018-12-29T08:08:27: PM2 log: Stopping app:server id:0
2018-12-29T08:08:27: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-29T08:08:27: PM2 log: pid=28263 msg=process killed
2018-12-29T08:08:27: PM2 log: App [server:0] starting in -fork mode-
2018-12-29T08:08:27: PM2 log: App [server:0] online
2018-12-29T09:41:23: PM2 log: Stopping app:server id:0
2018-12-29T09:41:23: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-29T09:41:23: PM2 log: pid=11341 msg=process killed
2018-12-29T09:41:23: PM2 log: App [server:0] starting in -fork mode-
2018-12-29T09:41:23: PM2 log: App [server:0] online
2018-12-31T10:03:33: PM2 log: Stopping app:server id:0
2018-12-31T10:03:33: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-31T10:03:33: PM2 log: pid=12337 msg=process killed
2018-12-31T10:03:33: PM2 log: App [server:0] starting in -fork mode-
2018-12-31T10:03:33: PM2 log: App [server:0] online
2018-12-31T10:48:51: PM2 log: Stopping app:server id:0
2018-12-31T10:48:51: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-31T10:48:51: PM2 log: pid=28552 msg=process killed
2018-12-31T10:48:51: PM2 log: App [server:0] starting in -fork mode-
2018-12-31T10:48:51: PM2 log: App [server:0] online


On the production, I use pm2 to start my server.



Here is my server.js file:



var express = require('express'),
app = express(),
port = process.env.PORT || 3000;

const morgan = require('morgan');

const routes = require('./api/routes/main_routes');

const authMiddleware = require('./api/authorization/auth');


bodyParser = require('body-parser');

app.use(morgan(':remote-addr - :remote-user [:date[clf]] ":method :url
HTTP/:http-version" :status :response-time ms'))
app.use('/images', express.static(__dirname + '/markers'));
app.use('/cars', express.static(__dirname + '/cars'));
app.use('/video', express.static(__dirname + '/video'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(authMiddleware)
routes(app); //register the routes

app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.send(err.message);
});

process.on('unhandledRejection', (reason, p) => {
console.log("Unhandled Rejection at: Promise ", p, " reason: ",
reason);
});

process.on('uncaughtException', function (exception) {
console.log(exception);
});


app.listen(port);
console.log('server started on:' + port);


And package.json file:



   {
"name": "api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"start": "nodemon server.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^1.11.0"
},
"dependencies": {
"axios": "^0.18.0",
"axios-cache-adapter": "^2.1.1",
"body-parser": "^1.17.2",
"cachios": "^2.0.0",
"cors": "^2.8.4",
"express": "^4.15.4",
"file-system": "^2.2.2",
"form-data": "^2.3.3",
"geolib": "^2.0.24",
"lodash": "^4.17.4",
"morgan": "^1.9.1",
"nodemon": "^1.18.6",
"path": "^0.12.7",
"sharp": "^0.18.4",
"websocket": "^1.0.28"
}
}


Full pm2 logs



            /root/.pm2/pm2.log last 15 lines:
PM2 | 2018-12-29T09:41:23: PM2 log: Stopping app:server id:0
PM2 | 2018-12-29T09:41:23: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
PM2 | 2018-12-29T09:41:23: PM2 log: pid=11341 msg=process killed
PM2 | 2018-12-29T09:41:23: PM2 log: App [server:0] starting in -fork mode-
PM2 | 2018-12-29T09:41:23: PM2 log: App [server:0] online
PM2 | 2018-12-31T10:03:33: PM2 log: Stopping app:server id:0
PM2 | 2018-12-31T10:03:33: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
PM2 | 2018-12-31T10:03:33: PM2 log: pid=12337 msg=process killed
PM2 | 2018-12-31T10:03:33: PM2 log: App [server:0] starting in -fork mode-
PM2 | 2018-12-31T10:03:33: PM2 log: App [server:0] online
PM2 | 2018-12-31T10:48:51: PM2 log: Stopping app:server id:0
PM2 | 2018-12-31T10:48:51: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
PM2 | 2018-12-31T10:48:51: PM2 log: pid=28552 msg=process killed
PM2 | 2018-12-31T10:48:51: PM2 log: App [server:0] starting in -fork mode-
PM2 | 2018-12-31T10:48:51: PM2 log: App [server:0] online

/root/.pm2/logs/server-error.log last 15 lines:
/root/.pm2/logs/server-out.log last 15 lines:
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:27 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.849 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:27 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.667 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:27 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.567 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 1.065 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 1.302 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.550 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.566 ms
0|server | 211
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:30 +0000] "GET /vehicles?user_lat=52.14601814935841&user_lng=21.04796773265618&center_lng=21.047964502570746&center_lat=52.14602095031964 HTTP/1.1" 200 5357.940 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.624 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.613 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.600 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.520 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.567 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.416 ms









share|improve this question

























  • Do you log node.js errors ? and incoming requests to your server ? i mean. What you see in pm2 logs

    – Maielo
    Dec 31 '18 at 11:38








  • 1





    Is it all requests or just /vehicles?...

    – Stamos
    Dec 31 '18 at 12:02











  • @Stamos there is only one endpoint, but I also provide a path for images.

    – Damian Kwasniak
    Dec 31 '18 at 16:11













  • @Maielo I added a full output to the question.

    – Damian Kwasniak
    Dec 31 '18 at 16:15











  • Can you share the /vehicle code?

    – Stamos
    Jan 2 at 9:08


















0















I recently pushed my node js and express server to the production.
After a few days of working on the production, I realized that sometimes with no reason my server stops responding for requests.



[31/Dec/2018:09:55:39 +0000] "GET /vehicles?user_lat=52.458876&user_lng=16.9006237 HTTP/1.1" - - ms


I was looking for an answer here but the only advice I found was to debug server and find out what is causing it crashes. Unfortunately, I am not able to find out where is the problem. I added listeners to the process to find unhandled promises rejection and uncaught exception but nothing was caught.



What I also found is when the server stops responding the CPU usage is
running high. Normally it takes 1-5%, but when it hangs it is around 20%.



The next strange thing for me is the output of pm2.log file. According to it, my server often restarts but I don't understand why.



pm2.log file:



        2018-12-22T10:12:10: PM2 log: pid=28427 msg=process killed
2018-12-22T10:12:10: PM2 log: App [server:0] starting in -fork mode-
2018-12-22T10:12:10: PM2 log: App [server:0] online
2018-12-24T13:33:40: PM2 log: Stopping app:server id:0
2018-12-24T13:33:40: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-24T13:33:41: PM2 log: pid=31380 msg=process killed
2018-12-24T13:33:41: PM2 log: App [server:0] starting in -fork mode-
2018-12-24T13:33:41: PM2 log: App [server:0] online
2018-12-24T13:45:29: PM2 log: Stopping app:server id:0
2018-12-24T13:45:29: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-24T13:45:29: PM2 log: pid=5521 msg=process killed
2018-12-24T13:45:29: PM2 log: App [server:0] starting in -fork mode-
2018-12-24T13:45:29: PM2 log: App [server:0] online
2018-12-25T03:11:40: PM2 log: Stopping app:server id:0
2018-12-25T03:11:40: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-25T03:11:40: PM2 log: pid=6049 msg=process killed
2018-12-25T03:11:40: PM2 log: App [server:0] starting in -fork mode-
2018-12-25T03:11:40: PM2 log: App [server:0] online
2018-12-27T05:07:30: PM2 log: Stopping app:server id:0
2018-12-27T05:07:30: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-27T05:07:30: PM2 log: pid=11531 msg=process killed
2018-12-27T05:07:30: PM2 log: App [server:0] starting in -fork mode-
2018-12-27T05:07:30: PM2 log: App [server:0] online
2018-12-29T08:08:27: PM2 log: Stopping app:server id:0
2018-12-29T08:08:27: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-29T08:08:27: PM2 log: pid=28263 msg=process killed
2018-12-29T08:08:27: PM2 log: App [server:0] starting in -fork mode-
2018-12-29T08:08:27: PM2 log: App [server:0] online
2018-12-29T09:41:23: PM2 log: Stopping app:server id:0
2018-12-29T09:41:23: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-29T09:41:23: PM2 log: pid=11341 msg=process killed
2018-12-29T09:41:23: PM2 log: App [server:0] starting in -fork mode-
2018-12-29T09:41:23: PM2 log: App [server:0] online
2018-12-31T10:03:33: PM2 log: Stopping app:server id:0
2018-12-31T10:03:33: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-31T10:03:33: PM2 log: pid=12337 msg=process killed
2018-12-31T10:03:33: PM2 log: App [server:0] starting in -fork mode-
2018-12-31T10:03:33: PM2 log: App [server:0] online
2018-12-31T10:48:51: PM2 log: Stopping app:server id:0
2018-12-31T10:48:51: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-31T10:48:51: PM2 log: pid=28552 msg=process killed
2018-12-31T10:48:51: PM2 log: App [server:0] starting in -fork mode-
2018-12-31T10:48:51: PM2 log: App [server:0] online


On the production, I use pm2 to start my server.



Here is my server.js file:



var express = require('express'),
app = express(),
port = process.env.PORT || 3000;

const morgan = require('morgan');

const routes = require('./api/routes/main_routes');

const authMiddleware = require('./api/authorization/auth');


bodyParser = require('body-parser');

app.use(morgan(':remote-addr - :remote-user [:date[clf]] ":method :url
HTTP/:http-version" :status :response-time ms'))
app.use('/images', express.static(__dirname + '/markers'));
app.use('/cars', express.static(__dirname + '/cars'));
app.use('/video', express.static(__dirname + '/video'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(authMiddleware)
routes(app); //register the routes

app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.send(err.message);
});

process.on('unhandledRejection', (reason, p) => {
console.log("Unhandled Rejection at: Promise ", p, " reason: ",
reason);
});

process.on('uncaughtException', function (exception) {
console.log(exception);
});


app.listen(port);
console.log('server started on:' + port);


And package.json file:



   {
"name": "api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"start": "nodemon server.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^1.11.0"
},
"dependencies": {
"axios": "^0.18.0",
"axios-cache-adapter": "^2.1.1",
"body-parser": "^1.17.2",
"cachios": "^2.0.0",
"cors": "^2.8.4",
"express": "^4.15.4",
"file-system": "^2.2.2",
"form-data": "^2.3.3",
"geolib": "^2.0.24",
"lodash": "^4.17.4",
"morgan": "^1.9.1",
"nodemon": "^1.18.6",
"path": "^0.12.7",
"sharp": "^0.18.4",
"websocket": "^1.0.28"
}
}


Full pm2 logs



            /root/.pm2/pm2.log last 15 lines:
PM2 | 2018-12-29T09:41:23: PM2 log: Stopping app:server id:0
PM2 | 2018-12-29T09:41:23: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
PM2 | 2018-12-29T09:41:23: PM2 log: pid=11341 msg=process killed
PM2 | 2018-12-29T09:41:23: PM2 log: App [server:0] starting in -fork mode-
PM2 | 2018-12-29T09:41:23: PM2 log: App [server:0] online
PM2 | 2018-12-31T10:03:33: PM2 log: Stopping app:server id:0
PM2 | 2018-12-31T10:03:33: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
PM2 | 2018-12-31T10:03:33: PM2 log: pid=12337 msg=process killed
PM2 | 2018-12-31T10:03:33: PM2 log: App [server:0] starting in -fork mode-
PM2 | 2018-12-31T10:03:33: PM2 log: App [server:0] online
PM2 | 2018-12-31T10:48:51: PM2 log: Stopping app:server id:0
PM2 | 2018-12-31T10:48:51: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
PM2 | 2018-12-31T10:48:51: PM2 log: pid=28552 msg=process killed
PM2 | 2018-12-31T10:48:51: PM2 log: App [server:0] starting in -fork mode-
PM2 | 2018-12-31T10:48:51: PM2 log: App [server:0] online

/root/.pm2/logs/server-error.log last 15 lines:
/root/.pm2/logs/server-out.log last 15 lines:
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:27 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.849 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:27 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.667 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:27 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.567 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 1.065 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 1.302 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.550 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.566 ms
0|server | 211
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:30 +0000] "GET /vehicles?user_lat=52.14601814935841&user_lng=21.04796773265618&center_lng=21.047964502570746&center_lat=52.14602095031964 HTTP/1.1" 200 5357.940 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.624 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.613 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.600 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.520 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.567 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.416 ms









share|improve this question

























  • Do you log node.js errors ? and incoming requests to your server ? i mean. What you see in pm2 logs

    – Maielo
    Dec 31 '18 at 11:38








  • 1





    Is it all requests or just /vehicles?...

    – Stamos
    Dec 31 '18 at 12:02











  • @Stamos there is only one endpoint, but I also provide a path for images.

    – Damian Kwasniak
    Dec 31 '18 at 16:11













  • @Maielo I added a full output to the question.

    – Damian Kwasniak
    Dec 31 '18 at 16:15











  • Can you share the /vehicle code?

    – Stamos
    Jan 2 at 9:08
















0












0








0








I recently pushed my node js and express server to the production.
After a few days of working on the production, I realized that sometimes with no reason my server stops responding for requests.



[31/Dec/2018:09:55:39 +0000] "GET /vehicles?user_lat=52.458876&user_lng=16.9006237 HTTP/1.1" - - ms


I was looking for an answer here but the only advice I found was to debug server and find out what is causing it crashes. Unfortunately, I am not able to find out where is the problem. I added listeners to the process to find unhandled promises rejection and uncaught exception but nothing was caught.



What I also found is when the server stops responding the CPU usage is
running high. Normally it takes 1-5%, but when it hangs it is around 20%.



The next strange thing for me is the output of pm2.log file. According to it, my server often restarts but I don't understand why.



pm2.log file:



        2018-12-22T10:12:10: PM2 log: pid=28427 msg=process killed
2018-12-22T10:12:10: PM2 log: App [server:0] starting in -fork mode-
2018-12-22T10:12:10: PM2 log: App [server:0] online
2018-12-24T13:33:40: PM2 log: Stopping app:server id:0
2018-12-24T13:33:40: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-24T13:33:41: PM2 log: pid=31380 msg=process killed
2018-12-24T13:33:41: PM2 log: App [server:0] starting in -fork mode-
2018-12-24T13:33:41: PM2 log: App [server:0] online
2018-12-24T13:45:29: PM2 log: Stopping app:server id:0
2018-12-24T13:45:29: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-24T13:45:29: PM2 log: pid=5521 msg=process killed
2018-12-24T13:45:29: PM2 log: App [server:0] starting in -fork mode-
2018-12-24T13:45:29: PM2 log: App [server:0] online
2018-12-25T03:11:40: PM2 log: Stopping app:server id:0
2018-12-25T03:11:40: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-25T03:11:40: PM2 log: pid=6049 msg=process killed
2018-12-25T03:11:40: PM2 log: App [server:0] starting in -fork mode-
2018-12-25T03:11:40: PM2 log: App [server:0] online
2018-12-27T05:07:30: PM2 log: Stopping app:server id:0
2018-12-27T05:07:30: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-27T05:07:30: PM2 log: pid=11531 msg=process killed
2018-12-27T05:07:30: PM2 log: App [server:0] starting in -fork mode-
2018-12-27T05:07:30: PM2 log: App [server:0] online
2018-12-29T08:08:27: PM2 log: Stopping app:server id:0
2018-12-29T08:08:27: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-29T08:08:27: PM2 log: pid=28263 msg=process killed
2018-12-29T08:08:27: PM2 log: App [server:0] starting in -fork mode-
2018-12-29T08:08:27: PM2 log: App [server:0] online
2018-12-29T09:41:23: PM2 log: Stopping app:server id:0
2018-12-29T09:41:23: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-29T09:41:23: PM2 log: pid=11341 msg=process killed
2018-12-29T09:41:23: PM2 log: App [server:0] starting in -fork mode-
2018-12-29T09:41:23: PM2 log: App [server:0] online
2018-12-31T10:03:33: PM2 log: Stopping app:server id:0
2018-12-31T10:03:33: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-31T10:03:33: PM2 log: pid=12337 msg=process killed
2018-12-31T10:03:33: PM2 log: App [server:0] starting in -fork mode-
2018-12-31T10:03:33: PM2 log: App [server:0] online
2018-12-31T10:48:51: PM2 log: Stopping app:server id:0
2018-12-31T10:48:51: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-31T10:48:51: PM2 log: pid=28552 msg=process killed
2018-12-31T10:48:51: PM2 log: App [server:0] starting in -fork mode-
2018-12-31T10:48:51: PM2 log: App [server:0] online


On the production, I use pm2 to start my server.



Here is my server.js file:



var express = require('express'),
app = express(),
port = process.env.PORT || 3000;

const morgan = require('morgan');

const routes = require('./api/routes/main_routes');

const authMiddleware = require('./api/authorization/auth');


bodyParser = require('body-parser');

app.use(morgan(':remote-addr - :remote-user [:date[clf]] ":method :url
HTTP/:http-version" :status :response-time ms'))
app.use('/images', express.static(__dirname + '/markers'));
app.use('/cars', express.static(__dirname + '/cars'));
app.use('/video', express.static(__dirname + '/video'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(authMiddleware)
routes(app); //register the routes

app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.send(err.message);
});

process.on('unhandledRejection', (reason, p) => {
console.log("Unhandled Rejection at: Promise ", p, " reason: ",
reason);
});

process.on('uncaughtException', function (exception) {
console.log(exception);
});


app.listen(port);
console.log('server started on:' + port);


And package.json file:



   {
"name": "api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"start": "nodemon server.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^1.11.0"
},
"dependencies": {
"axios": "^0.18.0",
"axios-cache-adapter": "^2.1.1",
"body-parser": "^1.17.2",
"cachios": "^2.0.0",
"cors": "^2.8.4",
"express": "^4.15.4",
"file-system": "^2.2.2",
"form-data": "^2.3.3",
"geolib": "^2.0.24",
"lodash": "^4.17.4",
"morgan": "^1.9.1",
"nodemon": "^1.18.6",
"path": "^0.12.7",
"sharp": "^0.18.4",
"websocket": "^1.0.28"
}
}


Full pm2 logs



            /root/.pm2/pm2.log last 15 lines:
PM2 | 2018-12-29T09:41:23: PM2 log: Stopping app:server id:0
PM2 | 2018-12-29T09:41:23: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
PM2 | 2018-12-29T09:41:23: PM2 log: pid=11341 msg=process killed
PM2 | 2018-12-29T09:41:23: PM2 log: App [server:0] starting in -fork mode-
PM2 | 2018-12-29T09:41:23: PM2 log: App [server:0] online
PM2 | 2018-12-31T10:03:33: PM2 log: Stopping app:server id:0
PM2 | 2018-12-31T10:03:33: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
PM2 | 2018-12-31T10:03:33: PM2 log: pid=12337 msg=process killed
PM2 | 2018-12-31T10:03:33: PM2 log: App [server:0] starting in -fork mode-
PM2 | 2018-12-31T10:03:33: PM2 log: App [server:0] online
PM2 | 2018-12-31T10:48:51: PM2 log: Stopping app:server id:0
PM2 | 2018-12-31T10:48:51: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
PM2 | 2018-12-31T10:48:51: PM2 log: pid=28552 msg=process killed
PM2 | 2018-12-31T10:48:51: PM2 log: App [server:0] starting in -fork mode-
PM2 | 2018-12-31T10:48:51: PM2 log: App [server:0] online

/root/.pm2/logs/server-error.log last 15 lines:
/root/.pm2/logs/server-out.log last 15 lines:
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:27 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.849 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:27 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.667 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:27 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.567 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 1.065 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 1.302 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.550 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.566 ms
0|server | 211
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:30 +0000] "GET /vehicles?user_lat=52.14601814935841&user_lng=21.04796773265618&center_lng=21.047964502570746&center_lat=52.14602095031964 HTTP/1.1" 200 5357.940 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.624 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.613 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.600 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.520 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.567 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.416 ms









share|improve this question
















I recently pushed my node js and express server to the production.
After a few days of working on the production, I realized that sometimes with no reason my server stops responding for requests.



[31/Dec/2018:09:55:39 +0000] "GET /vehicles?user_lat=52.458876&user_lng=16.9006237 HTTP/1.1" - - ms


I was looking for an answer here but the only advice I found was to debug server and find out what is causing it crashes. Unfortunately, I am not able to find out where is the problem. I added listeners to the process to find unhandled promises rejection and uncaught exception but nothing was caught.



What I also found is when the server stops responding the CPU usage is
running high. Normally it takes 1-5%, but when it hangs it is around 20%.



The next strange thing for me is the output of pm2.log file. According to it, my server often restarts but I don't understand why.



pm2.log file:



        2018-12-22T10:12:10: PM2 log: pid=28427 msg=process killed
2018-12-22T10:12:10: PM2 log: App [server:0] starting in -fork mode-
2018-12-22T10:12:10: PM2 log: App [server:0] online
2018-12-24T13:33:40: PM2 log: Stopping app:server id:0
2018-12-24T13:33:40: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-24T13:33:41: PM2 log: pid=31380 msg=process killed
2018-12-24T13:33:41: PM2 log: App [server:0] starting in -fork mode-
2018-12-24T13:33:41: PM2 log: App [server:0] online
2018-12-24T13:45:29: PM2 log: Stopping app:server id:0
2018-12-24T13:45:29: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-24T13:45:29: PM2 log: pid=5521 msg=process killed
2018-12-24T13:45:29: PM2 log: App [server:0] starting in -fork mode-
2018-12-24T13:45:29: PM2 log: App [server:0] online
2018-12-25T03:11:40: PM2 log: Stopping app:server id:0
2018-12-25T03:11:40: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-25T03:11:40: PM2 log: pid=6049 msg=process killed
2018-12-25T03:11:40: PM2 log: App [server:0] starting in -fork mode-
2018-12-25T03:11:40: PM2 log: App [server:0] online
2018-12-27T05:07:30: PM2 log: Stopping app:server id:0
2018-12-27T05:07:30: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-27T05:07:30: PM2 log: pid=11531 msg=process killed
2018-12-27T05:07:30: PM2 log: App [server:0] starting in -fork mode-
2018-12-27T05:07:30: PM2 log: App [server:0] online
2018-12-29T08:08:27: PM2 log: Stopping app:server id:0
2018-12-29T08:08:27: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-29T08:08:27: PM2 log: pid=28263 msg=process killed
2018-12-29T08:08:27: PM2 log: App [server:0] starting in -fork mode-
2018-12-29T08:08:27: PM2 log: App [server:0] online
2018-12-29T09:41:23: PM2 log: Stopping app:server id:0
2018-12-29T09:41:23: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-29T09:41:23: PM2 log: pid=11341 msg=process killed
2018-12-29T09:41:23: PM2 log: App [server:0] starting in -fork mode-
2018-12-29T09:41:23: PM2 log: App [server:0] online
2018-12-31T10:03:33: PM2 log: Stopping app:server id:0
2018-12-31T10:03:33: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-31T10:03:33: PM2 log: pid=12337 msg=process killed
2018-12-31T10:03:33: PM2 log: App [server:0] starting in -fork mode-
2018-12-31T10:03:33: PM2 log: App [server:0] online
2018-12-31T10:48:51: PM2 log: Stopping app:server id:0
2018-12-31T10:48:51: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
2018-12-31T10:48:51: PM2 log: pid=28552 msg=process killed
2018-12-31T10:48:51: PM2 log: App [server:0] starting in -fork mode-
2018-12-31T10:48:51: PM2 log: App [server:0] online


On the production, I use pm2 to start my server.



Here is my server.js file:



var express = require('express'),
app = express(),
port = process.env.PORT || 3000;

const morgan = require('morgan');

const routes = require('./api/routes/main_routes');

const authMiddleware = require('./api/authorization/auth');


bodyParser = require('body-parser');

app.use(morgan(':remote-addr - :remote-user [:date[clf]] ":method :url
HTTP/:http-version" :status :response-time ms'))
app.use('/images', express.static(__dirname + '/markers'));
app.use('/cars', express.static(__dirname + '/cars'));
app.use('/video', express.static(__dirname + '/video'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(authMiddleware)
routes(app); //register the routes

app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.send(err.message);
});

process.on('unhandledRejection', (reason, p) => {
console.log("Unhandled Rejection at: Promise ", p, " reason: ",
reason);
});

process.on('uncaughtException', function (exception) {
console.log(exception);
});


app.listen(port);
console.log('server started on:' + port);


And package.json file:



   {
"name": "api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"start": "nodemon server.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^1.11.0"
},
"dependencies": {
"axios": "^0.18.0",
"axios-cache-adapter": "^2.1.1",
"body-parser": "^1.17.2",
"cachios": "^2.0.0",
"cors": "^2.8.4",
"express": "^4.15.4",
"file-system": "^2.2.2",
"form-data": "^2.3.3",
"geolib": "^2.0.24",
"lodash": "^4.17.4",
"morgan": "^1.9.1",
"nodemon": "^1.18.6",
"path": "^0.12.7",
"sharp": "^0.18.4",
"websocket": "^1.0.28"
}
}


Full pm2 logs



            /root/.pm2/pm2.log last 15 lines:
PM2 | 2018-12-29T09:41:23: PM2 log: Stopping app:server id:0
PM2 | 2018-12-29T09:41:23: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
PM2 | 2018-12-29T09:41:23: PM2 log: pid=11341 msg=process killed
PM2 | 2018-12-29T09:41:23: PM2 log: App [server:0] starting in -fork mode-
PM2 | 2018-12-29T09:41:23: PM2 log: App [server:0] online
PM2 | 2018-12-31T10:03:33: PM2 log: Stopping app:server id:0
PM2 | 2018-12-31T10:03:33: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
PM2 | 2018-12-31T10:03:33: PM2 log: pid=12337 msg=process killed
PM2 | 2018-12-31T10:03:33: PM2 log: App [server:0] starting in -fork mode-
PM2 | 2018-12-31T10:03:33: PM2 log: App [server:0] online
PM2 | 2018-12-31T10:48:51: PM2 log: Stopping app:server id:0
PM2 | 2018-12-31T10:48:51: PM2 log: App [server:0] exited with code [0] via signal [SIGINT]
PM2 | 2018-12-31T10:48:51: PM2 log: pid=28552 msg=process killed
PM2 | 2018-12-31T10:48:51: PM2 log: App [server:0] starting in -fork mode-
PM2 | 2018-12-31T10:48:51: PM2 log: App [server:0] online

/root/.pm2/logs/server-error.log last 15 lines:
/root/.pm2/logs/server-out.log last 15 lines:
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:27 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.849 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:27 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.667 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:27 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.567 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 1.065 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 1.302 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.550 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:28 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.566 ms
0|server | 211
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:30 +0000] "GET /vehicles?user_lat=52.14601814935841&user_lng=21.04796773265618&center_lng=21.047964502570746&center_lat=52.14602095031964 HTTP/1.1" 200 5357.940 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.624 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.613 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.600 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.520 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_active.png HTTP/1.1" 200 0.567 ms
0|server | ::ffff:127.0.0.1 - - [31/Dec/2018:16:11:31 +0000] "GET /images/marker_inactive.png HTTP/1.1" 200 0.416 ms






node.js express server pm2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 17:24







Damian Kwasniak

















asked Dec 31 '18 at 11:25









Damian KwasniakDamian Kwasniak

14




14













  • Do you log node.js errors ? and incoming requests to your server ? i mean. What you see in pm2 logs

    – Maielo
    Dec 31 '18 at 11:38








  • 1





    Is it all requests or just /vehicles?...

    – Stamos
    Dec 31 '18 at 12:02











  • @Stamos there is only one endpoint, but I also provide a path for images.

    – Damian Kwasniak
    Dec 31 '18 at 16:11













  • @Maielo I added a full output to the question.

    – Damian Kwasniak
    Dec 31 '18 at 16:15











  • Can you share the /vehicle code?

    – Stamos
    Jan 2 at 9:08





















  • Do you log node.js errors ? and incoming requests to your server ? i mean. What you see in pm2 logs

    – Maielo
    Dec 31 '18 at 11:38








  • 1





    Is it all requests or just /vehicles?...

    – Stamos
    Dec 31 '18 at 12:02











  • @Stamos there is only one endpoint, but I also provide a path for images.

    – Damian Kwasniak
    Dec 31 '18 at 16:11













  • @Maielo I added a full output to the question.

    – Damian Kwasniak
    Dec 31 '18 at 16:15











  • Can you share the /vehicle code?

    – Stamos
    Jan 2 at 9:08



















Do you log node.js errors ? and incoming requests to your server ? i mean. What you see in pm2 logs

– Maielo
Dec 31 '18 at 11:38







Do you log node.js errors ? and incoming requests to your server ? i mean. What you see in pm2 logs

– Maielo
Dec 31 '18 at 11:38






1




1





Is it all requests or just /vehicles?...

– Stamos
Dec 31 '18 at 12:02





Is it all requests or just /vehicles?...

– Stamos
Dec 31 '18 at 12:02













@Stamos there is only one endpoint, but I also provide a path for images.

– Damian Kwasniak
Dec 31 '18 at 16:11







@Stamos there is only one endpoint, but I also provide a path for images.

– Damian Kwasniak
Dec 31 '18 at 16:11















@Maielo I added a full output to the question.

– Damian Kwasniak
Dec 31 '18 at 16:15





@Maielo I added a full output to the question.

– Damian Kwasniak
Dec 31 '18 at 16:15













Can you share the /vehicle code?

– Stamos
Jan 2 at 9:08







Can you share the /vehicle code?

– Stamos
Jan 2 at 9:08














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986887%2fnode-app-stops-responding-after-some-time%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986887%2fnode-app-stops-responding-after-some-time%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Angular Downloading a file using contenturl with Basic Authentication

Olmecas

Can't read property showImagePicker of undefined in react native iOS