fix(closing): fix errors on closing

This commit is contained in:
Alexey Kasyanchuk 2018-08-08 00:58:58 +03:00
parent ddfd43498d
commit 463eb7cce7
3 changed files with 38 additions and 18 deletions

View File

@ -210,10 +210,6 @@ module.exports = async ({
if(typeof callback != 'function') if(typeof callback != 'function')
return; return;
// ignore sql requests on closing
if(sphinxSingle.state === 'disconnected')
return
const cpu = cpuUsage() const cpu = cpuUsage()
const limit = Math.max(1, 5 - (cpu / 20) | 0) const limit = Math.max(1, 5 - (cpu / 20) | 0)

View File

@ -2,7 +2,15 @@ const mysql = require('mysql');
const config = require('./config'); const config = require('./config');
const expand = (sphinx) => { const expand = (sphinx) => {
const queryCall = sphinx.query.bind(sphinx) const queryOriginal = sphinx.query.bind(sphinx)
const queryCall = (...args) => {
if(sphinx.__closed)
{
logT('sql', 'prevent sql request after end of connection')
return
}
return queryOriginal(...args)
}
sphinx.query = (sql, args, callback) => new Promise((resolve, reject) => { sphinx.query = (sql, args, callback) => new Promise((resolve, reject) => {
if(typeof args === 'function' || typeof args === 'undefined') if(typeof args === 'function' || typeof args === 'undefined')
@ -125,10 +133,13 @@ const pool = async () => {
}); });
sphinx = expand(sphinx) sphinx = expand(sphinx)
const end = sphinx.end.bind(sphinx) const end = sphinx.end.bind(sphinx)
sphinx.end = async (cb) => new Promise(resolve => end(() => { sphinx.end = (cb) => new Promise(resolve => {
resolve() sphinx.__closed = true
if(cb) cb() end(() => {
})) resolve()
if(cb) cb()
})
})
return sphinx return sphinx
} }
else else
@ -161,7 +172,7 @@ const pool = async () => {
}, },
async end(cb) async end(cb)
{ {
await Promise.all(connectionPool.map(conn => new Promise(resolve => conn.end(resolve)))) await Promise.all(connectionPool.map(conn => conn.end()))
if(cb) if(cb)
cb() cb()
connectionPool = null connectionPool = null
@ -234,6 +245,18 @@ const single = (callback) => {
}); });
mysqlSingle._mysql = expand(mysqlSingle._mysql) mysqlSingle._mysql = expand(mysqlSingle._mysql)
// fix prevent query after closing
const end = mysqlSingle._mysql.end.bind(mysqlSingle._mysql)
mysqlSingle._mysql.end = (cb) => new Promise(resolve => {
mysqlSingle._mysql.__closed = true
end(() => {
resolve()
if(cb)
cb()
})
})
return proxySingle return proxySingle
} }

View File

@ -743,7 +743,7 @@ module.exports = function (send, recive, dataDirectory, version, env)
upnp.ratsUnmap() upnp.ratsUnmap()
logT('close', 'closing alternative db interface') logT('close', 'closing alternative db interface')
await new Promise(resolve => sphinxSingleAlternative.end(resolve)) await sphinxSingleAlternative.end()
// save torrents sessions // save torrents sessions
logT('close', 'save torrents downloads sessions') logT('close', 'save torrents downloads sessions')
@ -830,13 +830,14 @@ module.exports = function (send, recive, dataDirectory, version, env)
client.removeAllListeners('complete') client.removeAllListeners('complete')
logT('close', 'closing torrent client') logT('close', 'closing torrent client')
torrentClient.destroy(() => { torrentClient.destroy(() => spider.close(async () => {
sphinx.end(() => spider.close(() => { await sphinx.end()
sphinxSingle.destroy() logT('close', 'pool closed')
logT('close', 'spider closed') await sphinxSingle.end()
callback() logT('close', 'single closed')
})) logT('close', 'spider closed')
}) callback()
}))
} }
})() })()