-3

I have this function to fetch data from another server(it basically send a command to run a python program, which writes an output into a text file on the remote machine and then it returns the content of the txt file)

const getServerStatus = () => { // returns result of of screen -ls
    const commandQuery = "python3 /home/user/scripts/getServerStatus.py && cat /home/user/scripts/status.txt";
        
        return fetch('http://10.0.0.253:3005/fetch', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                command: commandQuery
            })
        })
        .then(res => {
            return res.json();
        })
        .then(data => {
            return data.status;
        })
        .catch(err => { // it catches when fatch fails (server is off)
            return 'OFF'; 
        });
};

And the function gets executed when the user visits home page

app.get('/', checkAuthenticated, async (req, res) => {
    const status = await getServerStatus()
    .then(data => {
        return data;
    });

    res.render('index.ejs', { 
        name: req.user.username,
        title: 'home',
        server_status: status
    });
})

It runs no problem and it returns data and the home page gets rendered very quickly, but the problem is that if I don't refresh the webpage for a curtain amount of time (maybe like 2-3 minutes or so), it'll take around 8-10 seconds to fetch and render the homepage again. I just can't figure out what the problem is. I think there is something wrong with my async functions but I am not sure. Or maybe my node server goes like an idle mode when there's no oncomming connections?

my /fetch endpoint

app.post('/fetch', (req, res) => {
    exec(req.body.command,
    function (error, stdout, stderr) {
        res.send(JSON.stringify({status :stdout.replace(/\s/g, '')}));
        console.log('[+] SERVER STATUS SENT');
        if (error !== null) {
             console.log('exec error: ' + error);
        }
    }); 
});
Slbox
  • 10,957
  • 15
  • 54
  • 106
osgo1115
  • 11
  • 1
  • 1
    You shouldn't need the `.then(data => { return data; })` since that's the same as `.then(data => data)` which is the same as the return value of `await`. But you never use the `status` variable after you went through the trouble of getting it, which seems odd. – Heretic Monkey Jan 18 '22 at 21:28
  • 1
    I can't see anything wrong with your code that would affect performance. My guess is the culprit is with `http://10.0.0.253:3005/fetch` just being slow. Try [measuring](https://stackoverflow.com/a/44158747/1048572) how long the request takes. Also try [measuring time in the python script itself](https://stackoverflow.com/a/7370824/1048572) and compare. I bet that somewhere (on either of your two servers, or in whatever the script is doing) some virtualisation is involved and it takes some time to spin up a new instance. – Bergi Jan 18 '22 at 21:33
  • Thanks for your comment! I changed the line you pointed out to ```.then(data => data);``` but it didn't fix it. And I am passing the status variable in ```res.render``` – osgo1115 Jan 18 '22 at 21:35
  • Thanks Bergi! I didn't see any issues with the /fetch endpoint on the server but could you take a look? I have added the endpoint in my post – osgo1115 Jan 18 '22 at 21:39
  • 1
    Apart from the huge security issues, no, there's no obvious problem with that code either. As I suggested, start by timing all the involved functions, on both servers, to find out where the time is actually spent. Only then we can help you finding out *why* the time is spent there. – Bergi Jan 18 '22 at 21:59
  • I am just curious but what are the security issues? – osgo1115 Jan 18 '22 at 22:16
  • uh, `exec(req.body.command)` without any authentication?! – Bergi Jan 18 '22 at 23:05
  • yeah I just noticed that and fixed! I'm a student working on a project and I'm pretty new to node.js so I missed that part! thanks for pointing out though – osgo1115 Jan 18 '22 at 23:40

1 Answers1

0

Turned out that it is a hardware issue? of my raspberry pi 4. If I run the node project on a different raspberry pi, it doesn't do that and runs perfectly without issues.

osgo1115
  • 11
  • 1