puppeter not longer launch instances at service boot
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2022-01-19 12:29:00 +01:00
parent 38965ba50e
commit 071d15b79d
2 changed files with 16 additions and 20 deletions

View File

@ -1,11 +1,9 @@
const express = require('express'); const express = require('express');
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const puppeteer = require('puppeteer');
const templatesPath = path.resolve(__dirname, './templates'); const templatesPath = path.resolve(__dirname, './templates');
const componentsPath = path.resolve(__dirname, './core/components'); const componentsPath = path.resolve(__dirname, './core/components');
const config = require('./core/config');
module.exports = async app => { module.exports = async app => {
global.appPath = __dirname; global.appPath = __dirname;
@ -55,21 +53,4 @@ module.exports = async app => {
app.use(`/api/${templateName}/assets`, express.static(assetsDir)); app.use(`/api/${templateName}/assets`, express.static(assetsDir));
}); });
}); });
// Instantiate Puppeteer browser
async function launchBrowser() {
config.browser = await puppeteer.launch({
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--single-process',
'--no-zygote'
]
});
config.browser.on('disconnected', launchBrowser);
}
launchBrowser();
}; };

View File

@ -2,6 +2,7 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const config = require('./config'); const config = require('./config');
const Component = require('./component'); const Component = require('./component');
const puppeteer = require('puppeteer');
if (!process.env.OPENSSL_CONF) if (!process.env.OPENSSL_CONF)
process.env.OPENSSL_CONF = '/etc/ssl/'; process.env.OPENSSL_CONF = '/etc/ssl/';
@ -31,7 +32,19 @@ class Report extends Component {
if (fs.existsSync(fullPath)) if (fs.existsSync(fullPath))
options = require(optionsPath); options = require(optionsPath);
const page = (await config.browser.pages())[0]; const browser = await puppeteer.launch({
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--single-process',
'--no-zygote'
]
});
// browser.on('disconnected', launchBrowser);
const page = (await browser.pages())[0];
await page.emulateMedia('screen'); await page.emulateMedia('screen');
await page.setContent(template); await page.setContent(template);
@ -51,6 +64,8 @@ class Report extends Component {
const buffer = await page.pdf(options); const buffer = await page.pdf(options);
await browser.close();
return buffer; return buffer;
} }