code: make stability

This commit is contained in:
ShawnPhang 2023-09-16 20:37:11 +08:00
parent 2816307513
commit 68f610e63e
3 changed files with 26 additions and 14 deletions

View File

@ -3,7 +3,7 @@
* @Date: 2022-02-01 13:41:59 * @Date: 2022-02-01 13:41:59
* @Description: * @Description:
* @LastEditors: ShawnPhang <https://m.palxp.cn> * @LastEditors: ShawnPhang <https://m.palxp.cn>
* @LastEditTime: 2023-09-07 23:42:43 * @LastEditTime: 2023-09-15 10:40:41
*/ */
const isDev = process.env.NODE_ENV === 'development' const isDev = process.env.NODE_ENV === 'development'

View File

@ -3,7 +3,7 @@
* @Date: 2021-09-30 14:47:22 * @Date: 2021-09-30 14:47:22
* @Description: * @Description:
* @LastEditors: ShawnPhang <https://m.palxp.cn> * @LastEditors: ShawnPhang <https://m.palxp.cn>
* @LastEditTime: 2023-09-13 16:45:02 * @LastEditTime: 2023-09-16 20:30:38
*/ */
const isDev = process.env.NODE_ENV === 'development' const isDev = process.env.NODE_ENV === 'development'
const puppeteer = require('puppeteer') const puppeteer = require('puppeteer')
@ -17,14 +17,21 @@ const maximum = 5000 // 最大宽高限制,超过截断以防止服务崩溃
const saveScreenshot = async (url: string, { path, width, height, thumbPath, size = 0, quality = 0, prevent, ua, devices, scale, wait }: any) => { const saveScreenshot = async (url: string, { path, width, height, thumbPath, size = 0, quality = 0, prevent, ua, devices, scale, wait }: any) => {
return new Promise(async (resolve: Function) => { return new Promise(async (resolve: Function) => {
let isPageLoad = false
// 启动浏览器 // 启动浏览器
const browser = await puppeteer.launch({ let browser = await puppeteer.launch({
headless: true, // !isDev, headless: true, // !isDev,
executablePath: isDev ? null : executablePath, executablePath: isDev ? null : executablePath,
ignoreHTTPSErrors: true, // 忽略https安全提示 ignoreHTTPSErrors: true, // 忽略https安全提示
args: ['no-first-run', 'single-process', 'disable-gpu', 'no-zygote', 'disable-dev-shm-usage', '--no-sandbox', '--disable-setuid-sandbox', `--window-size=${width},${height}`], // 优化配置 args: ['no-first-run', 'single-process', 'disable-gpu', 'no-zygote', 'disable-dev-shm-usage', '--no-sandbox', '--disable-setuid-sandbox', `--window-size=${width},${height}`], // 优化配置
defaultViewport: null, defaultViewport: null,
}) })
const regulators = setTimeout(() => {
browser && browser.close()
browser = null
console.log('超时强制释放浏览器')
resolve()
}, forceTimeOut * 1000)
// 打开页面 // 打开页面
const page = await browser.newPage() const page = await browser.newPage()
@ -51,6 +58,7 @@ const saveScreenshot = async (url: string, { path, width, height, thumbPath, siz
await page.screenshot({ path, fullPage: true }) await page.screenshot({ path, fullPage: true })
// 关闭浏览器 // 关闭浏览器
await browser.close() await browser.close()
browser = null
compress() compress()
clearTimeout(regulators) clearTimeout(regulators)
resolve() resolve()
@ -61,7 +69,7 @@ const saveScreenshot = async (url: string, { path, width, height, thumbPath, siz
// console.log('-> 开始截图') // console.log('-> 开始截图')
await page.screenshot({ path }) await page.screenshot({ path })
// 关闭浏览器 // 关闭浏览器
await browser.close() browserClose()
compress() compress()
// console.log('浏览器已释放'); // console.log('浏览器已释放');
clearTimeout(regulators) clearTimeout(regulators)
@ -70,16 +78,10 @@ const saveScreenshot = async (url: string, { path, width, height, thumbPath, siz
// 地址栏输入网页地址 // 地址栏输入网页地址
await page.goto(url, { waitUntil: 'domcontentloaded' }) await page.goto(url, { waitUntil: 'domcontentloaded' })
isPageLoad = true
// 截图: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagescreenshotoptions
const regulators = setTimeout(() => {
browser && browser.close()
console.log('强制释放浏览器')
resolve()
}, forceTimeOut * 1000)
function compress() {
// 压缩图片 // 压缩图片
function compress() {
try { try {
thumbPath && thumbPath &&
images(path) images(path)
@ -121,6 +123,16 @@ const saveScreenshot = async (url: string, { path, width, height, thumbPath, siz
}, timeout) }, timeout)
}) })
} }
// 异步关闭Error: Navigation failed because browser has disconnected!
async function browserClose() {
if (isPageLoad) {
await browser.close()
browser = null
} else {
browser = null
}
}
}) })
} }

View File

@ -3,7 +3,7 @@
* @Date: 2023-09-07 22:56:09 * @Date: 2023-09-07 22:56:09
* @Description: * @Description:
* @LastEditors: ShawnPhang <https://m.palxp.cn> * @LastEditors: ShawnPhang <https://m.palxp.cn>
* @LastEditTime: 2023-09-15 12:47:16 * @LastEditTime: 2023-09-16 20:32:48
*/ */
// const prefix = import.meta.env // const prefix = import.meta.env
const prefix = process.env const prefix = process.env
@ -24,5 +24,5 @@ export default {
// ICONFONT_URL: '//at.alicdn.com/t/font_3223711_74mlzj4jdue.css', // ICONFONT_URL: '//at.alicdn.com/t/font_3223711_74mlzj4jdue.css',
ICONFONT_URL: '//at.alicdn.com/t/font_2717063_ypy8vprc3b.css?display=swap', ICONFONT_URL: '//at.alicdn.com/t/font_2717063_ypy8vprc3b.css?display=swap',
ICONFONT_EXTRA: '//at.alicdn.com/t/c/font_3228074_6qsac4kteu7.css?&display=swap', ICONFONT_EXTRA: '//at.alicdn.com/t/c/font_3228074_6qsac4kteu7.css?&display=swap',
QINIUYUN_PLUGIN: 'https://cdn.jsdelivr.net/npm/qiniu-js@2.5.5/dist/qiniu.min.js', QINIUYUN_PLUGIN: 'https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/qiniu-js/3.4.0/qiniu.min.js',
} }