const {Cluster} = require('puppeteer-cluster'); const log4js = require('log4js'); const {cpus} = require('os'); module.exports = { init() { if (this.pool) return; Cluster.launch({ concurrency: Cluster.CONCURRENCY_PAGE, maxConcurrency: cpus().length, puppeteerOptions: { headless: 'new', args: [ '--no-sandbox', '--disable-setuid-sandbox', '--no-zygote' ] } }).then(cluster => { this.pool = cluster; log4js.configure({ appenders: { out: {type: 'stdout'} }, categories: {default: {appenders: ['out'], level: 'info'}}, }); const logger = log4js.getLogger(); cluster.on('taskerror', (err, data, willRetry) => { if (willRetry) logger.warn(`[Print] => ${err.message}\nThis job will be retried`); else logger.error(`[Print] => ${err.message}`); }); cluster.on('queue', () => process.env.SPEC_IS_RUNNING === 'false' && logger.info('Printing task initialized by pool')); }); } };