与上游同步修改
This commit is contained in:
parent
730b58ed08
commit
0c737301e8
|
@ -1,5 +1,5 @@
|
|||
const os = require('os')
|
||||
const { existsSync, realpathSync } = require('fs')
|
||||
const { existsSync } = require('fs')
|
||||
const { execSync } = require('child_process')
|
||||
const arch = os.arch()
|
||||
|
||||
|
@ -9,15 +9,14 @@ let executablePath
|
|||
// linux / android
|
||||
if (process.platform === 'linux' || process.platform === 'android') {
|
||||
for (const item of [
|
||||
'chromium',
|
||||
'chromium-browser',
|
||||
'chrome',
|
||||
'chrome-browser',
|
||||
"chromium",
|
||||
"chromium-browser",
|
||||
"chrome",
|
||||
]) {
|
||||
try {
|
||||
const chromiumPath = execSync(`command -v ${item}`).toString().trim()
|
||||
if (chromiumPath && existsSync(chromiumPath)) {
|
||||
executablePath = realpathSync(chromiumPath)
|
||||
executablePath = chromiumPath
|
||||
break
|
||||
}
|
||||
} catch (err) {
|
||||
|
@ -41,11 +40,11 @@ if (process.platform === 'darwin') {
|
|||
// windows 或其他
|
||||
if (!executablePath) {
|
||||
for (const item of [
|
||||
'C:/Program Files/Google/Chrome/Application/chrome.exe',
|
||||
'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe',
|
||||
'/usr/bin/chromium',
|
||||
'/usr/bin/chromium-browser',
|
||||
'/snap/bin/chromium',
|
||||
'/usr/bin/chrome',
|
||||
'C:/Program Files/Google/Chrome/Application/chrome.exe',
|
||||
'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe',
|
||||
]) {
|
||||
if (existsSync(item)) {
|
||||
executablePath = item
|
||||
|
|
|
@ -23,7 +23,7 @@ export default class PuppeteerRenderer {
|
|||
/** 截图次数 */
|
||||
this.renderNum = 0
|
||||
this.config = {
|
||||
headless: Data.def(config.headless, true),
|
||||
headless: Data.def(config.headless, 'new'),
|
||||
args: Data.def(config.args, [
|
||||
'--disable-gpu',
|
||||
'--disable-setuid-sandbox',
|
||||
|
@ -61,11 +61,15 @@ export default class PuppeteerRenderer {
|
|||
* 初始化chromium
|
||||
*/
|
||||
async browserInit () {
|
||||
if (this.browser) return this.browser
|
||||
if (this.lock) return false
|
||||
if (this.browser) {
|
||||
return this.browser
|
||||
}
|
||||
if (this.lock) {
|
||||
return false
|
||||
}
|
||||
this.lock = true
|
||||
|
||||
logger.mark('puppeteer Chromium 启动中...')
|
||||
logger.info('puppeteer Chromium 启动中...')
|
||||
|
||||
let connectFlag = false
|
||||
try {
|
||||
|
@ -77,7 +81,7 @@ export default class PuppeteerRenderer {
|
|||
// 是否有browser实例
|
||||
const browserUrl = (await redis.get(this.browserMacKey)) || this.config.wsEndpoint
|
||||
if (browserUrl) {
|
||||
logger.mark(`puppeteer Chromium from ${browserUrl}`)
|
||||
logger.info(`puppeteer Chromium from ${browserUrl}`)
|
||||
const browserWSEndpoint = await puppeteer.connect({ browserWSEndpoint: browserUrl }).catch((err) => {
|
||||
logger.error('puppeteer Chromium 缓存的实例已关闭')
|
||||
redis.del(this.browserMacKey)
|
||||
|
@ -91,7 +95,7 @@ export default class PuppeteerRenderer {
|
|||
}
|
||||
}
|
||||
} catch (e) {
|
||||
logger.mark('puppeteer Chromium 不存在已有实例')
|
||||
logger.info('puppeteer Chromium 不存在已有实例')
|
||||
}
|
||||
|
||||
if (!this.browser || !connectFlag) {
|
||||
|
@ -103,12 +107,12 @@ export default class PuppeteerRenderer {
|
|||
} else {
|
||||
logger.error(err.toString())
|
||||
if (errMsg.includes('Could not find Chromium')) {
|
||||
logger.error('没有正确安装Chromium,可以尝试执行安装命令:node ./node_modules/puppeteer/install.js')
|
||||
} else if (errMsg.includes('libatk-bridge')) {
|
||||
logger.error('没有正确安装Chromium,可尝试执行 sudo yum install -y chromium')
|
||||
logger.error('没有正确安装 Chromium,可以尝试执行安装命令:node node_modules/puppeteer/install.js')
|
||||
} else if (errMsg.includes('cannot open shared object file')) {
|
||||
logger.error('没有正确安装 Chromium 运行库')
|
||||
}
|
||||
}
|
||||
console.log(err, trace)
|
||||
logger.error(err, trace)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -119,20 +123,20 @@ export default class PuppeteerRenderer {
|
|||
return false
|
||||
}
|
||||
if (connectFlag) {
|
||||
logger.mark('puppeteer Chromium 已连接启动的实例')
|
||||
logger.info('puppeteer Chromium 已连接启动的实例')
|
||||
} else {
|
||||
console.log('chromium', this.browser.wsEndpoint())
|
||||
logger.info(`[Chromium] ${this.browser.wsEndpoint()}`)
|
||||
if (process.env.pm_id && this.browserMacKey) {
|
||||
//缓存一下实例30天
|
||||
// 缓存一下实例30天
|
||||
const expireTime = 60 * 60 * 24 * 30
|
||||
await redis.set(this.browserMacKey, this.browser.wsEndpoint(), { EX: expireTime })
|
||||
}
|
||||
logger.mark('puppeteer Chromium 启动成功')
|
||||
logger.info('puppeteer Chromium 启动成功')
|
||||
}
|
||||
|
||||
/** 监听Chromium实例是否断开 */
|
||||
this.browser.on('disconnected', (e) => {
|
||||
logger.error('Chromium实例关闭或崩溃!')
|
||||
logger.error('Chromium 实例关闭或崩溃!')
|
||||
this.browser = false
|
||||
})
|
||||
|
||||
|
@ -140,29 +144,19 @@ export default class PuppeteerRenderer {
|
|||
}
|
||||
|
||||
// 获取Mac地址
|
||||
async getMac () {
|
||||
// 获取Mac地址
|
||||
let mac = '00:00:00:00:00:00'
|
||||
getMac () {
|
||||
const mac = '00:00:00:00:00:00'
|
||||
try {
|
||||
const network = os.networkInterfaces()
|
||||
let osMac
|
||||
// 判断系统
|
||||
if (os.platform() === 'win32') {
|
||||
// windows下获取mac地址
|
||||
let osMacList = Object.keys(network).map(key => network[key]).flat()
|
||||
osMacList = osMacList.filter(item => item.family === 'IPv4' && item.mac !== mac)
|
||||
osMac = osMacList[0].mac
|
||||
} else if (os.platform() === 'linux') {
|
||||
// linux下获取mac地址
|
||||
osMac = network.enp6s18.filter(item => item.family === 'IPv4' && item.mac !== mac)[0].mac
|
||||
}
|
||||
if (osMac) {
|
||||
mac = String(osMac)
|
||||
for (const a in network) {
|
||||
for (const i of network[a]) {
|
||||
if (i.mac && i.mac != mac) {
|
||||
return i.mac
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('获取Mac地址失败', e.toString())
|
||||
}
|
||||
mac = mac.replace(/:/g, '')
|
||||
return mac
|
||||
}
|
||||
|
||||
|
@ -187,7 +181,9 @@ export default class PuppeteerRenderer {
|
|||
const pageHeight = data.multiPageHeight || 4000
|
||||
|
||||
let savePath = this.dealTpl(name, data)
|
||||
if (!savePath) return false
|
||||
if (!savePath) {
|
||||
return false
|
||||
}
|
||||
|
||||
let buff = ''
|
||||
let start = Date.now()
|
||||
|
@ -225,7 +221,7 @@ export default class PuppeteerRenderer {
|
|||
if (!data.multiPage) {
|
||||
buff = await body.screenshot(randData)
|
||||
/** 计算图片大小 */
|
||||
const kb = (buff.length / 1024).toFixed(2) + 'kb'
|
||||
const kb = (buff.length / 1024).toFixed(2) + 'KB'
|
||||
logger.mark(`[图片生成][${name}][${this.renderNum}次] ${kb} ${logger.green(`${Date.now() - start}ms`)}`)
|
||||
this.renderNum++
|
||||
ret.push(buff)
|
||||
|
@ -252,11 +248,13 @@ export default class PuppeteerRenderer {
|
|||
} else {
|
||||
buff = await page.screenshot(randData)
|
||||
}
|
||||
if (num > 2) await Data.sleep(200)
|
||||
if (num > 2) {
|
||||
await Data.sleep(200)
|
||||
}
|
||||
this.renderNum++
|
||||
|
||||
/** 计算图片大小 */
|
||||
const kb = (buff.length / 1024).toFixed(2) + 'kb'
|
||||
const kb = (buff.length / 1024).toFixed(2) + 'KB'
|
||||
logger.mark(`[图片生成][${name}][${i}/${num}] ${kb}`)
|
||||
ret.push(buff)
|
||||
}
|
||||
|
@ -265,9 +263,8 @@ export default class PuppeteerRenderer {
|
|||
}
|
||||
}
|
||||
page.close().catch((err) => logger.error(err))
|
||||
|
||||
} catch (error) {
|
||||
logger.error(`图片生成失败:${name}:${error}`)
|
||||
logger.error(`[图片生成][${name}] 图片生成失败:${error}`)
|
||||
/** 关闭浏览器 */
|
||||
if (this.browser) {
|
||||
await this.browser.close().catch((err) => logger.error(err))
|
||||
|
@ -280,7 +277,7 @@ export default class PuppeteerRenderer {
|
|||
this.shoting.pop()
|
||||
|
||||
if (ret.length === 0 || !ret[0]) {
|
||||
logger.error(`图片生成为空:${name}`)
|
||||
logger.error(`[图片生成][${name}] 图片生成为空`)
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -323,7 +320,9 @@ export default class PuppeteerRenderer {
|
|||
|
||||
/** 监听配置文件 */
|
||||
watch (tplFile) {
|
||||
if (this.watcher[tplFile]) return
|
||||
if (this.watcher[tplFile]) {
|
||||
return
|
||||
}
|
||||
|
||||
const watcher = chokidar.watch(tplFile)
|
||||
watcher.on('change', path => {
|
||||
|
@ -344,10 +343,9 @@ export default class PuppeteerRenderer {
|
|||
await this.browser.close().catch((err) => logger.error(err))
|
||||
}
|
||||
this.browser = false
|
||||
logger.mark('puppeteer 关闭重启...')
|
||||
logger.info('puppeteer Chromium 关闭重启...')
|
||||
}, 100)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue