2022-10-19 12:30:36 +00:00
|
|
|
const {Cluster} = require('puppeteer-cluster');
|
|
|
|
const log4js = require('log4js');
|
|
|
|
const {cpus} = require('os');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init() {
|
2023-04-04 13:00:28 +00:00
|
|
|
if (this.pool) return;
|
|
|
|
Cluster.launch({
|
|
|
|
concurrency: Cluster.CONCURRENCY_CONTEXT,
|
|
|
|
maxConcurrency: cpus().length,
|
|
|
|
puppeteerOptions: {
|
2023-05-23 11:31:09 +00:00
|
|
|
headless: 'new',
|
2023-04-04 13:00:28 +00:00
|
|
|
args: [
|
|
|
|
'--no-sandbox',
|
|
|
|
'--disable-setuid-sandbox',
|
|
|
|
'--no-zygote'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}).then(cluster => {
|
|
|
|
this.pool = cluster;
|
2022-10-19 12:30:36 +00:00
|
|
|
|
2023-04-04 13:00:28 +00:00
|
|
|
log4js.configure({
|
|
|
|
appenders: {
|
|
|
|
out: {type: 'stdout'}
|
|
|
|
},
|
|
|
|
categories: {default: {appenders: ['out'], level: 'info'}},
|
|
|
|
});
|
2022-10-19 12:30:36 +00:00
|
|
|
|
2023-04-04 13:00:28 +00:00
|
|
|
const logger = log4js.getLogger();
|
2022-10-19 12:30:36 +00:00
|
|
|
|
2023-04-04 13:00:28 +00:00
|
|
|
cluster.on('taskerror', (err, data, willRetry) => {
|
|
|
|
if (willRetry)
|
|
|
|
logger.warn(`[Print] => ${err.message}\nThis job will be retried`);
|
|
|
|
else
|
|
|
|
logger.error(`[Print] => ${err.message}`);
|
|
|
|
});
|
2022-10-19 12:30:36 +00:00
|
|
|
|
2023-04-04 13:00:28 +00:00
|
|
|
cluster.on('queue', () => logger.info('Printing task initialized by pool'));
|
|
|
|
});
|
2022-10-19 12:30:36 +00:00
|
|
|
}
|
|
|
|
};
|