|
| 1 | +const logger = require('./common/logger') |
| 2 | +const config = require('config') |
| 3 | +const pg = require('pg') |
| 4 | +const pgOptions = config.get('POSTGRES') |
| 5 | +const pgConnectionString = `postgresql://${pgOptions.user}:${pgOptions.password}@${pgOptions.host}:${pgOptions.port}/${pgOptions.database}` |
| 6 | +const pgClient = new pg.Client(pgConnectionString) |
| 7 | +const kafkaService = require('./services/pushToDirectKafka') |
| 8 | +const postMessage = require('./services/posttoslack') |
| 9 | +const pushToDynamoDb = require('./services/pushToDynamoDb') |
| 10 | +const auditTrail = require('./services/auditTrail'); |
| 11 | + |
| 12 | +const AWS = require("aws-sdk"); |
| 13 | + |
| 14 | +const documentClient = new AWS.DynamoDB.DocumentClient({ |
| 15 | + region: 'us-east-1', |
| 16 | + convertEmptyValues: true |
| 17 | +}); |
| 18 | + |
| 19 | +async function setupPgClient() { |
| 20 | + var payloadcopy |
| 21 | + try { |
| 22 | + const pgClient = new pg.Client(pgConnectionString) |
| 23 | + if (!pgClient.connect()) { |
| 24 | + await pgClient.connect() |
| 25 | + } |
| 26 | + rec_d_start = config.RECONSILER.RECONSILER_START |
| 27 | + rec_d_end = config.RECONSILER.RECONSILER_END |
| 28 | + rec_d_type = config.RECONSILER.RECONSILER_DURATION_TYPE |
| 29 | + var paramvalues = [rec_d_start,rec_d_end]; |
| 30 | + sql1 = "select a.payloadseqid from common_oltp.producer_audit_dd a where not exists (select from common_oltp.pgifx_sync_audit " |
| 31 | + sql2 = " where pgifx_sync_audit.payloadseqid = a.payloadseqid) and a.auditdatetime " |
| 32 | + sql3 = " between (timezone('utc',now())) - interval '1"+ rec_d_type + "' * ($1)" |
| 33 | + sql4 = " and (timezone('utc',now())) - interval '1"+ rec_d_type + "' * ($2)" |
| 34 | + |
| 35 | + sql = sql1 + sql2 + sql3 + sql4 |
| 36 | + logger.info(`${sql}`) |
| 37 | + const res = await pgClient.query(sql,paramvalues, async (err,result) => { |
| 38 | + if (err) { |
| 39 | + var errmsg0 = `error-sync: Audit Reconsiler1 query "${err.message}"` |
| 40 | + logger.debug (errmsg0) |
| 41 | + await callposttoslack(errmsg0) |
| 42 | + } |
| 43 | + else{ |
| 44 | + console.log("Reconsiler_dd_2 : Rowcount = ", result.rows.length) |
| 45 | + if (result.rows.length > 0) |
| 46 | + { |
| 47 | + Promise.all(result.rows.map(async (row) => { |
| 48 | + console.log(row.payloadseqid) |
| 49 | + const x = await calldynamodb(row.payloadseqid) |
| 50 | + console.log("val of x ",x) |
| 51 | + } |
| 52 | + //)) |
| 53 | + )).then(result => { if (typeof result.rows == "undefined") |
| 54 | + { |
| 55 | + console.log("terminating after posting to kafka") |
| 56 | + pgClient.end() |
| 57 | + terminate() |
| 58 | + } |
| 59 | + }) |
| 60 | + } |
| 61 | + else { |
| 62 | + console.log("terminate due to 0 rows") |
| 63 | + pgClient.end() |
| 64 | + terminate() |
| 65 | + } |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | + catch(err) |
| 70 | + { |
| 71 | + console.log(err) |
| 72 | + process.exit(1) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +async function calldynamodb(payloadseqid) |
| 78 | +{ |
| 79 | + console.log("At dynamoc function") |
| 80 | + var params = { |
| 81 | + TableName : config.DYNAMODB.DYNAMODB_TABLE, |
| 82 | + KeyConditionExpression: 'payloadseqid = :hkey', |
| 83 | + ExpressionAttributeValues: { |
| 84 | + ':hkey': payloadseqid |
| 85 | + }} |
| 86 | + |
| 87 | + return new Promise(async function (resolve, reject) { |
| 88 | + await documentClient.query(params, async function(err, data) { |
| 89 | + if (err) console.log(err); |
| 90 | + else { |
| 91 | + var s_payload = (data.Items[0].pl_document) |
| 92 | + // console.log("s_payload",s_payload) |
| 93 | + logger.info(`Reconsiler2 Before Posting Payload : ${JSON.stringify(s_payload)}`) |
| 94 | + await kafkaService.pushToKafka(s_payload) |
| 95 | + await audit(s_payload, 1) |
| 96 | + logger.info(`Reconsiler2 Payload posted`) |
| 97 | + }; |
| 98 | + resolve("done"); |
| 99 | + }); |
| 100 | +}) |
| 101 | +} |
| 102 | + |
| 103 | +async function audit(message, reconsileflag) { |
| 104 | + var pl_producererr = "Reconsiler2" |
| 105 | + const pl_processid = 5555 |
| 106 | + payload1 = (message.payload) |
| 107 | + const pl_seqid = payload1.payloadseqid |
| 108 | + const pl_topic = payload1.topic // TODO can move in config ? |
| 109 | + const pl_table = payload1.table |
| 110 | + const pl_uniquecolumn = payload1.Uniquecolumn |
| 111 | + const pl_operation = payload1.operation |
| 112 | + const pl_timestamp = payload1.timestamp |
| 113 | + //const pl_payload = JSON.stringify(message) |
| 114 | + const pl_payload = message |
| 115 | + const logMessage = `${pl_seqid} ${pl_processid} ${pl_table} ${pl_uniquecolumn} ${pl_operation}` |
| 116 | + logger.debug(`${pl_producererr} : ${logMessage}`); |
| 117 | + await auditTrail([pl_seqid, pl_processid, pl_table, pl_uniquecolumn, pl_operation, "push-to-kafka", "", "", "Reconsiler2", pl_payload, new Date(), ""], 'producer') |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + async function callposttoslack(slackmessage) { |
| 122 | + if (config.SLACK.SLACKNOTIFY === 'true') { |
| 123 | + return new Promise(function (resolve, reject) { |
| 124 | + postMessage(slackmessage, (response) => { |
| 125 | + console.log(`respnse : ${response}`) |
| 126 | + if (response.statusCode < 400) { |
| 127 | + logger.debug('Message posted successfully'); |
| 128 | + //callback(null); |
| 129 | + } else if (response.statusCode < 500) { |
| 130 | + const errmsg1 = `Slack Error: Reconsiler1: posting message to Slack API: ${response.statusCode} - ${response.statusMessage}` |
| 131 | + logger.debug(`error-sync: ${errmsg1}`) |
| 132 | + } else { |
| 133 | + logger.debug(`Reconsiler1: Server error when processing message: ${response.statusCode} - ${response.statusMessage}`); |
| 134 | + //callback(`Server error when processing message: ${response.statusCode} - ${response.statusMessage}`); |
| 135 | + } |
| 136 | + resolve("done") |
| 137 | + }); |
| 138 | + }) //end |
| 139 | + } |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + //=================BEGIN HERE ======================= |
| 144 | +const terminate = () => process.exit() |
| 145 | + |
| 146 | +async function run() { |
| 147 | + logger.debug("Initialising Reconsiler2 setup...") |
| 148 | + kafkaService.init().catch((e) => { |
| 149 | + logger.error(`Kafka producer intialization error: "${e}"`) |
| 150 | + terminate() |
| 151 | + }) |
| 152 | + setupPgClient() |
| 153 | +} |
| 154 | + |
| 155 | +run() |
0 commit comments