const {Cluster} = require('puppeteer-cluster'); const log4js = require('log4js'); const {cpus} = require('os'); module.exports = { init() { if (!this.pool) { Cluster.launch({ concurrency: Cluster.CONCURRENCY_CONTEXT, maxConcurrency: cpus().length, puppeteerOptions: { headless: true, 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', () => logger.info('Printing task initialized by pool')); }); } } };