最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js + setInterval = Connection lost: The server closed the connection - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 7

Looks 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.

发布评论

评论列表(0)

  1. 暂无评论