Skip to content
Snippets Groups Projects
Commit c04897ea authored by MoSelim's avatar MoSelim
Browse files

#652

handle xml parsing on write stream finish
parent 21baf02e
No related branches found
No related tags found
1 merge request!169Dev
......@@ -12,6 +12,7 @@ const {
} = require('../xpub-model/entities/manuscript/helpers/transform')
const NCBI_RESPONSE_EXT = new RegExp(/\S+.ld.response.xml$/i)
let c = new Client()
;(async () => {
const beforeUpdate = Date.now()
......@@ -23,11 +24,11 @@ function close(c) {
c.end()
ManuscriptAccess.knex().destroy()
c = null
logger.info('connection terminated')
process.exit()
}
async function fromNcbi() {
let c = new Client()
c.on('ready', () => {
c.list(
`/${config.get('ncbi-ftp')['receive-folder']}`,
......@@ -50,16 +51,22 @@ async function fromNcbi() {
c.get(remoteFilePath, (err, stream) => {
if (err) return
const path = `${os.tmpdir()}/${file.name}`
stream.pipe(fs.createWriteStream(path))
processFile(path).then(response => {
updateManuscriptNcbiStatus(file.name, response).then(() => {
c.rename(remoteFilePath, `${remoteFilePath}.processed`, err => {
if (err) {
logger.info(err)
}
if (index === array.length - 1) {
close(c)
}
const writeStream = stream.pipe(fs.createWriteStream(path))
writeStream.on('finish', () => {
processFile(path).then(response => {
updateManuscriptNcbiStatus(file.name, response).then(() => {
c.rename(
remoteFilePath,
`${remoteFilePath}.processed`,
err => {
if (err) {
logger.info(err)
}
if (index === array.length - 1) {
close(c)
}
},
)
})
})
})
......@@ -72,6 +79,17 @@ async function fromNcbi() {
c.connect({ host, user, password })
}
process
.on('uncaughtException', err => {
logger.error('Uncaught Exception thrown: ', err)
close(c)
})
.on('unhandledRejection', (reason, promise) => {
logger.error('Unhandled Rejection at Promise: ', promise)
logger.error('Reason: ', reason)
close(c)
})
function processFile(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (err, data) => {
......@@ -96,6 +114,9 @@ async function updateManuscriptNcbiStatus(fileName, response) {
// filename example: ems76395.2019_01_01-08_30_06.ld.response.xml
const manuscriptId = fileName.split('.')[0].toUpperCase()
const manuscript = await ManuscriptAccess.selectById(manuscriptId)
if (!manuscript) {
throw new Error('manuscript not found in db')
}
const newIds = manuscript['meta,articleIds'].filter(
id => id.pubIdType !== 'pmcid',
)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment