Skip to content

Commit 93fbe3b

Browse files
Merge pull request #2129 from OneCommunityGlobal/sundar/totalOrgEmail
Sundar: puppeteer chrome setup for azure + sonar fixes
2 parents 5fb918a + 0afc7e4 commit 93fbe3b

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

src/controllers/reportsController.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
/* eslint-disable consistent-return */
23
const fs = require('node:fs');
34
const mongoose = require('mongoose');
@@ -11,6 +12,7 @@ const emailSender = require('../utilities/emailSender');
1112
const cacheModule = require('../utilities/nodeCache');
1213

1314
const cacheUtil = cacheModule();
15+
const { getChromeExecutable } = require('../utilities/puppeteerUtil');
1416

1517
const reportsController = function () {
1618
const overviewReportHelper = overviewReportHelperClosure();
@@ -789,6 +791,7 @@ const reportsController = function () {
789791
console.log('Puppeteer email or password not found in environment variables');
790792
}
791793
const browser = await puppeteer.launch({
794+
executablePath: getChromeExecutable(),
792795
headless: true,
793796
args: ['--no-sandbox', '--disable-setuid-sandbox'],
794797
});

src/helpers/userHelper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const { NEW_USER_BLUE_SQUARE_NOTIFICATION_MESSAGE } = require('../constants/mess
3434
const timeUtils = require('../utilities/timeUtils');
3535
const Team = require('../models/team');
3636
const BlueSquareEmailAssignmentModel = require('../models/BlueSquareEmailAssignment');
37+
const { getChromeExecutable } = require('../utilities/puppeteerUtil');
3738
const myTeam = require('./helperModels/myTeam');
3839
const dashboardHelper = require('./dashboardhelper')();
3940
const reportHelper = require('./reporthelper')();
@@ -3258,6 +3259,7 @@ const userHelper = function () {
32583259
}
32593260
// launch puppeteer
32603261
const browser = await puppeteer.launch({
3262+
executablePath: getChromeExecutable(),
32613263
headless: true,
32623264
args: ['--no-sandbox', '--disable-setuid-sandbox'],
32633265
});

src/utilities/puppeteerUtil.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* eslint-disable no-console */
2+
const fs = require('node:fs');
3+
const path = require('node:path');
4+
const { execFileSync } = require('node:child_process');
5+
6+
const CACHE_DIR = '/home/puppeteer-cache';
7+
const LOCK_FILE = path.join(CACHE_DIR, 'install.lock');
8+
9+
function ensureChromeInstalled() {
10+
if (!fs.existsSync(CACHE_DIR)) {
11+
fs.mkdirSync(CACHE_DIR, { recursive: true });
12+
}
13+
14+
const chromeBase = path.join(CACHE_DIR, 'chrome');
15+
16+
let isInstalled = false;
17+
18+
if (fs.existsSync(chromeBase)) {
19+
const folders = fs.readdirSync(chromeBase);
20+
isInstalled = folders.length > 0;
21+
}
22+
23+
if (!isInstalled) {
24+
if (fs.existsSync(LOCK_FILE)) {
25+
console.log('Chrome installation already in progress...');
26+
return;
27+
}
28+
29+
fs.writeFileSync(LOCK_FILE, 'installing');
30+
31+
try {
32+
console.log('Installing Chrome...');
33+
execFileSync('npx', ['puppeteer', 'browsers', 'install', 'chrome'], {
34+
stdio: 'inherit',
35+
env: {
36+
...process.env,
37+
PUPPETEER_CACHE_DIR: CACHE_DIR,
38+
},
39+
});
40+
console.log('Chrome installed');
41+
} catch (err) {
42+
console.error('Chrome installation failed:', err);
43+
} finally {
44+
if (fs.existsSync(LOCK_FILE)) {
45+
fs.unlinkSync(LOCK_FILE);
46+
}
47+
}
48+
}
49+
}
50+
51+
function getChromeExecutable() {
52+
ensureChromeInstalled();
53+
54+
const chromeBase = path.join(CACHE_DIR, 'chrome');
55+
56+
if (!fs.existsSync(chromeBase)) {
57+
throw new Error('Chrome base directory not found');
58+
}
59+
60+
const folders = fs.readdirSync(chromeBase);
61+
62+
if (!folders.length) {
63+
throw new Error('No Chrome installation found');
64+
}
65+
66+
const latest = folders.sort().reverse()[0];
67+
68+
const chromePath = path.join(chromeBase, latest, 'chrome-linux64', 'chrome');
69+
70+
console.log('Using Chrome at:', chromePath);
71+
72+
return chromePath;
73+
}
74+
75+
module.exports = { getChromeExecutable };

0 commit comments

Comments
 (0)