From 071d15b79d40e429fb45c6b218e790231c1d2eff Mon Sep 17 00:00:00 2001 From: joan Date: Wed, 19 Jan 2022 12:29:00 +0100 Subject: [PATCH] puppeter not longer launch instances at service boot --- print/boot.js | 19 ------------------- print/core/report.js | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/print/boot.js b/print/boot.js index d9a555ead..b91bc8c32 100644 --- a/print/boot.js +++ b/print/boot.js @@ -1,11 +1,9 @@ const express = require('express'); const path = require('path'); const fs = require('fs'); -const puppeteer = require('puppeteer'); const templatesPath = path.resolve(__dirname, './templates'); const componentsPath = path.resolve(__dirname, './core/components'); -const config = require('./core/config'); module.exports = async app => { global.appPath = __dirname; @@ -55,21 +53,4 @@ module.exports = async app => { 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(); }; diff --git a/print/core/report.js b/print/core/report.js index 6c696bc75..ddd6c29ee 100644 --- a/print/core/report.js +++ b/print/core/report.js @@ -2,6 +2,7 @@ const fs = require('fs'); const path = require('path'); const config = require('./config'); const Component = require('./component'); +const puppeteer = require('puppeteer'); if (!process.env.OPENSSL_CONF) process.env.OPENSSL_CONF = '/etc/ssl/'; @@ -31,7 +32,19 @@ class Report extends Component { if (fs.existsSync(fullPath)) 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.setContent(template); @@ -51,6 +64,8 @@ class Report extends Component { const buffer = await page.pdf(options); + await browser.close(); + return buffer; }