CatPtain commited on
Commit
5a1360b
·
verified ·
1 Parent(s): d5c2fad

Upload screenshotService.js

Browse files
backend/src/services/screenshotService.js CHANGED
@@ -25,8 +25,8 @@ class ScreenshotService {
25
 
26
  const launchOptions = {
27
  headless: 'new',
28
- timeout: 30000, // 减少超时时间
29
- protocolTimeout: 30000,
30
  args: [
31
  '--no-sandbox',
32
  '--disable-setuid-sandbox',
@@ -55,22 +55,53 @@ class ScreenshotService {
55
  '--disable-hang-monitor',
56
  '--disable-prompt-on-repost',
57
  '--memory-pressure-off',
58
- '--max_old_space_size=512' // 减少内存使用
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  ]
60
  };
61
 
62
- // 如果是Hugging Face Space环境,使用更保守的配置
63
  if (this.isHuggingFaceSpace) {
64
- console.log('检测到Hugging Face Space环境,使用保守配置');
65
  launchOptions.args.push(
66
  '--single-process',
67
- '--no-zygote',
68
- '--disable-web-security',
69
  '--disable-features=site-per-process',
70
- '--disable-site-isolation-trials'
 
71
  );
72
  }
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  this.browser = await puppeteer.launch(launchOptions);
75
  console.log('✅ Puppeteer浏览器初始化成功');
76
 
@@ -88,7 +119,7 @@ class ScreenshotService {
88
 
89
  if (this.browserLaunchRetries <= this.maxBrowserLaunchRetries) {
90
  console.log(`尝试重新初始化浏览器 (${this.browserLaunchRetries}/${this.maxBrowserLaunchRetries})`);
91
- await this.delay(2000); // 等待2秒后重试
92
  return this.initBrowser();
93
  } else {
94
  // 如果Puppeteer完全失败,返回null,使用fallback方法
 
25
 
26
  const launchOptions = {
27
  headless: 'new',
28
+ timeout: 60000, // 增加超时时间
29
+ protocolTimeout: 60000,
30
  args: [
31
  '--no-sandbox',
32
  '--disable-setuid-sandbox',
 
55
  '--disable-hang-monitor',
56
  '--disable-prompt-on-repost',
57
  '--memory-pressure-off',
58
+ '--max_old_space_size=1024', // 增加内存限制
59
+ // 添加更多Hugging Face Space兼容性选项
60
+ '--disable-background-media-suspend',
61
+ '--disable-backgrounding-occluded-windows',
62
+ '--disable-renderer-backgrounding',
63
+ '--disable-field-trial-config',
64
+ '--disable-component-extensions-with-background-pages',
65
+ '--disable-permissions-api',
66
+ '--disable-client-side-phishing-detection',
67
+ '--no-zygote',
68
+ '--disable-web-security',
69
+ '--allow-running-insecure-content',
70
+ '--disable-features=VizDisplayCompositor,AudioServiceOutOfProcess',
71
+ '--disable-software-rasterizer',
72
+ '--disable-canvas-aa',
73
+ '--disable-2d-canvas-clip-aa',
74
+ '--disable-gl-drawing-for-tests',
75
+ '--use-gl=swiftshader'
76
  ]
77
  };
78
 
79
+ // 如果是Hugging Face Space环境,使用单进程模式
80
  if (this.isHuggingFaceSpace) {
81
+ console.log('检测到Hugging Face Space环境,使用单进程模式');
82
  launchOptions.args.push(
83
  '--single-process',
 
 
84
  '--disable-features=site-per-process',
85
+ '--disable-site-isolation-trials',
86
+ '--disable-features=BlockInsecurePrivateNetworkRequests'
87
  );
88
  }
89
 
90
+ // 尝试找到Chrome可执行文件路径
91
+ try {
92
+ const chromePath = process.env.CHROME_BIN ||
93
+ process.env.GOOGLE_CHROME_BIN ||
94
+ '/usr/bin/google-chrome-stable' ||
95
+ '/usr/bin/chromium-browser';
96
+
97
+ if (chromePath && require('fs').existsSync(chromePath)) {
98
+ launchOptions.executablePath = chromePath;
99
+ console.log(`使用Chrome路径: ${chromePath}`);
100
+ }
101
+ } catch (pathError) {
102
+ console.log('未找到自定义Chrome路径,使用默认配置');
103
+ }
104
+
105
  this.browser = await puppeteer.launch(launchOptions);
106
  console.log('✅ Puppeteer浏览器初始化成功');
107
 
 
119
 
120
  if (this.browserLaunchRetries <= this.maxBrowserLaunchRetries) {
121
  console.log(`尝试重新初始化浏览器 (${this.browserLaunchRetries}/${this.maxBrowserLaunchRetries})`);
122
+ await this.delay(3000); // 增加等待时间
123
  return this.initBrowser();
124
  } else {
125
  // 如果Puppeteer完全失败,返回null,使用fallback方法