echoapi-cron-scheduler-batch 1.0.3 → 1.0.4
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.
- package/index.js +58 -40
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -400,8 +400,10 @@ class CronScheduler {
|
|
|
400
400
|
runners.forEach((runner, index) => {
|
|
401
401
|
// 负载均衡:选取当前存活的 worker
|
|
402
402
|
const workers = Object.values(cluster.workers).filter(w => w.isConnected());
|
|
403
|
+
console.log(`[Dispatch] Active workers count: ${workers.length}`);
|
|
403
404
|
const worker = _.sample(workers);
|
|
404
405
|
if (worker) {
|
|
406
|
+
console.log(`[Dispatch] Sending Unit ${index} to Worker ${worker.process.pid}`);
|
|
405
407
|
worker.send({
|
|
406
408
|
action: 'EXECUTE_UNIT',
|
|
407
409
|
payload: {
|
|
@@ -412,6 +414,8 @@ class CronScheduler {
|
|
|
412
414
|
unit_index: index
|
|
413
415
|
}
|
|
414
416
|
});
|
|
417
|
+
} else {
|
|
418
|
+
console.error(`[Dispatch Error] No active workers available to handle job ${job.job_id}`);
|
|
415
419
|
}
|
|
416
420
|
});
|
|
417
421
|
} catch (e) {
|
|
@@ -424,50 +428,64 @@ class CronScheduler {
|
|
|
424
428
|
// ==========================================
|
|
425
429
|
|
|
426
430
|
_startWorker() {
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
431
|
+
try {
|
|
432
|
+
console.log(`[Worker ${process.pid}] Starting initialization...`);
|
|
433
|
+
const { run: runner } = require('runner-runtime');
|
|
434
|
+
const net = require('net');
|
|
435
|
+
|
|
436
|
+
process.on('message', async (msg) => {
|
|
437
|
+
if (msg.action === 'EXECUTE_UNIT') {
|
|
438
|
+
const { executionId, api_token, test_events, option } = msg.payload;
|
|
439
|
+
const socketPath = path.join(os.tmpdir(), `echoapi_${uuidv4()}.sock`);
|
|
440
|
+
|
|
441
|
+
const server = net.createServer((socket) => {
|
|
442
|
+
socket.on('data', async (stream) => {
|
|
443
|
+
try {
|
|
444
|
+
const info = JSON.parse(stream.toString());
|
|
445
|
+
const { action, data } = info;
|
|
446
|
+
await this._handleUnitScript(socket, action, data);
|
|
447
|
+
} catch (e) {
|
|
448
|
+
socket.write(JSON.stringify({ status: 'error', message: e.message }) + "\n\n");
|
|
449
|
+
}
|
|
450
|
+
});
|
|
444
451
|
});
|
|
445
|
-
});
|
|
446
452
|
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
453
|
+
// --- 这里是关键!注册错误监听 ---
|
|
454
|
+
server.on('error', (err) => {
|
|
455
|
+
// 在服务器上看到这个日志,就能定位是权限问题还是路径问题
|
|
456
|
+
console.error(`[Worker Socket Server Error] PID: ${process.pid}`);
|
|
457
|
+
console.error(`[Error Details] Code: ${err.code} | Message: ${err.message}`);
|
|
458
|
+
console.error(`[Attempted Path] ${socketPath}`);
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
server.listen(socketPath, () => {
|
|
462
|
+
const finalOptions = _.cloneDeep(option || {});
|
|
463
|
+
const base64Pipe = Buffer.from(socketPath).toString('base64');
|
|
464
|
+
_.set(finalOptions, 'env.ELECTRON_PIPE', base64Pipe);
|
|
465
|
+
|
|
466
|
+
runner(test_events, finalOptions, (res) => {
|
|
467
|
+
if (res?.action === 'complete') {
|
|
468
|
+
process.send({
|
|
469
|
+
action: 'UNIT_COMPLETED',
|
|
470
|
+
payload: { executionId, data: res.data, api_token }
|
|
471
|
+
});
|
|
472
|
+
server.close();
|
|
473
|
+
if (fs.existsSync(socketPath)) {
|
|
474
|
+
try { fs.unlinkSync(socketPath); } catch (e) { }
|
|
475
|
+
}
|
|
461
476
|
}
|
|
462
|
-
}
|
|
477
|
+
});
|
|
463
478
|
});
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
})
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
process.on('uncaughtException', (err) => {
|
|
482
|
+
console.error('[Worker Fatal Error] 捕获到沙箱崩溃:', err.message);
|
|
483
|
+
// 即使崩溃也不要让子进程立即退出,或者让 Master 自动重启它
|
|
484
|
+
});
|
|
485
|
+
} catch (error) {
|
|
486
|
+
console.error(`[Worker ${process.pid}] Critical Boot Error:`, err.stack);
|
|
487
|
+
process.exit(1);
|
|
488
|
+
}
|
|
471
489
|
}
|
|
472
490
|
|
|
473
491
|
async _handleUnitScript(socket, action, data) {
|