i have some web addresses in my db , i need to check these addresses for something and updated them in the db accordingly ... i need to do this over and over
here is simplified version of my code , i have two functions
1 - init
-> reads an address row from db , pass the address to second function to check and update the result in db for that address
2 - check_address
-> checking that address in the browser and returning the result
here is simplified version of my code
async function init()
{
try
{
let address = 'select * from addressess order by updated_at asc limit 1';
let result = await check_address(address);
await address.update({result : result });
}
catch(e){}
setTimeout( ()=> init() , 5000 );
}
async function check_address( address )
{
var browser = await puppeteer.launch();
let result = await 'open a tab , go to the address and cehck';
// close the browser
return result ;
}
init();
i feel like opening and closing browser for each address is very time/resource consuming , so i thought maybe i could have a global browser open at all time and just open and close tabs in the browser or maybe even use the same tab ... something like
let globalBrowser = false ;
async function check_address( address )
{
if(!globalBrowser)
{
globalBrowser = await puppeteer.launch();
globalBrowser._process.once('close', () => {
globalBrowser = false;
});
}
let result = await 'open a tab , go to the address and cehck';
return result ;
}
but im not sure if having a browser open at all time is ok , or this flow generally is a good idea
i have very limited experience with puppeteer , i would love some input from someone with more experience and knowledge of long running puppeteer app
CodePudding user response:
Indeed, opening and closing the browser is performanced heavy. If your script is running on a server you could open the browser initially and do a cron job (@package node-cron) every X hours on the check_adress
function.
Instead of closing the browser you could simply close the current page page via:
await page.close();