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.
Files changed (2) hide show
  1. package/index.js +58 -40
  2. 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
- const { run: runner } = require('runner-runtime');
428
- const net = require('net');
429
-
430
- process.on('message', async (msg) => {
431
- if (msg.action === 'EXECUTE_UNIT') {
432
- const { executionId, api_token, test_events, option } = msg.payload;
433
- const socketPath = path.join(os.tmpdir(), `echoapi_${uuidv4()}.sock`);
434
-
435
- const server = net.createServer((socket) => {
436
- socket.on('data', async (stream) => {
437
- try {
438
- const info = JSON.parse(stream.toString());
439
- const { action, data } = info;
440
- await this._handleUnitScript(socket, action, data);
441
- } catch (e) {
442
- socket.write(JSON.stringify({ status: 'error', message: e.message }) + "\n\n");
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
- server.listen(socketPath, () => {
448
- const finalOptions = _.cloneDeep(option || {});
449
- const base64Pipe = Buffer.from(socketPath).toString('base64');
450
- _.set(finalOptions, 'env.ELECTRON_PIPE', base64Pipe);
451
-
452
- runner(test_events, finalOptions, (res) => {
453
- if (res?.action === 'complete') {
454
- process.send({
455
- action: 'UNIT_COMPLETED',
456
- payload: { executionId, data: res.data, api_token }
457
- });
458
- server.close();
459
- if (fs.existsSync(socketPath)) {
460
- try { fs.unlinkSync(socketPath); } catch (e) { }
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
- process.on('uncaughtException', (err) => {
468
- console.error('[Worker Fatal Error] 捕获到沙箱崩溃:', err.message);
469
- // 即使崩溃也不要让子进程立即退出,或者让 Master 自动重启它
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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "echoapi-cron-scheduler-batch",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {