-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
restart a hang process of node.js #1072
Comments
Can you |
I restart it manually, not so special at all
|
So after the manual restart it still doesn't work ? |
After manual restart it works again. Yes that I mean. Is pm2 can detect such scenario? because right now, PM2 Can't detect if node hang. I want pm2 to detect it, so I don't restart it manually. I'm tired, because user get my site down, most of the time. This happens to me because of node 0.12.0. I think that is the problem, the node process is quickly becomes hang (maybe because of aggressive of GC, max sockets Infinity). Especially, when I activated pm2 keymetrics, with pmx. That node process hangs more quickly. Until now, I don't know what causes the problem. I can't backward to node 0.10.34. Which is has uptime 1 - 2 months (if I am not updating my code). Because I set package.json, latest to all my package. Still, I just want such feature, PM2 can detect hang node.js process. So I am not restarting it manually again, and not watching my realtime analytics that user is 0 (indicates that the site is down) every minutes. |
Ok, I think the only way to do that is by using PM2 programmatically. So there is file pm2.js : var pm2 = require('pm2');
// Connect or launch PM2
pm2.connect(function(err) {
// Start a script on the current folder
pm2.start('app.js', { name: 'myApp' }, function(err, proc) {
if (err) throw new Error(err);
setInterval(function() {
// Get all processes running
pm2.list(function(err, process_list) {
if (err) throw new Error(err);
process_list.forEach(function(proc) {
console.log(proc.pm2_env.axm_monitor);
if (proc.pm2_env.axm_monitor['User count'] > 60) {
pm2.restart(proc.pm2_env.pm_id, function() {});
}
});
});
}, 2000);
});
}); And then there is file app.js : var pmx = require('pmx');
var probe = pmx.probe();
var user_count = 0;
var user = probe.metric({
name : 'User count',
value : function() { return user_count; }
});
setInterval(function() {
console.log('hey');
user_count += 9;
}, 1000); No need to say you have to Some further documentation on using pm2 programmatically here : https://github.com/Unitech/PM2/blob/development/ADVANCED_README.md#using-pm2-programmatically If you have any questions I'll be glad to answer them :) |
Hmm I read the code, it's restart when user more than 60, it will restart |
You said that when your 'real-time user' counter was 0 it means the server is down. |
I count user manually via google analytics realtime by watching it. That user not visiting my site, thus my site down It will be complicated it I count it via pmx |
Ok then you can just ping your server and if it doesn't respond restart it :) var request = require('request');
var pm2 = require('pm2');
var ping_timeout = 3000;
var pm2_app_name = 'app';
var worker_interval = 1000;
var hostname = 'http://localhost:8000/';
var ping_server = function(timeout, callb) {
var timer = setTimeout(function() {
callb(false);
}, timeout);
request(hostname, function(err, res, body) {
clearTimeout(timer);
if (err || res.statusCode != 200)
return callb(false);
callb(true);
});
};
pm2.connect(function(err) {
if (err) throw err;
setTimeout(function worker() {
ping_server(ping_timeout, function(ok) {
if (!ok) {
console.log('server down, restarting it');
pm2.restart(pm2_app_name, function() {});
}
else {
console.log('server up');
}
setTimeout(worker, worker_interval);
});
}, worker_interval);
}); You can of course launch this worker with pm2 itself. |
That would be double allocate resources, since node 0.12.0 use maxsocket infinity, and my node process is nearly 100% that is 99%~ Also I can't strace it (it's return empty logs) Thanks I will try your code |
[update] Even I can't restart my node.js if hang, can't do pm2 restart myapps manually too.. |
Which version of pm2 are you using already ? |
0.12.0 |
I need this too, PM2 version is 0.14.3 It doesn't restart when my bot hangs |
Is there any update for this problem? |
It is an old issue, has this been implemented since 2015? Can pm2 automatically ping the service and restart it if it doesn't respond? |
For processes that were having this issue, I've had to move over to using systemd, seems to resolve the hanging issue and still no idea what causes this issue at its core. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I have a hunging process in PM2, when i debug with node i got this:
in my app I use setTimout function in websocket heartbeat |
@mwarhead create a new issue for it, then no point in necroing an issue. |
Well, suppose I have website with 10K - 50K hits (include crawler)
My node.js server is hang. Node process doesn't exit, etc
How pm2 handle that?
Because it just happened to me. Indications of process hang is:
Thanks
The text was updated successfully, but these errors were encountered: