@things-factory/integration-base 9.1.16 → 9.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -33,15 +33,9 @@ async function executeHeadlessRequestWithRecovery(connectionName, options, conte
|
|
|
33
33
|
}
|
|
34
34
|
const { endpoint, params: connectionParams, acquireSessionPage, releasePage, reAuthenticateSession, validateSession } = connection;
|
|
35
35
|
const loginPagePath = connectionParams?.loginPagePath || '/login';
|
|
36
|
-
// Connection 설정 로깅 (발전소별 차이 확인용)
|
|
37
|
-
logger.info(`[Connection Config] Endpoint: ${endpoint}`);
|
|
38
|
-
if (connectionParams?.username) {
|
|
39
|
-
logger.info(`[Connection Config] Username: ${connectionParams.username}`);
|
|
40
|
-
}
|
|
41
36
|
let page = null;
|
|
42
37
|
let pageResource = null; // 리소스 추적 객체
|
|
43
38
|
let lastError = null;
|
|
44
|
-
let consoleListener = null; // 브라우저 콘솔 리스너
|
|
45
39
|
// 재시도 로직
|
|
46
40
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
47
41
|
try {
|
|
@@ -56,15 +50,6 @@ async function executeHeadlessRequestWithRecovery(connectionName, options, conte
|
|
|
56
50
|
page = sessionResult;
|
|
57
51
|
pageResource = { page, requiresManualRelease: false };
|
|
58
52
|
}
|
|
59
|
-
// 초기 페이지 상태 로깅
|
|
60
|
-
const initialUrl = page.url();
|
|
61
|
-
const initialCookies = await page.cookies();
|
|
62
|
-
console.log('init page');
|
|
63
|
-
logger.info(`[Initial Page] URL: ${initialUrl}, Cookies: ${initialCookies.length}`);
|
|
64
|
-
// 로그인 페이지에 있는지 확인
|
|
65
|
-
if (initialUrl.includes('/login') || initialUrl.includes('/signin') || initialCookies.length === 0) {
|
|
66
|
-
logger.error(`[Login Failed] Still on login page or no cookies! URL: ${initialUrl}`);
|
|
67
|
-
}
|
|
68
53
|
// 페이지가 올바른 도메인에 있는지 확인
|
|
69
54
|
const currentUrl = page.url();
|
|
70
55
|
const targetDomain = new URL(endpoint).origin;
|
|
@@ -73,9 +58,7 @@ async function executeHeadlessRequestWithRecovery(connectionName, options, conte
|
|
|
73
58
|
await page.goto(targetDomain, { waitUntil: 'networkidle2' });
|
|
74
59
|
}
|
|
75
60
|
// 세션 검증은 2번째 시도부터만 수행 (첫 번째는 새 페이지이므로 불필요)
|
|
76
|
-
// 또는 이전 시도에서 세션 관련 에러가 발생한 경우에만 수행
|
|
77
61
|
if (attempt > 0) {
|
|
78
|
-
logger.debug(`Validating session for connection '${connectionName}' (attempt: ${attempt + 1})`);
|
|
79
62
|
const isSessionValid = await validateSession(page);
|
|
80
63
|
if (!isSessionValid) {
|
|
81
64
|
logger.warn(`Session invalid for connection '${connectionName}', attempting full connection reset (attempt: ${attempt + 1})`);
|
|
@@ -89,14 +72,7 @@ async function executeHeadlessRequestWithRecovery(connectionName, options, conte
|
|
|
89
72
|
.catch(() => logger.warn('Page readyState timeout'));
|
|
90
73
|
// JavaScript 초기화를 위한 추가 대기 (핵심 해결책)
|
|
91
74
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
92
|
-
|
|
93
|
-
const resetUrl = page.url();
|
|
94
|
-
const cookies = await page.cookies();
|
|
95
|
-
logger.info(`[Reset Success] URL: ${resetUrl}, Cookies: ${cookies.length}`);
|
|
96
|
-
// about:blank 체크
|
|
97
|
-
if (resetUrl === 'about:blank') {
|
|
98
|
-
throw new Error('Page stuck on about:blank after reset');
|
|
99
|
-
}
|
|
75
|
+
logger.info(`Full connection reset successful for connection '${connectionName}'`);
|
|
100
76
|
}
|
|
101
77
|
catch (resetError) {
|
|
102
78
|
logger.error(`Connection reset failed for connection '${connectionName}':`, resetError);
|
|
@@ -144,17 +120,6 @@ async function executeHeadlessRequestWithRecovery(connectionName, options, conte
|
|
|
144
120
|
requestOptions.body = typeof requestBody === 'string' ? requestBody : JSON.stringify(requestBody);
|
|
145
121
|
}
|
|
146
122
|
}
|
|
147
|
-
// 요청 직전 상태 로깅
|
|
148
|
-
logger.info(`[Request] ${method} ${url.toString()}`);
|
|
149
|
-
logger.debug(`[Request] Page: ${page.url()}, Closed: ${page.isClosed()}`);
|
|
150
|
-
// 브라우저 콘솔 메시지 캡처 (디버깅용)
|
|
151
|
-
consoleListener = msg => {
|
|
152
|
-
const text = msg.text();
|
|
153
|
-
if (text.includes('[Browser Context]')) {
|
|
154
|
-
logger.info(`[Browser] ${text}`);
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
page.on('console', consoleListener);
|
|
158
123
|
const response = await page.evaluate(async (urlString, opts, loginPagePath) => {
|
|
159
124
|
try {
|
|
160
125
|
// 페이지 상태 검증 (about:blank 또는 로딩 중인 페이지에서 fetch 방지)
|
|
@@ -171,10 +136,6 @@ async function executeHeadlessRequestWithRecovery(connectionName, options, conte
|
|
|
171
136
|
networkError: true
|
|
172
137
|
};
|
|
173
138
|
}
|
|
174
|
-
// 🔍 fetch 직전 디버깅 정보
|
|
175
|
-
console.log('[Browser Context] About to fetch:', urlString);
|
|
176
|
-
console.log('[Browser Context] Current location:', pageLocation);
|
|
177
|
-
console.log('[Browser Context] Cookies:', document.cookie);
|
|
178
139
|
const response = await fetch(urlString, { ...opts, redirect: 'manual' });
|
|
179
140
|
const result = {
|
|
180
141
|
ok: response.ok,
|
|
@@ -248,35 +209,19 @@ async function executeHeadlessRequestWithRecovery(connectionName, options, conte
|
|
|
248
209
|
}
|
|
249
210
|
catch (fetchError) {
|
|
250
211
|
// 네트워크 레벨 에러 처리
|
|
251
|
-
console.error('[Browser Context] Fetch failed:', fetchError.message);
|
|
252
|
-
console.error('[Browser Context] Fetch error name:', fetchError.name);
|
|
253
|
-
console.error('[Browser Context] Fetch error stack:', fetchError.stack);
|
|
254
212
|
return {
|
|
255
213
|
ok: false,
|
|
256
214
|
status: 0,
|
|
257
215
|
statusText: 'Network Error',
|
|
258
216
|
headers: {},
|
|
259
217
|
error: fetchError.message || 'Failed to fetch',
|
|
260
|
-
errorName: fetchError.name,
|
|
261
|
-
errorStack: fetchError.stack,
|
|
262
218
|
data: null,
|
|
263
219
|
networkError: true
|
|
264
220
|
};
|
|
265
221
|
}
|
|
266
222
|
}, url.toString(), requestOptions, loginPagePath);
|
|
267
|
-
// 브라우저 콘솔 리스너 제거
|
|
268
|
-
if (consoleListener && page && !page.isClosed()) {
|
|
269
|
-
try {
|
|
270
|
-
page.off('console', consoleListener);
|
|
271
|
-
consoleListener = null;
|
|
272
|
-
}
|
|
273
|
-
catch (e) {
|
|
274
|
-
// 리스너 제거 실패는 무시
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
223
|
// 네트워크 에러 체크 (fetch 자체가 실패한 경우)
|
|
278
224
|
if (response.networkError) {
|
|
279
|
-
logger.info(`[networkError ERROR] response: ${JSON.stringify(response)}`);
|
|
280
225
|
if (attempt < maxRetries) {
|
|
281
226
|
logger.warn(`Network error detected: ${response.error}, retrying... (${attempt + 1}/${maxRetries + 1})`);
|
|
282
227
|
await safeReleasePageResource(pageResource, releasePage, logger);
|
|
@@ -370,15 +315,6 @@ async function executeHeadlessRequestWithRecovery(connectionName, options, conte
|
|
|
370
315
|
throw new Error(response.error);
|
|
371
316
|
}
|
|
372
317
|
// 성공 시 페이지 릴리즈 후 결과 반환
|
|
373
|
-
if (consoleListener && page && !page.isClosed()) {
|
|
374
|
-
try {
|
|
375
|
-
page.off('console', consoleListener);
|
|
376
|
-
consoleListener = null;
|
|
377
|
-
}
|
|
378
|
-
catch (e) {
|
|
379
|
-
// 리스너 제거 실패는 무시
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
318
|
const result = {
|
|
383
319
|
data: response.data,
|
|
384
320
|
status: response.status,
|
|
@@ -390,15 +326,6 @@ async function executeHeadlessRequestWithRecovery(connectionName, options, conte
|
|
|
390
326
|
catch (error) {
|
|
391
327
|
lastError = error;
|
|
392
328
|
logger.error(`Headless request attempt ${attempt + 1} failed:`, error);
|
|
393
|
-
if (consoleListener && page && !page.isClosed()) {
|
|
394
|
-
try {
|
|
395
|
-
page.off('console', consoleListener);
|
|
396
|
-
consoleListener = null;
|
|
397
|
-
}
|
|
398
|
-
catch (e) {
|
|
399
|
-
// 리스너 제거 실패는 무시
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
329
|
await safeReleasePageResource(pageResource, releasePage, logger);
|
|
403
330
|
page = null;
|
|
404
331
|
pageResource = null;
|
|
@@ -527,9 +454,15 @@ async function performFullConnectionReset(domain, connectionName, logger, pageRe
|
|
|
527
454
|
// 기존 연결 완전 해제
|
|
528
455
|
await connector.disconnect(connectionEntity);
|
|
529
456
|
logger.info(`Connection '${connectionName}' disconnected for reset`);
|
|
457
|
+
// 브라우저 완전 종료 및 리소스 정리 대기 (중요!)
|
|
458
|
+
// 수동 재연결 시에는 자연스럽게 시간이 지나가지만,
|
|
459
|
+
// 코드에서는 즉시 연결을 시도하므로 명시적 대기 필요
|
|
460
|
+
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
530
461
|
// 새로운 연결 생성 (완전한 초기화)
|
|
531
462
|
await connector.connect(connectionEntity);
|
|
532
463
|
logger.info(`Connection '${connectionName}' reconnected after reset`);
|
|
464
|
+
// 새 브라우저 인스턴스 초기화 대기
|
|
465
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
533
466
|
// 새로운 connection 인스턴스 가져오기
|
|
534
467
|
const newConnection = await connection_manager_1.ConnectionManager.getConnectionInstanceByName(domain, connectionName);
|
|
535
468
|
if (!newConnection || !newConnection.acquireSessionPage) {
|
|
@@ -546,6 +479,8 @@ async function performFullConnectionReset(domain, connectionName, logger, pageRe
|
|
|
546
479
|
newPage = newSessionResult;
|
|
547
480
|
newPageResource = { page: newPage, requiresManualRelease: false };
|
|
548
481
|
}
|
|
482
|
+
// 새 페이지 안정화 대기 (핵심!)
|
|
483
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
549
484
|
return { pageResource: newPageResource, page: newPage };
|
|
550
485
|
}
|
|
551
486
|
//# sourceMappingURL=headless-request-with-recovery.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"headless-request-with-recovery.js","sourceRoot":"","sources":["../../../../server/engine/task/utils/headless-request-with-recovery.ts"],"names":[],"mappings":";;AAiBA,gFAqfC;AAsCD,0DAuDC;AAnmBD,iDAA8C;AAC9C,iEAA4D;AAa5D;;GAEG;AACI,KAAK,UAAU,kCAAkC,CACtD,cAAsB,EACtB,OAA+B,EAC/B,OAAgD;IAEhD,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAA;IACxC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,UAAU,GAAG,CAAC,EAAE,QAAQ,EAAE,WAAW,EAAE,GAAG,OAAO,CAAA;IAExG,iCAAiC;IACjC,MAAM,WAAW,GAAG,CAAC,eAAe,cAAc,EAAE,EAAE,SAAS,IAAI,EAAE,CAAC,CAAA;IAEtE,oBAAoB;IACpB,IAAI,IAAI,EAAE,CAAC;QACT,MAAM,WAAW,GAAG,CAAC,MAAM,EAAE,cAAc,EAAE,SAAS,EAAE,QAAQ,EAAE,cAAc,CAAC,CAAA;QACjF,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YACxB,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;gBACd,WAAW,CAAC,IAAI,CAAC,GAAG,GAAG,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;YAC1C,CAAC;QACH,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,MAAM,CAAC,IAAI,CAAC,aAAa,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IAElD,+BAA+B;IAC/B,IAAI,WAAW,GAAG,IAAI,CAAA;IACtB,IAAI,QAAQ,IAAI,IAAI,EAAE,CAAC;QACrB,WAAW,GAAG,IAAA,cAAM,EAAC,QAAQ,EAAE,IAAI,CAAC,CAAA;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,GAAG,MAAM,aAAa,CAAC,kBAAkB,EAAE,CAAA;IACjE,IAAI,eAAoB,CAAA;IACxB,IAAI,OAAY,CAAA;IAEhB,IAAI,gBAAgB,IAAI,OAAO,gBAAgB,KAAK,QAAQ,IAAI,gBAAgB,CAAC,IAAI,EAAE,CAAC;QACtF,eAAe,GAAG,gBAAgB,CAAA;QAClC,OAAO,GAAG,gBAAgB,CAAC,IAAI,CAAA;IACjC,CAAC;SAAM,CAAC;QACN,OAAO,GAAG,gBAAgB,CAAA;QAC1B,eAAe,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,qBAAqB,EAAE,KAAK,EAAE,CAAA;IACnE,CAAC;IAED,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,IAAI,EAAE,OAAO,EAAE,CAAA;AACzD,CAAC","sourcesContent":["import { access } from '@things-factory/utils'\nimport { ConnectionManager } from '../../connection-manager'\n\nexport interface HeadlessRequestOptions {\n method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'\n path: string\n headers?: Record<string, string>\n body?: any\n queryParams?: Record<string, any>\n maxRetries?: number\n accessor?: string // POST 요청에서 data 접근용\n contentType?: string // POST 요청에서 content-type 지정용\n}\n\n/**\n * 세션 회복 기능을 포함한 headless HTTP 요청 함수\n */\nexport async function executeHeadlessRequestWithRecovery(\n connectionName: string,\n options: HeadlessRequestOptions,\n context: { logger: any; data: any; domain: any }\n): Promise<any> {\n const { logger, data, domain } = context\n const { method, path, headers = {}, body, queryParams, maxRetries = 2, accessor, contentType } = options\n\n // 요청 컨텍스트 로깅 (발전소 식별을 위한 데이터 포함)\n const contextInfo = [`Connection: ${connectionName}`, `Path: ${path}`]\n\n // 발전소 식별자 추출 (디버깅용)\n if (data) {\n const identifiers = ['ICUS', 'SEL_METER_ID', 'plantId', 'siteId', 'powerPlantId']\n identifiers.forEach(key => {\n if (data[key]) {\n contextInfo.push(`${key}: ${data[key]}`)\n }\n })\n }\n\n logger.info(`[Context] ${contextInfo.join(', ')}`)\n\n // accessor가 있으면 data에서 body 추출\n let requestBody = body\n if (accessor && data) {\n requestBody = access(accessor, data)\n }\n\n const connection = await ConnectionManager.getConnectionInstanceByName(domain, connectionName)\n if (!connection) {\n throw new Error(`Connection '${connectionName}' is not established.`)\n }\n\n const {\n endpoint,\n params: connectionParams,\n acquireSessionPage,\n releasePage,\n reAuthenticateSession,\n validateSession\n } = connection\n const loginPagePath = connectionParams?.loginPagePath || '/login'\n\n // Connection 설정 로깅 (발전소별 차이 확인용)\n logger.info(`[Connection Config] Endpoint: ${endpoint}`)\n if (connectionParams?.username) {\n logger.info(`[Connection Config] Username: ${connectionParams.username}`)\n }\n\n let page = null\n let pageResource = null // 리소스 추적 객체\n let lastError = null\n let consoleListener = null // 브라우저 콘솔 리스너\n\n // 재시도 로직\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\n try {\n // 페이지 획득\n const sessionResult = await acquireSessionPage()\n\n // reAuthenticateSession의 반환 형태 확인\n if (sessionResult && typeof sessionResult === 'object' && sessionResult.page) {\n pageResource = sessionResult // {page, browser, requiresManualRelease}\n page = sessionResult.page\n } else {\n page = sessionResult\n pageResource = { page, requiresManualRelease: false }\n }\n\n // 초기 페이지 상태 로깅\n const initialUrl = page.url()\n const initialCookies = await page.cookies()\n console.log('init page')\n logger.info(`[Initial Page] URL: ${initialUrl}, Cookies: ${initialCookies.length}`)\n\n // 로그인 페이지에 있는지 확인\n if (initialUrl.includes('/login') || initialUrl.includes('/signin') || initialCookies.length === 0) {\n logger.error(`[Login Failed] Still on login page or no cookies! URL: ${initialUrl}`)\n }\n\n // 페이지가 올바른 도메인에 있는지 확인\n const currentUrl = page.url()\n const targetDomain = new URL(endpoint).origin\n if (!currentUrl.startsWith(targetDomain)) {\n logger.info(`Navigating to target domain: ${targetDomain}`)\n await page.goto(targetDomain, { waitUntil: 'networkidle2' })\n }\n\n // 세션 검증은 2번째 시도부터만 수행 (첫 번째는 새 페이지이므로 불필요)\n // 또는 이전 시도에서 세션 관련 에러가 발생한 경우에만 수행\n if (attempt > 0) {\n logger.debug(`Validating session for connection '${connectionName}' (attempt: ${attempt + 1})`)\n const isSessionValid = await validateSession(page)\n if (!isSessionValid) {\n logger.warn(\n `Session invalid for connection '${connectionName}', attempting full connection reset (attempt: ${attempt + 1})`\n )\n\n try {\n const resetResult = await performFullConnectionReset(\n domain,\n connectionName,\n logger,\n pageResource,\n releasePage\n )\n pageResource = resetResult.pageResource\n page = resetResult.page\n\n // 재연결 후 페이지 완전 준비 대기\n await page\n .waitForFunction(() => document.readyState === 'complete', { timeout: 10000 })\n .catch(() => logger.warn('Page readyState timeout'))\n\n // JavaScript 초기화를 위한 추가 대기 (핵심 해결책)\n await new Promise(resolve => setTimeout(resolve, 2000))\n\n // 페이지 상태 확인\n const resetUrl = page.url()\n const cookies = await page.cookies()\n\n logger.info(`[Reset Success] URL: ${resetUrl}, Cookies: ${cookies.length}`)\n\n // about:blank 체크\n if (resetUrl === 'about:blank') {\n throw new Error('Page stuck on about:blank after reset')\n }\n } catch (resetError) {\n logger.error(`Connection reset failed for connection '${connectionName}':`, resetError)\n // 재설정 실패 시 이번 시도는 실패로 처리하고 다음 시도로 넘어감\n if (attempt === maxRetries) {\n throw new Error(`Connection reset failed after ${maxRetries + 1} attempts: ${resetError.message}`)\n }\n continue\n }\n }\n }\n\n // URL 구성\n const url = new URL(path, endpoint)\n if (queryParams && typeof queryParams === 'object') {\n Object.keys(queryParams).forEach(key => {\n if (queryParams[key] !== null && queryParams[key] !== undefined) {\n url.searchParams.append(key, String(queryParams[key]))\n }\n })\n }\n\n // 요청 옵션 구성\n const requestOptions: any = {\n method,\n headers: {\n ...headers\n },\n credentials: 'include'\n }\n\n // Content-Type과 body 처리\n if (requestBody && ['POST', 'PUT', 'PATCH'].includes(method)) {\n if (contentType) {\n requestOptions.headers['content-type'] = contentType\n switch (contentType) {\n case 'text/plain':\n requestOptions.body = JSON.stringify(requestBody)\n break\n case 'application/json':\n default:\n requestOptions.body = typeof requestBody === 'string' ? requestBody : JSON.stringify(requestBody)\n break\n }\n } else {\n requestOptions.headers['Content-Type'] = 'application/json'\n requestOptions.body = typeof requestBody === 'string' ? requestBody : JSON.stringify(requestBody)\n }\n }\n\n // 요청 직전 상태 로깅\n logger.info(`[Request] ${method} ${url.toString()}`)\n logger.debug(`[Request] Page: ${page.url()}, Closed: ${page.isClosed()}`)\n\n // 브라우저 콘솔 메시지 캡처 (디버깅용)\n consoleListener = msg => {\n const text = msg.text()\n if (text.includes('[Browser Context]')) {\n logger.info(`[Browser] ${text}`)\n }\n }\n page.on('console', consoleListener)\n\n const response = await page.evaluate(\n async (urlString, opts, loginPagePath) => {\n try {\n // 페이지 상태 검증 (about:blank 또는 로딩 중인 페이지에서 fetch 방지)\n const pageLocation = window.location.href\n const pageReadyState = document.readyState\n\n if (pageLocation === 'about:blank' || pageReadyState !== 'complete') {\n return {\n ok: false,\n status: 0,\n statusText: 'Page Not Ready',\n headers: {},\n error: `Page state invalid - location: ${pageLocation}, readyState: ${pageReadyState}`,\n data: null,\n networkError: true\n }\n }\n\n // 🔍 fetch 직전 디버깅 정보\n console.log('[Browser Context] About to fetch:', urlString)\n console.log('[Browser Context] Current location:', pageLocation)\n console.log('[Browser Context] Cookies:', document.cookie)\n\n const response = await fetch(urlString, { ...opts, redirect: 'manual' })\n\n const result = {\n ok: response.ok,\n status: response.status,\n statusText: response.statusText,\n headers: Object.fromEntries(response.headers.entries())\n }\n\n // 302 리디렉션 감지 - 로그인 페이지로의 리디렉션 체크\n if ([301, 302, 307, 308].includes(response.status)) {\n const location = response.headers.get('location') || ''\n // connection의 loginPagePath와 일반적인 로그인 경로들을 체크\n if (\n location.includes(loginPagePath) ||\n location.includes('/login') ||\n location.includes('/signin') ||\n location.includes('/auth')\n ) {\n return {\n ...result,\n error: `Redirected to login page: ${location}`,\n redirectedToLogin: true,\n location,\n data: null\n }\n }\n // 다른 리디렉션은 follow\n const redirectResponse = await fetch(urlString, opts)\n return {\n ok: redirectResponse.ok,\n status: redirectResponse.status,\n statusText: redirectResponse.statusText,\n headers: Object.fromEntries(redirectResponse.headers.entries()),\n data: redirectResponse.ok\n ? await redirectResponse\n .json()\n .catch(() => redirectResponse.text())\n .catch(() => null)\n : null,\n error: redirectResponse.ok ? null : `HTTP ${redirectResponse.status}: ${redirectResponse.statusText}`\n }\n }\n\n // status가 0인 경우는 네트워크 레벨 에러 (CORS, 연결 실패 등)\n if (response.status === 0) {\n return {\n ok: false,\n status: 0,\n statusText: response.statusText || 'Network Error',\n headers: {},\n error: 'HTTP 0 - Network connection failed (possible CORS, SSL, or connectivity issue)',\n data: null,\n networkError: true\n }\n }\n\n if (!response.ok) {\n return {\n ...result,\n error: `HTTP ${response.status}: ${response.statusText}`,\n data: null\n }\n }\n\n const contentType = response.headers.get('content-type') || ''\n\n if (contentType.includes('application/json')) {\n result['data'] = await response.json()\n } else if (contentType.includes('text/')) {\n result['data'] = await response.text()\n } else {\n // 응답이 없는 경우 (204 No Content 등)\n result['data'] = null\n }\n\n return result\n } catch (fetchError) {\n // 네트워크 레벨 에러 처리\n console.error('[Browser Context] Fetch failed:', fetchError.message)\n console.error('[Browser Context] Fetch error name:', fetchError.name)\n console.error('[Browser Context] Fetch error stack:', fetchError.stack)\n\n return {\n ok: false,\n status: 0,\n statusText: 'Network Error',\n headers: {},\n error: fetchError.message || 'Failed to fetch',\n errorName: fetchError.name,\n errorStack: fetchError.stack,\n data: null,\n networkError: true\n }\n }\n },\n url.toString(),\n requestOptions,\n loginPagePath\n )\n\n // 브라우저 콘솔 리스너 제거\n if (consoleListener && page && !page.isClosed()) {\n try {\n page.off('console', consoleListener)\n consoleListener = null\n } catch (e) {\n // 리스너 제거 실패는 무시\n }\n }\n\n // 네트워크 에러 체크 (fetch 자체가 실패한 경우)\n if (response.networkError) {\n logger.info(`[networkError ERROR] response: ${JSON.stringify(response)}`)\n\n if (attempt < maxRetries) {\n logger.warn(`Network error detected: ${response.error}, retrying... (${attempt + 1}/${maxRetries + 1})`)\n await safeReleasePageResource(pageResource, releasePage, logger)\n page = null\n pageResource = null\n continue\n } else {\n await safeReleasePageResource(pageResource, releasePage, logger)\n throw new Error(`Network error after ${maxRetries + 1} attempts: ${response.error}`)\n }\n }\n\n // 로그인 리디렉션 감지 처리\n if (response.redirectedToLogin) {\n if (attempt < maxRetries) {\n logger.warn(\n `Login redirect detected: ${response.location}, performing full connection reset... (${attempt + 1}/${maxRetries + 1})`\n )\n\n // CRITICAL: 커넥션 재연결 방식으로 완전한 초기화\n try {\n const resetResult = await performFullConnectionReset(\n domain,\n connectionName,\n logger,\n pageResource,\n releasePage\n )\n pageResource = resetResult.pageResource\n page = resetResult.page\n\n // 재연결 후 페이지 완전 준비 대기\n await page\n .waitForFunction(() => document.readyState === 'complete', { timeout: 10000 })\n .catch(() => logger.warn('Page readyState timeout'))\n\n // JavaScript 초기화를 위한 추가 대기\n await new Promise(resolve => setTimeout(resolve, 2000))\n\n // 페이지 상태 확인\n const resetUrl = page.url()\n const cookies = await page.cookies()\n\n logger.info(`[Reset Success] URL: ${resetUrl}, Cookies: ${cookies.length}`)\n\n // about:blank 체크\n if (resetUrl === 'about:blank') {\n throw new Error('Page stuck on about:blank after reset')\n }\n } catch (resetError) {\n logger.error(`Connection reset failed after login redirect for connection '${connectionName}':`, resetError)\n if (attempt === maxRetries) {\n throw new Error(`Connection reset failed after login redirect: ${resetError.message}`)\n }\n continue\n }\n continue\n } else {\n await safeReleasePageResource(pageResource, releasePage, logger)\n throw new Error(`Login redirect after ${maxRetries + 1} attempts: ${response.error}`)\n }\n }\n\n // 세션 타임아웃 관련 에러 체크\n if (!response.ok && isSessionTimeoutError(response.status)) {\n if (attempt < maxRetries) {\n logger.warn(\n `Session timeout detected (${response.status}), performing full connection reset... (${attempt + 1}/${maxRetries + 1})`\n )\n\n // CRITICAL: 커넥션 재연결 방식으로 완전한 초기화\n try {\n const resetResult = await performFullConnectionReset(\n domain,\n connectionName,\n logger,\n pageResource,\n releasePage\n )\n pageResource = resetResult.pageResource\n page = resetResult.page\n\n // 재연결 후 페이지 완전 준비 대기\n await page\n .waitForFunction(() => document.readyState === 'complete', { timeout: 10000 })\n .catch(() => logger.warn('Page readyState timeout'))\n\n // JavaScript 초기화를 위한 추가 대기\n await new Promise(resolve => setTimeout(resolve, 2000))\n\n // 페이지 상태 확인\n const resetUrl = page.url()\n const cookies = await page.cookies()\n\n logger.info(`[Reset Success] URL: ${resetUrl}, Cookies: ${cookies.length}`)\n\n // about:blank 체크\n if (resetUrl === 'about:blank') {\n throw new Error('Page stuck on about:blank after reset')\n }\n } catch (resetError) {\n logger.error(\n `Connection reset failed after session timeout for connection '${connectionName}':`,\n resetError\n )\n if (attempt === maxRetries) {\n throw new Error(`Connection reset failed after session timeout: ${resetError.message}`)\n }\n continue\n }\n continue\n } else {\n await safeReleasePageResource(pageResource, releasePage, logger)\n throw new Error(`Session timeout after ${maxRetries + 1} attempts: ${response.error}`)\n }\n }\n\n // 기타 HTTP 에러\n if (!response.ok) {\n throw new Error(response.error)\n }\n\n // 성공 시 페이지 릴리즈 후 결과 반환\n if (consoleListener && page && !page.isClosed()) {\n try {\n page.off('console', consoleListener)\n consoleListener = null\n } catch (e) {\n // 리스너 제거 실패는 무시\n }\n }\n\n const result = {\n data: response.data,\n status: response.status,\n headers: response.headers\n }\n\n await safeReleasePageResource(pageResource, releasePage, logger)\n\n return result\n } catch (error) {\n lastError = error\n logger.error(`Headless request attempt ${attempt + 1} failed:`, error)\n\n if (consoleListener && page && !page.isClosed()) {\n try {\n page.off('console', consoleListener)\n consoleListener = null\n } catch (e) {\n // 리스너 제거 실패는 무시\n }\n }\n\n await safeReleasePageResource(pageResource, releasePage, logger)\n page = null\n pageResource = null\n\n // 세션 관련 에러가 아니거나 마지막 재시도면 에러 발생\n if (!isRecoverableError(error) || attempt === maxRetries) {\n throw error\n }\n\n logger.info(`Retrying request... (${attempt + 2}/${maxRetries + 1})`)\n }\n }\n\n // 모든 재시도가 실패한 경우 - 혹시 남은 리소스가 있으면 정리\n if (pageResource) {\n await safeReleasePageResource(pageResource, releasePage, logger)\n }\n throw lastError || new Error('Request failed after all retry attempts')\n}\n\n/**\n * 세션 타임아웃 관련 HTTP 상태 코드인지 확인\n */\nfunction isSessionTimeoutError(statusCode: number): boolean {\n return [401, 403].includes(statusCode)\n}\n\n/**\n * 복구 가능한 에러인지 확인\n */\nfunction isRecoverableError(error: any): boolean {\n const errorMessage = error.message?.toLowerCase() || ''\n\n // 복구 가능한 에러 키워드 (네트워크, 세션/인증 관련)\n const recoverableErrorKeywords = [\n 'failed to fetch',\n 'network error',\n 'connection failed',\n 'connection reset',\n 'timeout',\n 'timed out',\n 'unauthorized',\n 'forbidden',\n 'session',\n 'authentication',\n 'login',\n 'expired',\n 'redirected to login' // 로그인 리디렉션 추가\n ]\n\n return recoverableErrorKeywords.some(keyword => errorMessage.includes(keyword))\n}\n\n/**\n * Safely release page resource with comprehensive error handling\n */\nexport async function safeReleasePageResource(pageResource: any, releasePage: Function, logger: any): Promise<void> {\n if (!pageResource) {\n return\n }\n\n try {\n // Handle different pageResource formats\n if (pageResource.page) {\n // This is a complex resource object {page, browser, requiresManualRelease}\n const { page, browser, requiresManualRelease } = pageResource\n\n if (requiresManualRelease && browser) {\n // Manual release required - close page first, then release browser\n try {\n if (page && !page.isClosed()) {\n await page.close()\n logger.info('Page closed during manual resource release')\n }\n } catch (closeError) {\n logger.error('Failed to close page during manual release:', closeError)\n }\n\n // Release browser back to pool\n try {\n const { getHeadlessPool } = require('../../resource-pool/headless-pool')\n const pool = getHeadlessPool()\n await pool.release(browser)\n logger.info('Browser manually released to pool')\n } catch (releaseError) {\n logger.error('Failed to manually release browser to pool:', releaseError)\n }\n } else {\n // Standard release through releasePage\n await releasePage(page)\n logger.info('Page released through standard releasePage method')\n }\n } else {\n // Simple page object - use standard release\n await releasePage(pageResource)\n logger.info('Simple page resource released')\n }\n } catch (error) {\n logger.error('Critical error during page resource release:', error)\n\n // Last resort: try to force close the page if it exists\n try {\n const page = pageResource.page || pageResource\n if (page && !page.isClosed()) {\n await page.close()\n logger.warn('Force closed page as last resort')\n }\n } catch (forceCloseError) {\n logger.error('Failed to force close page:', forceCloseError)\n }\n }\n}\n\n/**\n * 커넥션을 완전 재초기화하는 함수\n * 세션 만료 시 커넥션 재연결과 동일한 완전한 초기화를 수행\n */\nasync function performFullConnectionReset(\n domain: any,\n connectionName: string,\n logger: any,\n pageResource: any,\n releasePage: Function\n): Promise<{ pageResource: any; page: any }> {\n // 기존 페이지 리소스 해제\n await safeReleasePageResource(pageResource, releasePage, logger)\n\n // 커넥션 엔티티 가져오기\n const connectionEntity = await ConnectionManager.getConnectionEntityByName(domain, connectionName)\n if (!connectionEntity) {\n throw new Error('Connection entity not found for reset')\n }\n\n // Connector를 직접 사용하여 연결 재설정\n const { type, edge } = connectionEntity\n const ProxyConnector = require('../../connector/proxy-connector').ProxyConnector\n const connector = edge ? ProxyConnector.instance : ConnectionManager.getConnector(type)\n\n if (!connector) {\n throw new Error(`Connector not found for type: ${type}`)\n }\n\n // 기존 연결 완전 해제\n await connector.disconnect(connectionEntity)\n logger.info(`Connection '${connectionName}' disconnected for reset`)\n\n // 새로운 연결 생성 (완전한 초기화)\n await connector.connect(connectionEntity)\n logger.info(`Connection '${connectionName}' reconnected after reset`)\n\n // 새로운 connection 인스턴스 가져오기\n const newConnection = await ConnectionManager.getConnectionInstanceByName(domain, connectionName)\n if (!newConnection || !newConnection.acquireSessionPage) {\n throw new Error('Failed to acquire session page after connection reset')\n }\n\n const newSessionResult = await newConnection.acquireSessionPage()\n let newPageResource: any\n let newPage: any\n\n if (newSessionResult && typeof newSessionResult === 'object' && newSessionResult.page) {\n newPageResource = newSessionResult\n newPage = newSessionResult.page\n } else {\n newPage = newSessionResult\n newPageResource = { page: newPage, requiresManualRelease: false }\n }\n\n return { pageResource: newPageResource, page: newPage }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"headless-request-with-recovery.js","sourceRoot":"","sources":["../../../../server/engine/task/utils/headless-request-with-recovery.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AAC,CAAA;IAEvD,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,IAAI,EAAE,OAAO,EAAE,CAAA;AACzD,CAAC","sourcesContent":["import { access } from '@things-factory/utils'\nimport { ConnectionManager } from '../../connection-manager'\n\nexport interface HeadlessRequestOptions {\n method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'\n path: string\n headers?: Record<string, string>\n body?: any\n queryParams?: Record<string, any>\n maxRetries?: number\n accessor?: string // POST 요청에서 data 접근용\n contentType?: string // POST 요청에서 content-type 지정용\n}\n\n/**\n * 세션 회복 기능을 포함한 headless HTTP 요청 함수\n */\nexport async function executeHeadlessRequestWithRecovery(\n connectionName: string,\n options: HeadlessRequestOptions,\n context: { logger: any; data: any; domain: any }\n): Promise<any> {\n const { logger, data, domain } = context\n const { method, path, headers = {}, body, queryParams, maxRetries = 2, accessor, contentType } = options\n\n // 요청 컨텍스트 로깅 (발전소 식별을 위한 데이터 포함)\n const contextInfo = [`Connection: ${connectionName}`, `Path: ${path}`]\n\n // 발전소 식별자 추출 (디버깅용)\n if (data) {\n const identifiers = ['ICUS', 'SEL_METER_ID', 'plantId', 'siteId', 'powerPlantId']\n identifiers.forEach(key => {\n if (data[key]) {\n contextInfo.push(`${key}: ${data[key]}`)\n }\n })\n }\n\n logger.info(`[Context] ${contextInfo.join(', ')}`)\n\n // accessor가 있으면 data에서 body 추출\n let requestBody = body\n if (accessor && data) {\n requestBody = access(accessor, data)\n }\n\n const connection = await ConnectionManager.getConnectionInstanceByName(domain, connectionName)\n if (!connection) {\n throw new Error(`Connection '${connectionName}' is not established.`)\n }\n\n const {\n endpoint,\n params: connectionParams,\n acquireSessionPage,\n releasePage,\n reAuthenticateSession,\n validateSession\n } = connection\n const loginPagePath = connectionParams?.loginPagePath || '/login'\n\n let page = null\n let pageResource = null // 리소스 추적 객체\n let lastError = null\n\n // 재시도 로직\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\n try {\n // 페이지 획득\n const sessionResult = await acquireSessionPage()\n\n // reAuthenticateSession의 반환 형태 확인\n if (sessionResult && typeof sessionResult === 'object' && sessionResult.page) {\n pageResource = sessionResult // {page, browser, requiresManualRelease}\n page = sessionResult.page\n } else {\n page = sessionResult\n pageResource = { page, requiresManualRelease: false }\n }\n\n // 페이지가 올바른 도메인에 있는지 확인\n const currentUrl = page.url()\n const targetDomain = new URL(endpoint).origin\n if (!currentUrl.startsWith(targetDomain)) {\n logger.info(`Navigating to target domain: ${targetDomain}`)\n await page.goto(targetDomain, { waitUntil: 'networkidle2' })\n }\n\n // 세션 검증은 2번째 시도부터만 수행 (첫 번째는 새 페이지이므로 불필요)\n if (attempt > 0) {\n const isSessionValid = await validateSession(page)\n\n if (!isSessionValid) {\n logger.warn(\n `Session invalid for connection '${connectionName}', attempting full connection reset (attempt: ${attempt + 1})`\n )\n\n try {\n const resetResult = await performFullConnectionReset(\n domain,\n connectionName,\n logger,\n pageResource,\n releasePage\n )\n pageResource = resetResult.pageResource\n page = resetResult.page\n\n // 재연결 후 페이지 완전 준비 대기\n await page\n .waitForFunction(() => document.readyState === 'complete', { timeout: 10000 })\n .catch(() => logger.warn('Page readyState timeout'))\n\n // JavaScript 초기화를 위한 추가 대기 (핵심 해결책)\n await new Promise(resolve => setTimeout(resolve, 2000))\n\n logger.info(`Full connection reset successful for connection '${connectionName}'`)\n } catch (resetError) {\n logger.error(`Connection reset failed for connection '${connectionName}':`, resetError)\n // 재설정 실패 시 이번 시도는 실패로 처리하고 다음 시도로 넘어감\n if (attempt === maxRetries) {\n throw new Error(`Connection reset failed after ${maxRetries + 1} attempts: ${resetError.message}`)\n }\n continue\n }\n }\n }\n\n // URL 구성\n const url = new URL(path, endpoint)\n if (queryParams && typeof queryParams === 'object') {\n Object.keys(queryParams).forEach(key => {\n if (queryParams[key] !== null && queryParams[key] !== undefined) {\n url.searchParams.append(key, String(queryParams[key]))\n }\n })\n }\n\n // 요청 옵션 구성\n const requestOptions: any = {\n method,\n headers: {\n ...headers\n },\n credentials: 'include'\n }\n\n // Content-Type과 body 처리\n if (requestBody && ['POST', 'PUT', 'PATCH'].includes(method)) {\n if (contentType) {\n requestOptions.headers['content-type'] = contentType\n switch (contentType) {\n case 'text/plain':\n requestOptions.body = JSON.stringify(requestBody)\n break\n case 'application/json':\n default:\n requestOptions.body = typeof requestBody === 'string' ? requestBody : JSON.stringify(requestBody)\n break\n }\n } else {\n requestOptions.headers['Content-Type'] = 'application/json'\n requestOptions.body = typeof requestBody === 'string' ? requestBody : JSON.stringify(requestBody)\n }\n }\n\n const response = await page.evaluate(\n async (urlString, opts, loginPagePath) => {\n try {\n // 페이지 상태 검증 (about:blank 또는 로딩 중인 페이지에서 fetch 방지)\n const pageLocation = window.location.href\n const pageReadyState = document.readyState\n\n if (pageLocation === 'about:blank' || pageReadyState !== 'complete') {\n return {\n ok: false,\n status: 0,\n statusText: 'Page Not Ready',\n headers: {},\n error: `Page state invalid - location: ${pageLocation}, readyState: ${pageReadyState}`,\n data: null,\n networkError: true\n }\n }\n\n const response = await fetch(urlString, { ...opts, redirect: 'manual' })\n\n const result = {\n ok: response.ok,\n status: response.status,\n statusText: response.statusText,\n headers: Object.fromEntries(response.headers.entries())\n }\n\n // 302 리디렉션 감지 - 로그인 페이지로의 리디렉션 체크\n if ([301, 302, 307, 308].includes(response.status)) {\n const location = response.headers.get('location') || ''\n // connection의 loginPagePath와 일반적인 로그인 경로들을 체크\n if (\n location.includes(loginPagePath) ||\n location.includes('/login') ||\n location.includes('/signin') ||\n location.includes('/auth')\n ) {\n return {\n ...result,\n error: `Redirected to login page: ${location}`,\n redirectedToLogin: true,\n location,\n data: null\n }\n }\n // 다른 리디렉션은 follow\n const redirectResponse = await fetch(urlString, opts)\n return {\n ok: redirectResponse.ok,\n status: redirectResponse.status,\n statusText: redirectResponse.statusText,\n headers: Object.fromEntries(redirectResponse.headers.entries()),\n data: redirectResponse.ok\n ? await redirectResponse\n .json()\n .catch(() => redirectResponse.text())\n .catch(() => null)\n : null,\n error: redirectResponse.ok ? null : `HTTP ${redirectResponse.status}: ${redirectResponse.statusText}`\n }\n }\n\n // status가 0인 경우는 네트워크 레벨 에러 (CORS, 연결 실패 등)\n if (response.status === 0) {\n return {\n ok: false,\n status: 0,\n statusText: response.statusText || 'Network Error',\n headers: {},\n error: 'HTTP 0 - Network connection failed (possible CORS, SSL, or connectivity issue)',\n data: null,\n networkError: true\n }\n }\n\n if (!response.ok) {\n return {\n ...result,\n error: `HTTP ${response.status}: ${response.statusText}`,\n data: null\n }\n }\n\n const contentType = response.headers.get('content-type') || ''\n\n if (contentType.includes('application/json')) {\n result['data'] = await response.json()\n } else if (contentType.includes('text/')) {\n result['data'] = await response.text()\n } else {\n // 응답이 없는 경우 (204 No Content 등)\n result['data'] = null\n }\n\n return result\n } catch (fetchError) {\n // 네트워크 레벨 에러 처리\n return {\n ok: false,\n status: 0,\n statusText: 'Network Error',\n headers: {},\n error: fetchError.message || 'Failed to fetch',\n data: null,\n networkError: true\n }\n }\n },\n url.toString(),\n requestOptions,\n loginPagePath\n )\n\n // 네트워크 에러 체크 (fetch 자체가 실패한 경우)\n if (response.networkError) {\n if (attempt < maxRetries) {\n logger.warn(`Network error detected: ${response.error}, retrying... (${attempt + 1}/${maxRetries + 1})`)\n await safeReleasePageResource(pageResource, releasePage, logger)\n page = null\n pageResource = null\n continue\n } else {\n await safeReleasePageResource(pageResource, releasePage, logger)\n throw new Error(`Network error after ${maxRetries + 1} attempts: ${response.error}`)\n }\n }\n\n // 로그인 리디렉션 감지 처리\n if (response.redirectedToLogin) {\n if (attempt < maxRetries) {\n logger.warn(\n `Login redirect detected: ${response.location}, performing full connection reset... (${attempt + 1}/${maxRetries + 1})`\n )\n\n // CRITICAL: 커넥션 재연결 방식으로 완전한 초기화\n try {\n const resetResult = await performFullConnectionReset(\n domain,\n connectionName,\n logger,\n pageResource,\n releasePage\n )\n pageResource = resetResult.pageResource\n page = resetResult.page\n\n // 재연결 후 페이지 완전 준비 대기\n await page\n .waitForFunction(() => document.readyState === 'complete', { timeout: 10000 })\n .catch(() => logger.warn('Page readyState timeout'))\n\n // JavaScript 초기화를 위한 추가 대기\n await new Promise(resolve => setTimeout(resolve, 2000))\n\n // 페이지 상태 확인\n const resetUrl = page.url()\n const cookies = await page.cookies()\n\n logger.info(`[Reset Success] URL: ${resetUrl}, Cookies: ${cookies.length}`)\n\n // about:blank 체크\n if (resetUrl === 'about:blank') {\n throw new Error('Page stuck on about:blank after reset')\n }\n } catch (resetError) {\n logger.error(`Connection reset failed after login redirect for connection '${connectionName}':`, resetError)\n if (attempt === maxRetries) {\n throw new Error(`Connection reset failed after login redirect: ${resetError.message}`)\n }\n continue\n }\n continue\n } else {\n await safeReleasePageResource(pageResource, releasePage, logger)\n throw new Error(`Login redirect after ${maxRetries + 1} attempts: ${response.error}`)\n }\n }\n\n // 세션 타임아웃 관련 에러 체크\n if (!response.ok && isSessionTimeoutError(response.status)) {\n if (attempt < maxRetries) {\n logger.warn(\n `Session timeout detected (${response.status}), performing full connection reset... (${attempt + 1}/${maxRetries + 1})`\n )\n\n // CRITICAL: 커넥션 재연결 방식으로 완전한 초기화\n try {\n const resetResult = await performFullConnectionReset(\n domain,\n connectionName,\n logger,\n pageResource,\n releasePage\n )\n pageResource = resetResult.pageResource\n page = resetResult.page\n\n // 재연결 후 페이지 완전 준비 대기\n await page\n .waitForFunction(() => document.readyState === 'complete', { timeout: 10000 })\n .catch(() => logger.warn('Page readyState timeout'))\n\n // JavaScript 초기화를 위한 추가 대기\n await new Promise(resolve => setTimeout(resolve, 2000))\n\n // 페이지 상태 확인\n const resetUrl = page.url()\n const cookies = await page.cookies()\n\n logger.info(`[Reset Success] URL: ${resetUrl}, Cookies: ${cookies.length}`)\n\n // about:blank 체크\n if (resetUrl === 'about:blank') {\n throw new Error('Page stuck on about:blank after reset')\n }\n } catch (resetError) {\n logger.error(\n `Connection reset failed after session timeout for connection '${connectionName}':`,\n resetError\n )\n if (attempt === maxRetries) {\n throw new Error(`Connection reset failed after session timeout: ${resetError.message}`)\n }\n continue\n }\n continue\n } else {\n await safeReleasePageResource(pageResource, releasePage, logger)\n throw new Error(`Session timeout after ${maxRetries + 1} attempts: ${response.error}`)\n }\n }\n\n // 기타 HTTP 에러\n if (!response.ok) {\n throw new Error(response.error)\n }\n\n // 성공 시 페이지 릴리즈 후 결과 반환\n const result = {\n data: response.data,\n status: response.status,\n headers: response.headers\n }\n\n await safeReleasePageResource(pageResource, releasePage, logger)\n\n return result\n } catch (error) {\n lastError = error\n logger.error(`Headless request attempt ${attempt + 1} failed:`, error)\n\n await safeReleasePageResource(pageResource, releasePage, logger)\n page = null\n pageResource = null\n\n // 세션 관련 에러가 아니거나 마지막 재시도면 에러 발생\n if (!isRecoverableError(error) || attempt === maxRetries) {\n throw error\n }\n\n logger.info(`Retrying request... (${attempt + 2}/${maxRetries + 1})`)\n }\n }\n\n // 모든 재시도가 실패한 경우 - 혹시 남은 리소스가 있으면 정리\n if (pageResource) {\n await safeReleasePageResource(pageResource, releasePage, logger)\n }\n throw lastError || new Error('Request failed after all retry attempts')\n}\n\n/**\n * 세션 타임아웃 관련 HTTP 상태 코드인지 확인\n */\nfunction isSessionTimeoutError(statusCode: number): boolean {\n return [401, 403].includes(statusCode)\n}\n\n/**\n * 복구 가능한 에러인지 확인\n */\nfunction isRecoverableError(error: any): boolean {\n const errorMessage = error.message?.toLowerCase() || ''\n\n // 복구 가능한 에러 키워드 (네트워크, 세션/인증 관련)\n const recoverableErrorKeywords = [\n 'failed to fetch',\n 'network error',\n 'connection failed',\n 'connection reset',\n 'timeout',\n 'timed out',\n 'unauthorized',\n 'forbidden',\n 'session',\n 'authentication',\n 'login',\n 'expired',\n 'redirected to login' // 로그인 리디렉션 추가\n ]\n\n return recoverableErrorKeywords.some(keyword => errorMessage.includes(keyword))\n}\n\n/**\n * Safely release page resource with comprehensive error handling\n */\nexport async function safeReleasePageResource(pageResource: any, releasePage: Function, logger: any): Promise<void> {\n if (!pageResource) {\n return\n }\n\n try {\n // Handle different pageResource formats\n if (pageResource.page) {\n // This is a complex resource object {page, browser, requiresManualRelease}\n const { page, browser, requiresManualRelease } = pageResource\n\n if (requiresManualRelease && browser) {\n // Manual release required - close page first, then release browser\n try {\n if (page && !page.isClosed()) {\n await page.close()\n logger.info('Page closed during manual resource release')\n }\n } catch (closeError) {\n logger.error('Failed to close page during manual release:', closeError)\n }\n\n // Release browser back to pool\n try {\n const { getHeadlessPool } = require('../../resource-pool/headless-pool')\n const pool = getHeadlessPool()\n await pool.release(browser)\n logger.info('Browser manually released to pool')\n } catch (releaseError) {\n logger.error('Failed to manually release browser to pool:', releaseError)\n }\n } else {\n // Standard release through releasePage\n await releasePage(page)\n logger.info('Page released through standard releasePage method')\n }\n } else {\n // Simple page object - use standard release\n await releasePage(pageResource)\n logger.info('Simple page resource released')\n }\n } catch (error) {\n logger.error('Critical error during page resource release:', error)\n\n // Last resort: try to force close the page if it exists\n try {\n const page = pageResource.page || pageResource\n if (page && !page.isClosed()) {\n await page.close()\n logger.warn('Force closed page as last resort')\n }\n } catch (forceCloseError) {\n logger.error('Failed to force close page:', forceCloseError)\n }\n }\n}\n\n/**\n * 커넥션을 완전 재초기화하는 함수\n * 세션 만료 시 커넥션 재연결과 동일한 완전한 초기화를 수행\n */\nasync function performFullConnectionReset(\n domain: any,\n connectionName: string,\n logger: any,\n pageResource: any,\n releasePage: Function\n): Promise<{ pageResource: any; page: any }> {\n // 기존 페이지 리소스 해제\n await safeReleasePageResource(pageResource, releasePage, logger)\n\n // 커넥션 엔티티 가져오기\n const connectionEntity = await ConnectionManager.getConnectionEntityByName(domain, connectionName)\n if (!connectionEntity) {\n throw new Error('Connection entity not found for reset')\n }\n\n // Connector를 직접 사용하여 연결 재설정\n const { type, edge } = connectionEntity\n const ProxyConnector = require('../../connector/proxy-connector').ProxyConnector\n const connector = edge ? ProxyConnector.instance : ConnectionManager.getConnector(type)\n\n if (!connector) {\n throw new Error(`Connector not found for type: ${type}`)\n }\n\n // 기존 연결 완전 해제\n await connector.disconnect(connectionEntity)\n logger.info(`Connection '${connectionName}' disconnected for reset`)\n\n // 브라우저 완전 종료 및 리소스 정리 대기 (중요!)\n // 수동 재연결 시에는 자연스럽게 시간이 지나가지만,\n // 코드에서는 즉시 연결을 시도하므로 명시적 대기 필요\n await new Promise(resolve => setTimeout(resolve, 3000))\n\n // 새로운 연결 생성 (완전한 초기화)\n await connector.connect(connectionEntity)\n logger.info(`Connection '${connectionName}' reconnected after reset`)\n\n // 새 브라우저 인스턴스 초기화 대기\n await new Promise(resolve => setTimeout(resolve, 2000))\n\n // 새로운 connection 인스턴스 가져오기\n const newConnection = await ConnectionManager.getConnectionInstanceByName(domain, connectionName)\n if (!newConnection || !newConnection.acquireSessionPage) {\n throw new Error('Failed to acquire session page after connection reset')\n }\n\n const newSessionResult = await newConnection.acquireSessionPage()\n let newPageResource: any\n let newPage: any\n\n if (newSessionResult && typeof newSessionResult === 'object' && newSessionResult.page) {\n newPageResource = newSessionResult\n newPage = newSessionResult.page\n } else {\n newPage = newSessionResult\n newPageResource = { page: newPage, requiresManualRelease: false }\n }\n\n // 새 페이지 안정화 대기 (핵심!)\n await new Promise(resolve => setTimeout(resolve, 1000))\n\n return { pageResource: newPageResource, page: newPage }\n}\n"]}
|