coopernurse / node-pool

Generic resource pooling for node.js
2.38k stars 259 forks source link

Not Working on Docker to run Puppeteer #266

Closed pavansai1122 closed 5 years ago

pavansai1122 commented 5 years ago

I'm using this package to run Puppeteer by default and its working good on my local machine. But after deployed on server it was not working and not throwing any error. Here is My code `import puppeteer from 'puppeteer'; const genericPool = require('generic-pool');

class BrowserPool { constructor() { const factory = { create: async function () { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.setViewport({ width: 1200, height: 400 }); return page; }, destroy: function (browserInstance) { browserInstance.close(); }, }; this.browserPagePool = genericPool.createPool(factory, { max: 3, min: 1, maxWaitingClients: 50, }); this.browserPagePool.on("factoryCreateError", err => { console.log(err); }); this.browserPagePool.on("factoryDestroyError", err => { console.log(err); }); // console.log(this.browserPagePool) } async openPage(html, format, quality = 100) { try { const page = await this.browserPagePool.acquire(); await page.goBack(); await page.goto(data:text/html,${html}, { waitUntil: ['networkidle0', 'domcontentloaded'] }); let replyData; if (format == 'image') { replyData = await page.screenshot({ type: 'jpeg', quality: quality, printBackground: true, encoding: "base64", waitUntil: 'networkidle0' }); } else if (format == 'pdf') { replyData = await page.pdf({ printBackground: true, format: 'A3' }); } await this.browserPagePool.release(page); return replyData; } catch (err) { console.log('error in browser pooling class', err); return ''; } }; } export default new BrowserPool();`

LeMoussel commented 5 years ago

There is puppeteer-cluster: Puppeteer Pool, run a cluster of instances in parallel .

This library spawns a pool of Chromium instances via Puppeteer and helps to keep track of jobs and errors. This is helpful if you want to crawl multiple pages or run tests in parallel.

pavansai1122 commented 5 years ago

thank you i tried above puppeteer-cluster worked for me. And I'm solved earlier problem using npm "Generic pool" by placing some puppeteer arguments const factory = { create: async function () { const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] }); const page = await browser.newPage(); await page.setViewport({ width: 1200, height: 400 }); Thank you @LeMoussel