//const sql = require("mssql/msnodesqlv8"); const sql = require("mssql"); const request = require("request-promise"); const fs = require("fs"); const winston = require("winston"); const logger = winston.createLogger({ level: 'info', format: winston.format.combine( winston.format.timestamp(), winston.format.printf(info => { return `${info.timestamp} ${info.level}: ${info.message}`; }) ), transports: [ new winston.transports.Console(), new winston.transports.File({ filename: './log/casRpsLogExtractorError.log', level: 'error' }), new winston.transports.File({ filename: './log/Combined.log' }) ] }); logger.log("info", "Starting casRpsLogExtractor v0.1 by j.tretter@gmail.com"); const config = JSON.parse(fs.readFileSync("casRpsLogExtractor.config.json", "utf8")); logger.log("debug", "Config" + JSON.stringify(config)) const processingType = "casrpslog"; let maxPKey = ""; let maxPKeyUnsent = ""; let lastOutMaxPKey = ""; let allRows = []; try { lastOutMaxPKey = maxPKey = fs.readFileSync("casRpsLogExtractor.maxPKey.dat", "utf8"); logger.log("debug", "MaxPKey Loaded: " + maxPKey); } catch (ex) { if (ex.errno === -4058) { logger.log("info", "MaxPKey-File not existing. Will be created."); } else { logger.log("error", "opening casRpsLogExtractor.maxPKey.dat :" + ex); } } sql.connect(config.sqlConfig, err => { if (err) { logger.log("error", "Connecting to DB: " + err); } const sqlRequest = new sql.Request(); sqlRequest.stream = true; setInterval(_ => { sqlRequest.query("select top 100 * from rpslog where pkey > '" + maxPKey + "' order by pkey"); }, config.pollInterval) sqlRequest.on('row', row => { outData = { sourceTag: config.sourceTag, processingType: processingType, data: row }; process.stdout.write("#"); request.json = true; 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); } }); sqlRequest.on('done', result => { 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("casRpsLogExtractor.maxPKey.dat", maxPKey); }).catch(err=>{ logger.log("error","Sending Request to Service: " + err) }); process.stdout.write(">" + allRows.length + ">"); allRows=[]; } process.stdout.write("."); }); })