admin管理员组文章数量:1022736
I have a simple Node.JS script (just a script, no server), which is supposed to do a thing every 24 hours. But after about 8 hours I see this in the error log:
Error: Connection lost: The server closed the connection.
at Protocol.end (/home/user/dev/app/node_modules/mysql/lib/protocol/Protocol.js:73:13)
at Socket.onend (stream.js:79:10)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:910:16
at process._tickCallback (node.js:415:13)
error: Forever detected script exited with code: 8
error: Forever restarting script for 1 time
I'm sure this is stupid and it's an easy fix, but I just can't find it yet. Any ideas?
I have a simple Node.JS script (just a script, no server), which is supposed to do a thing every 24 hours. But after about 8 hours I see this in the error log:
Error: Connection lost: The server closed the connection.
at Protocol.end (/home/user/dev/app/node_modules/mysql/lib/protocol/Protocol.js:73:13)
at Socket.onend (stream.js:79:10)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:910:16
at process._tickCallback (node.js:415:13)
error: Forever detected script exited with code: 8
error: Forever restarting script for 1 time
I'm sure this is stupid and it's an easy fix, but I just can't find it yet. Any ideas?
Share Improve this question asked Jul 30, 2013 at 8:56 Nikolay DyankovNikolay Dyankov 7,28811 gold badges65 silver badges89 bronze badges 3- 1 Without seeing your code it would be hard to say, for sure. However, the error suggests something to do with your MySQL connection: perhaps -- if you're keeping it open -- this is dropping out after a long period of inactivity. – Xophmeister Commented Jul 30, 2013 at 9:02
- 1 It looks like the connection was closed from the other side. It would be helpful to see some code. – freakish Commented Jul 30, 2013 at 9:03
- I'm making a few $.get requests and some MySQL queries. Code ing in a few minutes. – Nikolay Dyankov Commented Jul 30, 2013 at 9:04
1 Answer
Reset to default 7Looks like you're keeping an open connection to a remote MySQL server. The remote server is closing the connection after 8 hours of idle connection. So you have two options. Either you send a keepalive request or you disconnect after your initial request and then open the connection again when timer initiates the next event.
Keepalive
If you want to send a keepalive request, simply setup a select 1
on an interval timer.
select 1
is just a simple query that will cause MySQL to return a result of 1
and reset the MySQL server's connection timeout. It would look something like this.
function keepalive() {
connection.query('select 1', [], function(err, result) {
if(err) return console.log(err);
// Successul keepalive
});
}
setInterval(keepalive, 1000*60*5);
Pooling
However, you're better off using the mysql module's pool functionality. It will connect to the MySQL server on an as-needed basis and will handle disconnects for you automatically.
I have a simple Node.JS script (just a script, no server), which is supposed to do a thing every 24 hours. But after about 8 hours I see this in the error log:
Error: Connection lost: The server closed the connection.
at Protocol.end (/home/user/dev/app/node_modules/mysql/lib/protocol/Protocol.js:73:13)
at Socket.onend (stream.js:79:10)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:910:16
at process._tickCallback (node.js:415:13)
error: Forever detected script exited with code: 8
error: Forever restarting script for 1 time
I'm sure this is stupid and it's an easy fix, but I just can't find it yet. Any ideas?
I have a simple Node.JS script (just a script, no server), which is supposed to do a thing every 24 hours. But after about 8 hours I see this in the error log:
Error: Connection lost: The server closed the connection.
at Protocol.end (/home/user/dev/app/node_modules/mysql/lib/protocol/Protocol.js:73:13)
at Socket.onend (stream.js:79:10)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:910:16
at process._tickCallback (node.js:415:13)
error: Forever detected script exited with code: 8
error: Forever restarting script for 1 time
I'm sure this is stupid and it's an easy fix, but I just can't find it yet. Any ideas?
Share Improve this question asked Jul 30, 2013 at 8:56 Nikolay DyankovNikolay Dyankov 7,28811 gold badges65 silver badges89 bronze badges 3- 1 Without seeing your code it would be hard to say, for sure. However, the error suggests something to do with your MySQL connection: perhaps -- if you're keeping it open -- this is dropping out after a long period of inactivity. – Xophmeister Commented Jul 30, 2013 at 9:02
- 1 It looks like the connection was closed from the other side. It would be helpful to see some code. – freakish Commented Jul 30, 2013 at 9:03
- I'm making a few $.get requests and some MySQL queries. Code ing in a few minutes. – Nikolay Dyankov Commented Jul 30, 2013 at 9:04
1 Answer
Reset to default 7Looks like you're keeping an open connection to a remote MySQL server. The remote server is closing the connection after 8 hours of idle connection. So you have two options. Either you send a keepalive request or you disconnect after your initial request and then open the connection again when timer initiates the next event.
Keepalive
If you want to send a keepalive request, simply setup a select 1
on an interval timer.
select 1
is just a simple query that will cause MySQL to return a result of 1
and reset the MySQL server's connection timeout. It would look something like this.
function keepalive() {
connection.query('select 1', [], function(err, result) {
if(err) return console.log(err);
// Successul keepalive
});
}
setInterval(keepalive, 1000*60*5);
Pooling
However, you're better off using the mysql module's pool functionality. It will connect to the MySQL server on an as-needed basis and will handle disconnects for you automatically.
本文标签: javascriptNodejssetIntervalConnection lost The server closed the connectionStack Overflow
版权声明:本文标题:javascript - Node.js + setInterval = Connection lost: The server closed the connection - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745546264a2155407.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论