Skip to content

Commit

Permalink
fix(fixed probing and made it more optimal): probing fix
Browse files Browse the repository at this point in the history
  • Loading branch information
acuciureanu committed Jan 12, 2024
1 parent f1a9bda commit bf88a03
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions services/check.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { PromisePool } from '@supercharge/promise-pool';

const payload = load('../sandbox/js/check.payload.js');

const probe = async (pageUrl) => {
const page = await browser.newPage();

const probe = async (pageUrl, page) => {
await page.goto(pageUrl, { waitUntil: 'networkidle0' });

await page.evaluate((payload) => {
Expand All @@ -23,25 +21,30 @@ const probe = async (pageUrl) => {
};

const probeAll = async (urls, concurrency = 10) => {
const browser = await puppeteer.launch({ headless: "new" });
try {
const { results } = await PromisePool.for(urls)
.withConcurrency(concurrency)
.onTaskFinished((url, pool) => console.log(`[${pool.processedPercentage().toFixed(2)}%] Processed ${url} ...`))
.process(probe);
const browser = await puppeteer.launch({ headless: 'new' });
const pages = await browser.pages();

log(results);
return results;
try {
const { results } = await PromisePool.for(urls)
.withConcurrency(concurrency)
.onTaskFinished((url, pool) => console.log(`[${pool.processedPercentage().toFixed(2)}%] Processed ${url} ...`))
.process(async (url) => {
const page = pages.length > 0 ? pages.shift() : await browser.newPage();
const result = await probe(url, page);
pages.push(page);
return result;
});

log(results);
return results;
} finally {
const pages = await browser.pages();
for (const page of pages) {
if (!page.isClosed()) {
await page.close();
}
}
await browser.close();
}
browser.close();
};

export default { probeAll };

0 comments on commit bf88a03

Please sign in to comment.