Nodejs Duplicate data when using Request Api

Asked

Viewed 97 times

0

hello,

I’m new to nodejs! I’m developing a code that reads a table in postgresql and acts with the record runs some GET API. For example, returns "int" executes "http://api1/param", if returns "Fab" executes "http://api2/param". Then write the return of Apis to another table.

But I’m having the following problem, running the API 2 or more times and writing to the bank. What I don’t understand is that it doesn’t always happen this way.

Someone could help me?

Code:

const express = require('express'),
      http = require('http'),
      app = express(),
      server = http.createServer(app),
      schedule = require('node-schedule'),
      dateFormat = require('dateformat'),
      pg = require('pg'),
      request = require('request');

const configPg150 = {
    user: 'xxxxx',
    database: 'xxxxxx',
    password: 'xxxxxx',
    port: xxxxx
};

var pools = new pg.Pool(configPg150)

const { Pool } = require('pg');
const pool = new Pool(configPg150);

const tbNome = "tb1";
const tbNomeN = "tb2";

(async () => {
    const client = await pool.connect()
    try {
        let startTime = new Date(Date.now() + 1000);
        var j = schedule.scheduleJob({ start: startTime, end: null, rule: '*/3 * * * * *' }, function(){
            seleciona(client);
        });
    } finally {
        //client.release()
    }
    await pool.end();
})().catch(e => console.log(e.stack))

async function seleciona(client) {

    agora = new Date(Date.now());
    dt_envioA = dateFormat(agora, "HH:MM:ss");
    console.log("Seleciona - "+dt_envioA);

    const res = await client.query('SELECT * FROM "'+tbNomeN+'" WHERE bloq = 0 ORDER BY id ASC LIMIT 10')
    if (res.rows != undefined)  {
        for(var i = 0; i < res.rows.length; i++) {
            var myJSONObject = {
                'Id' : res.rows[i].id,
                'MessageTo' : "55"+res.rows[i].ToNo,
                'MessageTo55' : res.rows[i].ToNo,
                'MessageText' : res.rows[i].Message,
                'id_sms' : res.rows[i].id_sms,
                'id_usu' : res.rows[i].id_usu,
                'envio_por': res.rows[i].envio_por
            };
            await verificaEnvio(myJSONObject, client);
        }
    }
}

async function verificaEnvio(arrDados, client) {
    let text = 'SELECT * FROM '+tbNome+' WHERE id_sms = $1 AND numero = $2 ORDER BY id ASC LIMIT 1';
    let values = [arrDados['id_sms'], arrDados['MessageTo55']];
    const resI = client.query(text, values, (err, result) => {
        if (err)
            return console.error('Error executing query', err.stack)

        agora = new Date(Date.now());
        dt_envioA = dateFormat(agora, "HH:MM:ss");
        console.log(dt_envioA + ' ---------- '+arrDados['id_sms']+' - '+arrDados['MessageTo55']+' - '+result.rowCount);

        if (result.rowCount == 0) {
            if (arrDados['envio_por'] == "hot") {
                send3(arrDados, client); 
            } else if (arrDados['envio_por'] == "fab") {
                send2(arrDados, client);
            } else if (arrDados['envio_por'] == "int") {
                send1(arrDados, client);
            } else {
                send1(arrDados, client);
            }
        } else {
            delData(arrDados, client); 
        }

    });
}

async function delData(arrDados, client) {
    agora = new Date(Date.now());
    dt_envioA = dateFormat(agora, "HH:MM:ss");    
    console.log(dt_envioA + ' - DDD DEL: '+arrDados['id_sms']+' - '+arrDados['MessageTo55']);

    agora = new Date(Date.now());
    dt_envio = dateFormat(agora, "yyyy-mm-dd HH:MM:ss");

    const textD = 'DELETE FROM "'+tbNomeN+'" WHERE "id" = '+arrDados["Id"];
    const valuesD = [];
    const resD = await client.query(textD, valuesD);
}

async function send1(arrDados, client) {

    agora = new Date(Date.now());
    dt_envioA = dateFormat(agora, "HH:MM:ss");
    console.log(dt_envioA + ' - SSS ONE: '+arrDados['id_sms']+' - '+arrDados['MessageTo55']);

    await request({
        url: "http://api.xxxxxxx1.com.br/api/send/simple&token=xxxxxxxx&to="+arrDados['MessageTo']+"&msg="+arrDados["MessageText"],
        method: "GET",
        json: true
    }, function(err,httpResponse,body){
        if (err) { return console.log(err); }
        agora = new Date(Date.now());
        dt_envio = dateFormat(agora, "yyyy-mm-dd HH:MM:ss");

        const text = 'INSERT INTO "'+tbNome+'" ("ToNo", "Message", "retorno", "dt_envio", "id_sms", "enviado_por", "retorno_txt", "id_usu", "numero") VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)';
        const values = [arrDados['MessageTo55'], arrDados['MessageText'], body, dt_envio, arrDados['id_sms'], 'ccccc', 'ccccc', arrDados['id_usu'], arrDados['MessageTo55']];
        const resI = client.query(text, values);

        const textD = 'DELETE FROM "'+tbNomeN+'" WHERE "id" = '+arrDados["Id"];
        const valuesD = [];
        const resD = client.query(textD, valuesD);
    });
}

async function send2(arrDados, client) {

    agora = new Date(Date.now());
    dt_envioA = dateFormat(agora, "HH:MM:ss");
    console.log(dt_envioA + ' - MMM FAB: '+arrDados['id_sms']+' - '+arrDados['MessageTo55']);

    await request({
        url: "https://api.xxxxxxxx2.com.br/send.php?username=xxxxxx&password=xxxxxxxx&to="+arrDados['MessageTo']+"&content="+arrDados["MessageText"],
        method: "GET",
        json: true
    }, function(err,httpResponse,body){
        if (err) { return console.log(err); }
        agora = new Date(Date.now());
        dt_envio = dateFormat(agora, "yyyy-mm-dd HH:MM:ss");

        const text = 'INSERT INTO "'+tbNome+'" ("ToNo", "Message", "retorno", "dt_envio", "id_sms", "enviado_por", "retorno_txt", "id_usu", "numero") VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)';
        const values = [arrDados['MessageTo55'], arrDados['MessageText'], body, dt_envio, arrDados['id_sms'], 'cccc', 'cccc', arrDados['id_usu'], arrDados['MessageTo55']];
        const resI = client.query(text, values);

        const textD = 'DELETE FROM "'+tbNomeN+'" WHERE "id" = '+arrDados["Id"];
        const valuesD = [];
        const resD = client.query(textD, valuesD);
    });
}

async function send3(arrDados, client) {

    agora = new Date(Date.now());
    dt_envioA = dateFormat(agora, "HH:MM:ss");
    console.log(dt_envioA + ' - XXX HOT: '+arrDados['id_sms']+' - '+arrDados['MessageTo55']);

    var resposta = "";
    await request({
        url: "http://painel.xxxxxxxx3.com.br/SendAPI/Send.aspx?usr=xxxxxxxxx&pwd=xxxxxxxx&number="+arrDados['MessageTo']+"&msg="+arrDados["MessageText"],
        method: "GET",
        json: true
    }, function(err,httpResponse,body){
        if (err) { return console.log(err); }

        agora = new Date(Date.now());
        dt_envio = dateFormat(agora, "yyyy-mm-dd HH:MM:ss");

        const text = 'INSERT INTO "'+tbNome+'" ("ToNo", "Message", "retorno", "dt_envio", "id_sms", "enviado_por", "retorno_txt", "id_usu", "numero") VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)';
        const values = [arrDados['MessageTo55'], arrDados['MessageText'], body, dt_envio, arrDados['id_sms'], 'ccccc', 'ccccc', arrDados['id_usu'], arrDados['MessageTo55']];
        const resI = client.query(text, values);

        const textD = 'DELETE FROM "'+tbNomeN+'" WHERE "id" = '+arrDados["Id"];
        const valuesD = [];
        const resD = client.query(textD, valuesD);
    });
}

1 answer

0

Friend, I think your problem is in your crontab. In your code you make a schedule (Schedule) to run the select function(client).

Let startTime = new date(Date.now() + 1000); var j = Schedule.scheduleJob({ start: startTime, end: null, Rule: '*/3 * * * * *' },
selects(client);

In this code you are asking cron to run the function select(client) every 3 minutes (Rule: '*/3 * * * *') after 1 second (start: startTime) and never stop (end: null).
Notice the problem? After you receive the first request in the API your program will be running select(client) every 3 minutes.
To solve your problem you should see if you really need to do this scheduling, if you need then put a stop condition.

  • Thank you. But one question. I need to monitor the database to check if you have a new record. And process this record according to the code. There’s another way to do it?

  • Is there another application outside the API that writes the data in that database? If you don’t have it, you don’t need to do this monitoring, you can use the callback whenever you do an insert in the bank. Like this, in the function that makes the Insert puts a callback, so when you finish the Insert it will return to that callback and in it you put what should be done with the new record. There’s a lot of information about callback out there, very easy to find.

  • Now, if this verification is really necessary, I would create a Trigger in the main table (Trigger onInesrt), so after the Insert in this table it records the new ID in a secondary table, (table Temregistronovo for example). So in your code you can create a Schedule to keep monitoring this table Temregistronovo, if you have something in it he knows that it was inserted given new in the main table, and with the ID he can search, after you have done what you need to delete the contents of the table Temregistronovo, so you know that the only record there is always new information.

  • Thank you very much!! It helped a lot!!!

  • If helped mark the answer as correct to help other people tbm!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.