| const { parentPort } = require('worker_threads'); |
| const path = require('path'); |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| console.log(' [Inspector Worker] Worker thread started'); |
|
|
| |
| parentPort.on('message', async (data) => { |
| const { projectPath, scanTarget = 'โปรเจกต์' } = data; |
| |
| try { |
| console.log(` [Inspector Worker] Received job to analyze ${scanTarget}: ${projectPath}`); |
| |
| |
| parentPort.postMessage({ |
| type: 'progress', |
| message: `เริ่มการวิเคราะห์ ${scanTarget}...`, |
| progress: 0 |
| }); |
| |
| |
| const statusUpdateCallback = (message) => { |
| |
| parentPort.postMessage({ |
| type: 'status-update', |
| data: message |
| }); |
| }; |
| |
| |
| let ProjectInspector; |
| try { |
| |
| ProjectInspector = require('./project-inspector'); |
| } catch (e) { |
| throw new Error(`Failed to require ProjectInspector: ${e.message}`); |
| } |
|
|
| if (typeof ProjectInspector !== 'function') { |
| throw new Error('ProjectInspector that was required is not a constructor. Check module.exports.'); |
| } |
|
|
| |
| const inspector = new ProjectInspector(statusUpdateCallback); |
| |
| console.log(` [Inspector Worker] Created ProjectInspector with status callback`); |
| |
| |
| parentPort.postMessage({ |
| type: 'progress', |
| message: 'กำลังเตรียมเครื่องมือวิเคราะห์...', |
| progress: 10 |
| }); |
| |
| |
| const startTime = Date.now(); |
| console.log(` [Inspector Worker] Starting intensive analysis of: ${projectPath}`); |
| |
| |
| parentPort.postMessage({ |
| type: 'progress', |
| message: 'กำลังสแกนไฟล์ทั้งหมด...', |
| progress: 25 |
| }); |
| |
| |
| const analysisResult = await inspector.analyzeProject(projectPath); |
| |
| const endTime = Date.now(); |
| const duration = (endTime - startTime) / 1000; |
| |
| console.log(` [Inspector Worker] Analysis completed in ${duration}s`); |
| console.log(` [Inspector Worker] Found ${analysisResult.totalFiles} files, ${analysisResult.totalIssues} issues`); |
| |
| |
| parentPort.postMessage({ |
| type: 'progress', |
| message: 'การวิเคราะห์เสร็จสมบูรณ์!', |
| progress: 100 |
| }); |
| |
| |
| parentPort.postMessage({ |
| type: 'analysis-result', |
| success: true, |
| analysis: analysisResult, |
| scanTarget, |
| duration, |
| timestamp: new Date().toISOString() |
| }); |
|
|
| |
| console.log(' [Inspector Worker] Results sent, scheduling graceful exit...'); |
| setTimeout(() => { |
| console.log(' [Inspector Worker] Analysis complete, terminating gracefully'); |
| process.exit(0); |
| }, 100); |
|
|
| } catch (error) { |
| console.error(` [Inspector Worker] Analysis failed:`, error); |
| |
| |
| parentPort.postMessage({ |
| type: 'analysis-result', |
| success: false, |
| error: { |
| message: error.message, |
| stack: error.stack, |
| name: error.name, |
| timestamp: new Date().toISOString() |
| } |
| }); |
|
|
| |
| setTimeout(() => { |
| console.log(' [Inspector Worker] Error handled, terminating gracefully'); |
| process.exit(0); |
| }, 50); |
| } |
| }); |
|
|
| |
| process.on('unhandledRejection', (reason, promise) => { |
| console.error(' [Inspector Worker] Unhandled Rejection at:', promise, 'reason:', reason); |
| parentPort.postMessage({ |
| type: 'error', |
| error: { |
| message: `Unhandled rejection: ${reason}`, |
| stack: reason?.stack || 'No stack trace available', |
| name: 'UnhandledRejection', |
| timestamp: new Date().toISOString() |
| } |
| }); |
| }); |
|
|
| process.on('uncaughtException', (error) => { |
| console.error(' [Inspector Worker] Uncaught Exception:', error); |
| parentPort.postMessage({ |
| type: 'error', |
| error: { |
| message: error.message, |
| stack: error.stack, |
| name: error.name, |
| timestamp: new Date().toISOString() |
| } |
| }); |
| |
| setTimeout(() => { |
| console.log(' [Inspector Worker] Graceful shutdown after uncaught exception'); |
| process.exit(0); |
| }, 100); |
| }); |
|
|
| console.log(' [Inspector Worker] Listening for messages from Main Process...'); |