Commit f1c0112e authored by youxiaoji's avatar youxiaoji

* [playwright 初始化后不再初始化]

parent 488dd115
......@@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import jakarta.annotation.PreDestroy;
import java.util.concurrent.*;
/**
......@@ -54,21 +55,11 @@ public class PlaywrightManagerImpl implements PlaywrightManager {
log.info("正在初始化Playwright管理器...");
// 创建Playwright实例
this.playwright = Playwright.create();
// 启动Chrome浏览器,无头模式
this.browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
.setHeadless(true)
.setArgs(java.util.Arrays.asList(
"--no-sandbox",
"--disable-dev-shm-usage",
"--disable-gpu",
"--remote-allow-origins=*")));
this.browser.onDisconnected((browser) -> {
log.info("浏览器实例已断开连接");
this.browser.close();
userContexts.clear();
if (playwright == null) {
this.playwright = Playwright.create();
}
if (browser == null) {
// 启动Chrome浏览器,无头模式
this.browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
.setHeadless(true)
.setArgs(java.util.Arrays.asList(
......@@ -76,8 +67,20 @@ public class PlaywrightManagerImpl implements PlaywrightManager {
"--disable-dev-shm-usage",
"--disable-gpu",
"--remote-allow-origins=*")));
});
this.browser.onDisconnected((browser) -> {
log.info("浏览器实例已断开连接");
this.browser.close();
userContexts.clear();
this.browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
.setHeadless(true)
.setArgs(java.util.Arrays.asList(
"--no-sandbox",
"--disable-dev-shm-usage",
"--disable-gpu",
"--remote-allow-origins=*")));
});
}
// 每5分钟检查一次超时的用户上下文
cleanupScheduler.scheduleAtFixedRate(this::cleanupExpiredContexts,
5, 3600, TimeUnit.MINUTES);
......@@ -126,8 +129,8 @@ public class PlaywrightManagerImpl implements PlaywrightManager {
.setViewportSize(1920, 1080) // 设置视口大小为全高清分辨率,适用于Windows 11桌面环境
.setUserAgent(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"); // 设置用户代理为Windows
// 11
// Chrome浏览器
// 11
// Chrome浏览器
return getUserContext(userId, options);
}
......@@ -182,7 +185,7 @@ public class PlaywrightManagerImpl implements PlaywrightManager {
/**
* 检查BrowserContext是否已关闭
*
*
* @param context 要检查的BrowserContext
* @return 如果上下文已关闭则返回true,否则返回false
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment