fix(db): fix external sphinx openning check in some cases #57

This commit is contained in:
Alexey Kasyanchuk 2018-08-08 23:31:01 +03:00
parent 1e24bd67ef
commit 34cdc7fb09
3 changed files with 38 additions and 18 deletions

27
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "rats-search", "name": "rats-search",
"version": "0.27.0", "version": "0.28.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@ -10571,6 +10571,11 @@
"integrity": "sha1-EaBgVotnM5REAz0BJaYaINVk+zQ=", "integrity": "sha1-EaBgVotnM5REAz0BJaYaINVk+zQ=",
"dev": true "dev": true
}, },
"is-running": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/is-running/-/is-running-2.1.0.tgz",
"integrity": "sha1-MKc/9cw4VOT8JUkICen1q/jeCeA="
},
"is-scoped": { "is-scoped": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/is-scoped/-/is-scoped-1.0.0.tgz", "resolved": "https://registry.npmjs.org/is-scoped/-/is-scoped-1.0.0.tgz",
@ -10674,6 +10679,17 @@
"requires": { "requires": {
"node-fetch": "^1.0.1", "node-fetch": "^1.0.1",
"whatwg-fetch": ">=0.10.0" "whatwg-fetch": ">=0.10.0"
},
"dependencies": {
"node-fetch": {
"version": "1.7.3",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.3.tgz",
"integrity": "sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==",
"requires": {
"encoding": "^0.1.11",
"is-stream": "^1.0.1"
}
}
} }
}, },
"isstream": { "isstream": {
@ -12413,15 +12429,6 @@
"integrity": "sha1-VfuN62mQcHB/tn+RpGDwRIKUx30=", "integrity": "sha1-VfuN62mQcHB/tn+RpGDwRIKUx30=",
"dev": true "dev": true
}, },
"node-fetch": {
"version": "1.7.3",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.3.tgz",
"integrity": "sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==",
"requires": {
"encoding": "^0.1.11",
"is-stream": "^1.0.1"
}
},
"node-libs-browser": { "node-libs-browser": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/node-libs-browser/-/node-libs-browser-2.1.0.tgz", "resolved": "https://registry.npmjs.org/node-libs-browser/-/node-libs-browser-2.1.0.tgz",

View File

@ -123,6 +123,7 @@
"glob": "^7.1.2", "glob": "^7.1.2",
"iconv-lite": "^0.4.19", "iconv-lite": "^0.4.19",
"ipaddr.js": "^1.5.4", "ipaddr.js": "^1.5.4",
"is-running": "^2.1.0",
"json-socket": "^0.3.0", "json-socket": "^0.3.0",
"lodash": "^4.17.5", "lodash": "^4.17.5",
"material-ui": "^0.20.0", "material-ui": "^0.20.0",

View File

@ -10,6 +10,7 @@ const { spawn, exec } = require('child_process')
const appConfig = require('./config') const appConfig = require('./config')
const findFiles = require('./findFiles') const findFiles = require('./findFiles')
const _ = require('lodash') const _ = require('lodash')
const isRunning = require('is-running')
const writeSphinxConfig = (path, dbPath) => { const writeSphinxConfig = (path, dbPath) => {
let config = ` let config = `
@ -156,9 +157,11 @@ module.exports = (callback, dataDirectory, onClose) => {
appConfig['dbPath'] = sphinxConfigDirectory appConfig['dbPath'] = sphinxConfigDirectory
} }
// check external sphinx instance for using
const sphinxPid=`${sphinxConfigDirectory}/searchd.pid` const sphinxPid=`${sphinxConfigDirectory}/searchd.pid`
const isSphinxExternal=fs.existsSync(sphinxPid) const isSphinxExternal=fs.existsSync(sphinxPid) && isRunning(parseInt(fs.readFileSync(sphinxPid)))
logT('sphinx', "Pid: "+sphinxPid + (isSphinxExternal?" exists.":" no file.")); if(isSphinxExternal)
logT('sphinx', `founded running sphinx instance in ${sphinxPid}, using it`)
const { isInitDb } = isSphinxExternal ? {isInitDb: false} : writeSphinxConfig(sphinxConfigDirectory, appConfig.dbPath) const { isInitDb } = isSphinxExternal ? {isInitDb: false} : writeSphinxConfig(sphinxConfigDirectory, appConfig.dbPath)
@ -170,7 +173,7 @@ module.exports = (callback, dataDirectory, onClose) => {
} }
const sphinx = !isSphinxExternal ? spawn(sphinxPath, options) : const sphinx = !isSphinxExternal ? spawn(sphinxPath, options) :
{isExternal: true, on: (d,f) => {}, stdout: {on : (d,f)=>{} }}; {isExternal: true, on: (d,f) => {}, stdout: {on : (d,f)=>{} }}; // running stub
// remeber initizalizing of db // remeber initizalizing of db
sphinx.start = start sphinx.start = start
@ -180,8 +183,6 @@ module.exports = (callback, dataDirectory, onClose) => {
const optimizeResolvers = {} const optimizeResolvers = {}
if (isSphinxExternal && callback) setTimeout(()=>{logT('sphinx', 'external sphinx signalled');callback()},500);
sphinx.stdout.on('data', (data) => { sphinx.stdout.on('data', (data) => {
logT('sphinx', `sphinx: ${data}`) logT('sphinx', `sphinx: ${data}`)
@ -211,12 +212,16 @@ module.exports = (callback, dataDirectory, onClose) => {
} }
}) })
sphinx.on('close', (code, signal) => { const close = () => {
logT('sphinx', `sphinx closed with code ${code} and signal ${signal}`)
if(onClose && !sphinx.replaceOnClose) // sometime we don't want to call default callback if(onClose && !sphinx.replaceOnClose) // sometime we don't want to call default callback
onClose() onClose()
if(sphinx.onClose) if(sphinx.onClose)
sphinx.onClose() sphinx.onClose()
}
sphinx.on('close', (code, signal) => {
logT('sphinx', `sphinx closed with code ${code} and signal ${signal}`)
close()
}) })
sphinx.stop = (onFinish, replaceFinish) => { sphinx.stop = (onFinish, replaceFinish) => {
@ -225,8 +230,14 @@ module.exports = (callback, dataDirectory, onClose) => {
sphinx.onClose = onFinish sphinx.onClose = onFinish
if(replaceFinish) if(replaceFinish)
sphinx.replaceOnClose = true // sometime we don't want to call default callback sphinx.replaceOnClose = true // sometime we don't want to call default callback
if (!sphinx.isExternal) if (!sphinx.isExternal)
exec(`"${sphinxPath}" --config "${config}" --stopwait`) exec(`"${sphinxPath}" --config "${config}" --stopwait`)
else
{
logT('sphinx', `ignoring sphinx closing because external sphinx instance`)
close()
}
} }
sphinx.waitOptimized = (table) => new Promise((resolve) => { sphinx.waitOptimized = (table) => new Promise((resolve) => {
@ -280,8 +291,9 @@ module.exports = (callback, dataDirectory, onClose) => {
_.merge(sphinx, sphinx.start(callback)); _.merge(sphinx, sphinx.start(callback));
} }
return sphinx if (isSphinxExternal && callback) setTimeout(()=>{logT('sphinx', 'external sphinx signalled');callback()}, 0);
return sphinx
} }
return start(callback) return start(callback)