细节优化
This commit is contained in:
parent
52c5079489
commit
4604a5985f
|
@ -8,6 +8,8 @@ port: 2536
|
|||
chromium_path:
|
||||
# puppeteer接口地址
|
||||
puppeteer_ws:
|
||||
# puppeteer截图超时时间
|
||||
puppeteer_timeout:
|
||||
|
||||
# 米游社接口代理地址,国际服用
|
||||
proxyAddress:
|
||||
|
|
|
@ -32,7 +32,7 @@
|
|||
"node-xlsx": "^0.23.0",
|
||||
"oicq": "link:lib/modules/oicq",
|
||||
"pm2": "^5.3.0",
|
||||
"puppeteer": "^21.3.4",
|
||||
"puppeteer": "^21.3.6",
|
||||
"redis": "^4.6.10",
|
||||
"sequelize": "^6.33.0",
|
||||
"sqlite3": "^5.1.6",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import plugin from '../../lib/plugins/plugin.js'
|
||||
export class newcomer extends plugin {
|
||||
constructor () {
|
||||
constructor() {
|
||||
super({
|
||||
name: '欢迎新人',
|
||||
dsc: '新人入群欢迎',
|
||||
|
@ -11,14 +11,14 @@ export class newcomer extends plugin {
|
|||
}
|
||||
|
||||
/** 接受到消息都会执行一次 */
|
||||
async accept () {
|
||||
async accept() {
|
||||
if (this.e.user_id == this.e.self_id) return
|
||||
|
||||
/** 定义入群欢迎内容 */
|
||||
let msg = '欢迎新人!'
|
||||
/** 冷却cd 30s */
|
||||
let cd = 30
|
||||
|
||||
if (this.e.user_id == this.e.bot.uin) return
|
||||
|
||||
/** cd */
|
||||
let key = `Yz:newcomers:${this.e.group_id}`
|
||||
if (await redis.get(key)) return
|
||||
|
@ -34,7 +34,7 @@ export class newcomer extends plugin {
|
|||
}
|
||||
|
||||
export class outNotice extends plugin {
|
||||
constructor () {
|
||||
constructor() {
|
||||
super({
|
||||
name: '退群通知',
|
||||
dsc: 'xx退群了',
|
||||
|
@ -45,8 +45,8 @@ export class outNotice extends plugin {
|
|||
this.tips = '退群了'
|
||||
}
|
||||
|
||||
async accept () {
|
||||
if (this.e.user_id == this.e.bot.uin) return
|
||||
async accept() {
|
||||
if (this.e.user_id == this.e.self_id) return
|
||||
|
||||
let name, msg
|
||||
if (this.e.member) {
|
||||
|
@ -61,4 +61,4 @@ export class outNotice extends plugin {
|
|||
logger.mark(`[退出通知]${this.e.logText} ${msg}`)
|
||||
await this.reply(msg)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@ export class quit extends plugin {
|
|||
}
|
||||
|
||||
async accept () {
|
||||
if (this.e.user_id != this.e.bot.uin) return
|
||||
if (this.e.user_id != this.e.self_id) return
|
||||
|
||||
let other = cfg.other
|
||||
if (other.autoQuit <= 0) return
|
||||
|
@ -18,7 +18,7 @@ export class quit extends plugin {
|
|||
/** 判断主人,主人邀请不退群 */
|
||||
let gl = await this.e.group.getMemberMap()
|
||||
for (let qq of cfg.masterQQ) {
|
||||
if (gl.has(Number(qq))) {
|
||||
if (gl.has(Number(qq) || String(qq))) {
|
||||
logger.mark(`[主人拉群] ${this.e.group_id}`)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -18,3 +18,6 @@ args:
|
|||
- --disable-setuid-sandbox
|
||||
- --no-sandbox
|
||||
- --no-zygote
|
||||
|
||||
# puppeteer截图超时时间
|
||||
puppeteerTimeout:
|
||||
|
|
|
@ -9,9 +9,11 @@ import { Data } from '#miao'
|
|||
const _path = process.cwd()
|
||||
// mac地址
|
||||
let mac = ''
|
||||
// 超时计时器
|
||||
let overtimeList = []
|
||||
|
||||
export default class Puppeteer extends Renderer {
|
||||
constructor(config) {
|
||||
constructor (config) {
|
||||
super({
|
||||
id: 'puppeteer',
|
||||
type: 'image',
|
||||
|
@ -41,12 +43,14 @@ export default class Puppeteer extends Renderer {
|
|||
/** chromium其他路径 */
|
||||
this.config.wsEndpoint = config.puppeteerWS || cfg?.bot?.puppeteer_ws
|
||||
}
|
||||
/** puppeteer超时超时时间 */
|
||||
this.puppeteerTimeout = config.puppeteerTimeout || cfg?.bot?.puppeteer_timeout || 0
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化chromium
|
||||
*/
|
||||
async browserInit() {
|
||||
async browserInit () {
|
||||
if (this.browser) return this.browser
|
||||
if (this.lock) return false
|
||||
this.lock = true
|
||||
|
@ -64,7 +68,7 @@ export default class Puppeteer extends Renderer {
|
|||
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((err) => {
|
||||
const browserWSEndpoint = await puppeteer.connect({ browserWSEndpoint: browserUrl }).catch(() => {
|
||||
logger.error('puppeteer Chromium 缓存的实例已关闭')
|
||||
redis.del(this.browserMacKey)
|
||||
})
|
||||
|
@ -117,7 +121,7 @@ export default class Puppeteer extends Renderer {
|
|||
}
|
||||
|
||||
/** 监听Chromium实例是否断开 */
|
||||
this.browser.on('disconnected', (e) => {
|
||||
this.browser.on('disconnected', () => {
|
||||
logger.error('Chromium 实例关闭或崩溃!')
|
||||
this.browser = false
|
||||
})
|
||||
|
@ -127,23 +131,31 @@ export default class Puppeteer extends Renderer {
|
|||
|
||||
// 获取Mac地址
|
||||
getMac () {
|
||||
const mac = '00:00:00:00:00:00'
|
||||
let mac = '00:00:00:00:00:00'
|
||||
try {
|
||||
const network = os.networkInterfaces()
|
||||
let macFlag = false
|
||||
for (const a in network) {
|
||||
for (const i of network[a]) {
|
||||
if (i.mac && i.mac != mac) {
|
||||
return i.mac
|
||||
if (i.mac && i.mac !== mac) {
|
||||
macFlag = true
|
||||
mac = i.mac
|
||||
break
|
||||
}
|
||||
}
|
||||
if (macFlag) {
|
||||
break
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
}
|
||||
mac = mac.replace(/:/g, '')
|
||||
return mac
|
||||
}
|
||||
|
||||
/**
|
||||
* `chromium` 截图
|
||||
* @param name
|
||||
* @param data 模板参数
|
||||
* @param data.tplFile 模板路径,必传
|
||||
* @param data.saveId 生成html名称,为空name代替
|
||||
|
@ -154,9 +166,9 @@ export default class Puppeteer extends Renderer {
|
|||
* @param data.multiPage 是否分页截图,默认false
|
||||
* @param data.multiPageHeight 分页状态下页面高度,默认4000
|
||||
* @param data.pageGotoParams 页面goto时的参数
|
||||
* @return img/[]img 不做segment包裹
|
||||
* @return img 不做segment包裹
|
||||
*/
|
||||
async screenshot(name, data = {}) {
|
||||
async screenshot (name, data = {}) {
|
||||
if (!await this.browserInit()) {
|
||||
return false
|
||||
}
|
||||
|
@ -173,6 +185,23 @@ export default class Puppeteer extends Renderer {
|
|||
let ret = []
|
||||
this.shoting.push(name)
|
||||
|
||||
const puppeteerTimeout = this.puppeteerTimeout
|
||||
let overtime
|
||||
let overtimeFlag = false
|
||||
if (puppeteerTimeout > 0) {
|
||||
// TODO 截图超时处理
|
||||
overtime = setTimeout(() => {
|
||||
if (!overtimeFlag) {
|
||||
logger.error(`[图片生成][${name}] 截图超时,当前等待队列:${this.shoting.join(',')}`)
|
||||
this.restart(true)
|
||||
this.shoting = []
|
||||
overtimeList.forEach(item => {
|
||||
clearTimeout(item)
|
||||
})
|
||||
}
|
||||
}, puppeteerTimeout)
|
||||
}
|
||||
|
||||
try {
|
||||
const page = await this.browser.newPage()
|
||||
let pageGotoParams = lodash.extend({ timeout: 120000 }, data.pageGotoParams || {})
|
||||
|
@ -254,6 +283,12 @@ export default class Puppeteer extends Renderer {
|
|||
this.browser = false
|
||||
ret = []
|
||||
return false
|
||||
} finally {
|
||||
if (overtime) {
|
||||
overtimeFlag = true
|
||||
clearTimeout(overtime)
|
||||
overtimeList = []
|
||||
}
|
||||
}
|
||||
|
||||
this.shoting.pop()
|
||||
|
@ -263,22 +298,22 @@ export default class Puppeteer extends Renderer {
|
|||
return false
|
||||
}
|
||||
|
||||
this.restart()
|
||||
this.restart(false)
|
||||
|
||||
return data.multiPage ? ret : ret[0]
|
||||
}
|
||||
|
||||
/** 重启 */
|
||||
restart() {
|
||||
restart (force = false) {
|
||||
/** 截图超过重启数时,自动关闭重启浏览器,避免生成速度越来越慢 */
|
||||
if (this.renderNum % this.restartNum === 0) {
|
||||
if (this.shoting.length <= 0) {
|
||||
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 关闭重启...')
|
||||
logger.info(`puppeteer Chromium ${force ? '强制' : ''}关闭重启...`)
|
||||
}, 100)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue