From bce3458160e3151d797fc8c826fb1832cd78f1bb Mon Sep 17 00:00:00 2001 From: Joe Date: Thu, 31 Jan 2019 08:32:12 -0600 Subject: [PATCH] error handling in dbExtractor --- DBExtractor/DBExtractor.js | 88 ++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/DBExtractor/DBExtractor.js b/DBExtractor/DBExtractor.js index b83d1eb..4fbe8e9 100644 --- a/DBExtractor/DBExtractor.js +++ b/DBExtractor/DBExtractor.js @@ -58,51 +58,55 @@ try { } } -sql.connect(config.sqlConfig, err => { - if (err) { - logger.log("error", "Connecting to DB: " + err); - } - const sqlRequest = new sql.Request(); - sqlRequest.stream = true; +try { + sql.connect(config.sqlConfig, err => { + if (err) { + logger.log("error", "Connecting to DB: " + err); + } + const sqlRequest = new sql.Request(); + sqlRequest.stream = true; - function runNextPoll(){ - sqlRequest.query("select top " + maxResultsPerMessage + " * from rpslog where pkey > '" + maxPKey + "' order by pkey"); - } - - runNextPoll(); + function runNextPoll(){ + sqlRequest.query("select top " + maxResultsPerMessage + " * from rpslog where pkey > '" + maxPKey + "' order by pkey"); + } + + runNextPoll(); - sqlRequest.on('row', row => { - outData = { sourceTag: config.sourceTag, processingType: config.processingType, data: row }; - process.stdout.write("#"); - request.json = true; + sqlRequest.on('row', row => { + outData = { sourceTag: config.sourceTag, processingType: config.processingType, data: row }; + process.stdout.write("#"); + request.json = true; - logger.log('debug', "adding row to array:" + JSON.stringify(outData)); - allRows.push(outData); + logger.log('debug', "adding row to array:" + JSON.stringify(outData)); + allRows.push(outData); - if (row.PKey > maxPKeyUnsent) { - maxPKeyUnsent = row.PKey; - logger.log("debug", "MaxPKeyUnsent Written: " + maxPKeyUnsent); - } - }); + if (row.PKey > maxPKeyUnsent) { + maxPKeyUnsent = row.PKey; + logger.log("debug", "MaxPKeyUnsent Written: " + maxPKeyUnsent); + } + }); - sqlRequest.on('done', result => { - clearInterval(theInterval); - if (allRows.length>0){ - request({ method: 'POST', uri: config.serverURI, body: JSON.stringify(allRows), json: true }).then(_ => { - // count up the maxPKey only when the request was successful... - maxPKey=maxPKeyUnsent; - fs.writeFileSync("DBExtractor.maxPKey.dat", maxPKey); - }).catch(err=>{ - logger.log("error","Sending Request to Service: " + err) - }).finally(_=>{ - runNextPoll(); - }); - process.stdout.write(">" + allRows.length + ">"); - } else { - theInterval = setInterval(runNextPoll, config.pollIntervalIdle); - process.stdout.write("."); - } - allRows=[]; + sqlRequest.on('done', result => { + clearInterval(theInterval); + if (allRows.length>0){ + request({ method: 'POST', uri: config.serverURI, body: JSON.stringify(allRows), json: true }).then(_ => { + // count up the maxPKey only when the request was successful... + maxPKey=maxPKeyUnsent; + fs.writeFileSync("DBExtractor.maxPKey.dat", maxPKey); + }).catch(err=>{ + logger.log("error","Sending Request to Service: " + err) + }).finally(_=>{ + runNextPoll(); + }); + process.stdout.write(">" + allRows.length + ">"); + } else { + theInterval = setInterval(runNextPoll, config.pollIntervalIdle); + process.stdout.write("."); + } + allRows=[]; - }); -}) + }); + }) +} catch (ex) { + logger.log("error", `Fatal error occured: ${ex}`); +} \ No newline at end of file