优化 Chromium

This commit is contained in:
🌌 2024-03-07 20:43:30 +08:00
parent 70cc336360
commit c815d50be8
2 changed files with 87 additions and 107 deletions

View File

@ -10,7 +10,7 @@ chromiumPath:
puppeteerWS:
# headless
headless: 'new'
headless: "new"
# puppeteer启动args注意args的--前缀
args:

View File

@ -1,23 +1,20 @@
import Renderer from '../../../lib/renderer/Renderer.js'
import os from 'node:os'
import lodash from 'lodash'
import puppeteer from 'puppeteer'
import Renderer from "../../../lib/renderer/Renderer.js"
import os from "node:os"
import lodash from "lodash"
import puppeteer from "puppeteer"
// 暂时保留对原config的兼容
import cfg from '../../../lib/config/config.js'
import { Data } from '#miao'
import cfg from "../../../lib/config/config.js"
const _path = process.cwd()
// mac地址
let mac = ''
// 超时计时器
let overtimeList = []
let mac = ""
export default class Puppeteer extends Renderer {
constructor (config) {
constructor(config) {
super({
id: 'puppeteer',
type: 'image',
render: 'screenshot'
id: "puppeteer",
type: "image",
render: "screenshot"
})
this.browser = false
this.lock = false
@ -27,22 +24,20 @@ export default class Puppeteer extends Renderer {
/** 截图次数 */
this.renderNum = 0
this.config = {
headless: Data.def(config.headless, true),
args: Data.def(config.args, [
'--disable-gpu',
'--disable-setuid-sandbox',
'--no-sandbox',
'--no-zygote'
])
headless: config.headless || "new",
args: config.args || [
"--disable-gpu",
"--disable-setuid-sandbox",
"--no-sandbox",
"--no-zygote"
]
}
if (config.chromiumPath || cfg?.bot?.chromium_path) {
if (config.chromiumPath || cfg?.bot?.chromium_path)
/** chromium其他路径 */
this.config.executablePath = config.chromiumPath || cfg?.bot?.chromium_path
}
if (config.puppeteerWS || cfg?.bot?.puppeteer_ws) {
if (config.puppeteerWS || cfg?.bot?.puppeteer_ws)
/** chromium其他路径 */
this.config.wsEndpoint = config.puppeteerWS || cfg?.bot?.puppeteer_ws
}
/** puppeteer超时超时时间 */
this.puppeteerTimeout = config.puppeteerTimeout || cfg?.bot?.puppeteer_timeout || 0
}
@ -50,12 +45,12 @@ export default class Puppeteer extends Renderer {
/**
* 初始化chromium
*/
async browserInit () {
async browserInit() {
if (this.browser) return this.browser
if (this.lock) return false
this.lock = true
logger.info('puppeteer Chromium 启动中...')
logger.info("puppeteer Chromium 启动中...")
let connectFlag = false
try {
@ -67,35 +62,32 @@ export default class Puppeteer extends Renderer {
// 是否有browser实例
const browserUrl = (await redis.get(this.browserMacKey)) || this.config.wsEndpoint
if (browserUrl) {
logger.info(`puppeteer Chromium from ${browserUrl}`)
const browserWSEndpoint = await puppeteer.connect({ browserWSEndpoint: browserUrl }).catch(() => {
logger.error('puppeteer Chromium 缓存的实例已关闭')
redis.del(this.browserMacKey)
})
// 如果有实例,直接使用
if (browserWSEndpoint) {
this.browser = browserWSEndpoint
if (this.browser) {
try {
const browserWSEndpoint = await puppeteer.connect({ browserWSEndpoint: browserUrl })
// 如果有实例,直接使用
if (browserWSEndpoint) {
this.browser = browserWSEndpoint
connectFlag = true
}
logger.info(`puppeteer Chromium 连接成功 ${browserUrl}`)
} catch (err) {
await redis.del(this.browserMacKey)
}
}
} catch (e) {
logger.info('puppeteer Chromium 不存在已有实例')
}
} catch (err) {}
if (!this.browser || !connectFlag) {
// 如果没有实例初始化puppeteer
this.browser = await puppeteer.launch(this.config).catch((err, trace) => {
let errMsg = err.toString() + (trace ? trace.toString() : '')
if (typeof err == 'object') {
let errMsg = err.toString() + (trace ? trace.toString() : "")
if (typeof err == "object") {
logger.error(JSON.stringify(err))
} 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('cannot open shared object file')) {
logger.error('没有正确安装 Chromium 运行库')
if (errMsg.includes("Could not find Chromium")) {
logger.error("没有正确安装 Chromium可以尝试执行安装命令node node_modules/puppeteer/install.js")
} else if (errMsg.includes("cannot open shared object file")) {
logger.error("没有正确安装 Chromium 运行库")
}
}
logger.error(err, trace)
@ -105,24 +97,26 @@ export default class Puppeteer extends Renderer {
this.lock = false
if (!this.browser) {
logger.error('puppeteer Chromium 启动失败')
logger.error("puppeteer Chromium 启动失败")
return false
}
if (connectFlag) {
logger.info('puppeteer Chromium 已连接启动的实例')
} else {
logger.info(`[Chromium] ${this.browser.wsEndpoint()}`)
if (process.env.pm_id && this.browserMacKey) {
if (!connectFlag) {
logger.info(`puppeteer Chromium 启动成功 ${this.browser.wsEndpoint()}`)
if (this.browserMacKey) {
// 缓存一下实例30天
const expireTime = 60 * 60 * 24 * 30
await redis.set(this.browserMacKey, this.browser.wsEndpoint(), { EX: expireTime })
}
logger.info('puppeteer Chromium 启动成功')
}
/** 监听Chromium实例是否断开 */
this.browser.on('disconnected', () => {
logger.error('Chromium 实例关闭或崩溃!')
this.browser.on("disconnected", async () => {
logger.info("puppeteer Chromium 实例关闭")
try {
await this.browser.close()
} catch (err) {
logger.error("puppeteer Chromium 关闭错误", err)
}
this.browser = false
})
@ -130,8 +124,8 @@ export default class Puppeteer extends Renderer {
}
// 获取Mac地址
getMac () {
let mac = '00:00:00:00:00:00'
getMac() {
let mac = "00:00:00:00:00:00"
try {
const network = os.networkInterfaces()
let macFlag = false
@ -149,7 +143,7 @@ export default class Puppeteer extends Renderer {
}
} catch (e) {
}
mac = mac.replace(/:/g, '')
mac = mac.replace(/:/g, "")
return mac
}
@ -168,18 +162,15 @@ export default class Puppeteer extends Renderer {
* @param data.pageGotoParams 页面goto时的参数
* @return img 不做segment包裹
*/
async screenshot (name, data = {}) {
if (!await this.browserInit()) {
async screenshot(name, data = {}) {
if (!await this.browserInit())
return false
}
const pageHeight = data.multiPageHeight || 4000
let savePath = this.dealTpl(name, data)
if (!savePath) {
return false
}
if (!savePath) return false
let buff = ''
let buff = ""
let start = Date.now()
let ret = []
@ -187,17 +178,13 @@ export default class Puppeteer extends Renderer {
const puppeteerTimeout = this.puppeteerTimeout
let overtime
let overtimeFlag = false
if (puppeteerTimeout > 0) {
// TODO 截图超时处理
overtime = setTimeout(() => {
if (!overtimeFlag) {
logger.error(`[图片生成][${name}] 截图超时,当前等待队列:${this.shoting.join(',')}`)
if (this.shoting.length) {
logger.error(`[图片生成][${name}] 截图超时,当前等待队列:${this.shoting.join(",")}`)
this.restart(true)
this.shoting = []
overtimeList.forEach(item => {
clearTimeout(item)
})
}
}, puppeteerTimeout)
}
@ -205,8 +192,8 @@ export default class Puppeteer extends Renderer {
try {
const page = await this.browser.newPage()
let pageGotoParams = lodash.extend({ timeout: 120000 }, data.pageGotoParams || {})
await page.goto(`file://${_path}${lodash.trim(savePath, '.')}`, pageGotoParams)
let body = await page.$('#container') || await page.$('body')
await page.goto(`file://${_path}${lodash.trim(savePath, ".")}`, pageGotoParams)
let body = await page.$("#container") || await page.$("body")
// 计算页面高度
const boundingBox = await body.boundingBox()
@ -214,27 +201,27 @@ export default class Puppeteer extends Renderer {
let num = 1
let randData = {
type: data.imgType || 'jpeg',
type: data.imgType || "jpeg",
omitBackground: data.omitBackground || false,
quality: data.quality || 90,
path: data.path || ''
path: data.path || ""
}
if (data.multiPage) {
randData.type = 'jpeg'
randData.type = "jpeg"
num = Math.round(boundingBox.height / pageHeight) || 1
}
if (data.imgType === 'png') {
if (data.imgType === "png") {
delete randData.quality
}
if (!data.multiPage) {
buff = await body.screenshot(randData)
/** 计算图片大小 */
const kb = (buff.length / 1024).toFixed(2) + 'KB'
logger.mark(`[图片生成][${name}][${this.renderNum}次] ${kb} ${logger.green(`${Date.now() - start}ms`)}`)
this.renderNum++
/** 计算图片大小 */
const kb = (buff.length / 1024).toFixed(2) + "KB"
logger.mark(`[图片生成][${name}][${this.renderNum}次] ${kb} ${logger.green(`${Date.now() - start}ms`)}`)
ret.push(buff)
} else {
// 分片截图
@ -260,12 +247,12 @@ export default class Puppeteer extends Renderer {
buff = await page.screenshot(randData)
}
if (num > 2) {
await Data.sleep(200)
await new Promise(resolve => setTimeout(resolve, 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)
}
@ -273,22 +260,16 @@ export default class Puppeteer extends Renderer {
logger.mark(`[图片生成][${name}] 处理完成`)
}
}
page.close().catch((err) => logger.error(err))
} catch (error) {
logger.error(`[图片生成][${name}] 图片生成失败${error}`)
page.close().catch(err => logger.error(err))
} catch (err) {
logger.error(`[图片生成][${name}] 图片生成失败`, err)
/** 关闭浏览器 */
if (this.browser) {
await this.browser.close().catch((err) => logger.error(err))
}
this.browser = false
this.restart(true)
if (overtime) clearTimeout(overtime)
ret = []
return false
} finally {
if (overtime) {
overtimeFlag = true
clearTimeout(overtime)
overtimeList = []
}
if (overtime) clearTimeout(overtime)
}
this.shoting.pop()
@ -298,24 +279,23 @@ export default class Puppeteer extends Renderer {
return false
}
this.restart(false)
this.restart()
return data.multiPage ? ret : ret[0]
}
/** 重启 */
restart (force = false) {
restart(force = false) {
/** 截图超过重启数时,自动关闭重启浏览器,避免生成速度越来越慢 */
if (this.renderNum % this.restartNum === 0 || force) {
if (this.shoting.length <= 0 || force) {
setTimeout(async () => {
if (this.browser) {
await this.browser.close().catch((err) => logger.error(err))
}
this.browser = false
logger.info(`puppeteer Chromium ${force ? '强制' : ''}关闭重启...`)
}, 100)
if (!this.browser) return
if (!force) if (this.renderNum % this.restartNum !== 0 || this.shoting.length > 0) return
setTimeout(async () => {
logger.info(`puppeteer Chromium ${force ? "强制" : ""}关闭重启...`)
try {
await this.browser.close()
} catch (err) {
logger.error("puppeteer Chromium 关闭错误", err)
}
}
this.browser = false
}, 100)
}
}
}