исправлен дисконект от базы
This commit is contained in:
parent
5059beeaf0
commit
e988f11891
85
index.js
85
index.js
@ -16,8 +16,50 @@ const mysqlSettings = {
|
|||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
server.listen(8095);
|
server.listen(8095);
|
||||||
let listenerMysql = mysql.createConnection(mysqlSettings);
|
|
||||||
let socketMysql = mysql.createConnection(mysqlSettings);
|
let socketMysql;
|
||||||
|
function handleSocketDisconnect() {
|
||||||
|
socketMysql = mysql.createConnection(mysqlSettings);
|
||||||
|
|
||||||
|
socketMysql.connect(function(mysqlError) {
|
||||||
|
if (mysqlError) {
|
||||||
|
console.error('error connecting: ' + mysqlError.stack);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
socketMysql.on('error', function(err) {
|
||||||
|
console.log('db error', err);
|
||||||
|
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
|
||||||
|
handleSocketDisconnect(); // lost due to either server restart, or a
|
||||||
|
} else { // connnection idle timeout (the wait_timeout
|
||||||
|
throw err; // server variable configures this)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
handleSocketDisconnect();
|
||||||
|
|
||||||
|
let listenerMysql;
|
||||||
|
function handleListenerDisconnect() {
|
||||||
|
listenerMysql = mysql.createConnection(mysqlSettings);
|
||||||
|
|
||||||
|
listenerMysql.connect(function(mysqlError) {
|
||||||
|
if (mysqlError) {
|
||||||
|
console.error('error connecting: ' + mysqlError.stack);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
listenerMysql.on('error', function(err) {
|
||||||
|
console.log('db error', err);
|
||||||
|
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
|
||||||
|
handleListenerDisconnect(); // lost due to either server restart, or a
|
||||||
|
} else { // connnection idle timeout (the wait_timeout
|
||||||
|
throw err; // server variable configures this)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
handleListenerDisconnect();
|
||||||
|
|
||||||
|
|
||||||
app.get('/', function(req, res)
|
app.get('/', function(req, res)
|
||||||
@ -27,14 +69,8 @@ app.get('/', function(req, res)
|
|||||||
|
|
||||||
app.use(express.static('build'));
|
app.use(express.static('build'));
|
||||||
|
|
||||||
socketMysql.connect(function(mysqlError) {
|
io.on('connection', function(socket)
|
||||||
if (mysqlError) {
|
{
|
||||||
console.error('error connecting: ' + mysqlError.stack);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
io.on('connection', function(socket)
|
|
||||||
{
|
|
||||||
function baseRowData(row)
|
function baseRowData(row)
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
@ -115,34 +151,26 @@ socketMysql.connect(function(mysqlError) {
|
|||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
listenerMysql.connect(function(err) {
|
let undoneQueries = 0;
|
||||||
if (err) {
|
let pushDatabaseBalance = () => {
|
||||||
console.error('error connecting: ' + err.stack);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let undoneQueries = 0;
|
|
||||||
let pushDatabaseBalance = () => {
|
|
||||||
undoneQueries++;
|
undoneQueries++;
|
||||||
if(undoneQueries >= 5000)
|
if(undoneQueries >= 5000)
|
||||||
{
|
{
|
||||||
console.log('too much freeze mysql connection. doing balance');
|
console.log('too much freeze mysql connection. doing balance');
|
||||||
spider.ignore = true;
|
spider.ignore = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let popDatabaseBalance = () => {
|
let popDatabaseBalance = () => {
|
||||||
undoneQueries--;
|
undoneQueries--;
|
||||||
if(undoneQueries == 0)
|
if(undoneQueries == 0)
|
||||||
{
|
{
|
||||||
spider.ignore = false;
|
spider.ignore = false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
client.on('complete', function (metadata, infohash, rinfo) {
|
client.on('complete', function (metadata, infohash, rinfo) {
|
||||||
console.log('writing torrent to db');
|
console.log('writing torrent to db');
|
||||||
const hash = infohash.toString('hex');
|
const hash = infohash.toString('hex');
|
||||||
let size = metadata.info.length ? metadata.info.length : 0;
|
let size = metadata.info.length ? metadata.info.length : 0;
|
||||||
@ -221,9 +249,8 @@ listenerMysql.connect(function(err) {
|
|||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
// spider.on('nodes', (nodes)=>console.log('foundNodes'))
|
|
||||||
|
|
||||||
spider.listen(4445)
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// spider.on('nodes', (nodes)=>console.log('foundNodes'))
|
||||||
|
|
||||||
|
spider.listen(4445)
|
Loading…
Reference in New Issue
Block a user