feat: 添加代理隧道连接失败的重试机制
refactor(crawler): 在各爬虫服务中实现代理错误重试逻辑 feat(uni-app): 新增投标项目查看器的uni-app版本
This commit is contained in:
@@ -46,6 +46,56 @@ async function simulateHumanScrolling(page: puppeteer.Page) {
|
||||
await new Promise((r) => setTimeout(r, 1000));
|
||||
}
|
||||
|
||||
// 检查错误是否为代理隧道连接失败
|
||||
function isTunnelConnectionFailedError(error: unknown): boolean {
|
||||
if (error instanceof Error) {
|
||||
return (
|
||||
error.message.includes('net::ERR_TUNNEL_CONNECTION_FAILED') ||
|
||||
error.message.includes('ERR_TUNNEL_CONNECTION_FAILED')
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 延迟重试函数
|
||||
async function delayRetry(
|
||||
operation: () => Promise<void>,
|
||||
maxRetries: number = 3,
|
||||
delayMs: number = 5000,
|
||||
logger?: Logger,
|
||||
): Promise<void> {
|
||||
let lastError: Error | unknown;
|
||||
|
||||
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
try {
|
||||
await operation();
|
||||
return;
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
|
||||
if (isTunnelConnectionFailedError(error)) {
|
||||
if (attempt < maxRetries) {
|
||||
const delay = delayMs * attempt; // 递增延迟
|
||||
logger?.warn(
|
||||
`代理隧道连接失败,第 ${attempt} 次尝试失败,${delay / 1000} 秒后重试...`,
|
||||
);
|
||||
await new Promise((resolve) => setTimeout(resolve, delay));
|
||||
} else {
|
||||
logger?.error(
|
||||
`代理隧道连接失败,已达到最大重试次数 ${maxRetries} 次`,
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
} else {
|
||||
// 非代理错误,直接抛出
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
export interface CgnpcResult {
|
||||
title: string;
|
||||
publishDate: Date;
|
||||
@@ -96,7 +146,14 @@ export const CgnpcCrawler = {
|
||||
|
||||
try {
|
||||
logger.log(`Navigating to ${this.url}...`);
|
||||
await page.goto(this.url, { waitUntil: 'networkidle2', timeout: 60000 });
|
||||
await delayRetry(
|
||||
async () => {
|
||||
await page.goto(this.url, { waitUntil: 'networkidle2', timeout: 60000 });
|
||||
},
|
||||
3,
|
||||
5000,
|
||||
logger,
|
||||
);
|
||||
|
||||
// 模拟人类行为
|
||||
logger.log('Simulating human mouse movements...');
|
||||
|
||||
Reference in New Issue
Block a user