salix/print/core/cluster.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-10-19 12:30:36 +00:00
const {Cluster} = require('puppeteer-cluster');
const log4js = require('log4js');
const {cpus} = require('os');
module.exports = {
init() {
if (this.pool) return;
Cluster.launch({
2024-02-05 12:19:44 +00:00
concurrency: Cluster.CONCURRENCY_PAGE,
maxConcurrency: cpus().length,
puppeteerOptions: {
2023-05-23 11:31:09 +00:00
headless: 'new',
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--no-zygote'
]
}
}).then(cluster => {
this.pool = cluster;
2022-10-19 12:30:36 +00:00
log4js.configure({
appenders: {
out: {type: 'stdout'}
},
categories: {default: {appenders: ['out'], level: 'info'}},
});
2022-10-19 12:30:36 +00:00
const logger = log4js.getLogger();
2022-10-19 12:30:36 +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
2024-02-06 11:12:45 +00:00
cluster.on('queue', () =>
process.env.SPEC_IS_RUNNING === 'false' && logger.info('Printing task initialized by pool'));
});
2022-10-19 12:30:36 +00:00
}
};