@resolveio/server-lib 20.14.22 → 20.14.24

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.
@@ -31,9 +31,14 @@ export declare class WorkerDispatcherManager {
31
31
  private _taskTimings;
32
32
  private MAX_CONCURRENCY;
33
33
  private WORKER_TASK_LOG_THRESHOLD_MS;
34
+ private WORKER_PUBLICATION_SLOW_MS;
35
+ private _workerTaskDebug;
34
36
  constructor();
35
37
  static create(websocketManager: WebSocketManager, methodManager: MethodManager): WorkerDispatcherManager;
36
38
  initialize(websocketManager: WebSocketManager, methodManager: MethodManager): void;
39
+ private parseDebugFlag;
40
+ private summarizePublicationValue;
41
+ private getPublicationLogInfo;
37
42
  isSafeShutdown(): boolean;
38
43
  addWorker(ws: WebSocket.WebSocket): void;
39
44
  disconnectWorker(workerId: string): void;
@@ -35,6 +35,8 @@ var WorkerDispatcherManager = /** @class */ (function () {
35
35
  this._taskTimings = new Map();
36
36
  this.MAX_CONCURRENCY = 10;
37
37
  this.WORKER_TASK_LOG_THRESHOLD_MS = 200;
38
+ this.WORKER_PUBLICATION_SLOW_MS = 20000;
39
+ this._workerTaskDebug = false;
38
40
  }
39
41
  WorkerDispatcherManager.create = function (websocketManager, methodManager) {
40
42
  var workerDispatcherManager = new WorkerDispatcherManager();
@@ -42,8 +44,62 @@ var WorkerDispatcherManager = /** @class */ (function () {
42
44
  return workerDispatcherManager;
43
45
  };
44
46
  WorkerDispatcherManager.prototype.initialize = function (websocketManager, methodManager) {
47
+ var _a, _b;
45
48
  this._websocketManager = websocketManager;
46
49
  this._methodManager = methodManager;
50
+ this._workerTaskDebug = this.parseDebugFlag((_a = process.env.WORKER_TASK_DEBUG) !== null && _a !== void 0 ? _a : process.env.WORKER_DISPATCH_DEBUG);
51
+ var slowPublicationMs = parseInt((_b = process.env.WORKER_PUBLICATION_SLOW_MS) !== null && _b !== void 0 ? _b : '', 10);
52
+ if (!Number.isNaN(slowPublicationMs) && slowPublicationMs > 0) {
53
+ this.WORKER_PUBLICATION_SLOW_MS = slowPublicationMs;
54
+ }
55
+ };
56
+ WorkerDispatcherManager.prototype.parseDebugFlag = function (value) {
57
+ if (value === true) {
58
+ return true;
59
+ }
60
+ if (value === false || value === null || value === undefined) {
61
+ return false;
62
+ }
63
+ if (typeof value === 'number') {
64
+ return value === 1;
65
+ }
66
+ if (typeof value === 'string') {
67
+ var normalized = value.trim().toLowerCase();
68
+ return ['1', 'true', 'yes', 'y', 'on'].includes(normalized);
69
+ }
70
+ return false;
71
+ };
72
+ WorkerDispatcherManager.prototype.summarizePublicationValue = function (value) {
73
+ if (value === null || value === undefined) {
74
+ return value;
75
+ }
76
+ var valueType = typeof value;
77
+ if (valueType === 'string' || valueType === 'number' || valueType === 'boolean') {
78
+ return value;
79
+ }
80
+ if (Array.isArray(value)) {
81
+ return "[Array(".concat(value.length, ")]");
82
+ }
83
+ if (valueType === 'object') {
84
+ return "[Object keys=".concat(Object.keys(value).length, "]");
85
+ }
86
+ return "[".concat(valueType, "]");
87
+ };
88
+ WorkerDispatcherManager.prototype.getPublicationLogInfo = function (method, params) {
89
+ var _this = this;
90
+ if (method !== 'runPublication' || !Array.isArray(params)) {
91
+ return null;
92
+ }
93
+ var publication = typeof params[0] === 'string' ? params[0] : null;
94
+ var subscriptionData = Array.isArray(params[1]) ? params[1] : [];
95
+ var userId = typeof params[2] === 'string' ? params[2] : null;
96
+ var subscriptionDataPreview = subscriptionData.slice(0, 3).map(function (item) { return _this.summarizePublicationValue(item); });
97
+ return {
98
+ publication: publication,
99
+ subscriptionDataCount: subscriptionData.length,
100
+ subscriptionDataPreview: subscriptionDataPreview,
101
+ userId: userId
102
+ };
47
103
  };
48
104
  WorkerDispatcherManager.prototype.isSafeShutdown = function () {
49
105
  return !this._taskQueue.length && !this._pendingTasks.size;
@@ -127,11 +183,27 @@ var WorkerDispatcherManager = /** @class */ (function () {
127
183
  queuedAt: queuedAt,
128
184
  method: method,
129
185
  messageId: messageId,
130
- userContext: userContext
186
+ userContext: userContext,
187
+ params: params
131
188
  });
132
189
  if (userContext && userContext.id_ws) {
133
190
  this._clientRequests[taskId] = userContext.id_ws;
134
191
  }
192
+ if (this.shouldDebug()) {
193
+ var publicationInfo = this.getPublicationLogInfo(method, params);
194
+ var logPayload = {
195
+ taskId: taskId,
196
+ method: method,
197
+ messageId: messageId,
198
+ priority: method === 'runPublication' ? 'publication' : 'method',
199
+ queueDepth: this._taskQueue.length,
200
+ userContext: userContext
201
+ };
202
+ if (publicationInfo) {
203
+ Object.assign(logPayload, publicationInfo);
204
+ }
205
+ this.logWorkerEvent('workerTaskQueued', logPayload);
206
+ }
135
207
  this.dispatchQueue();
136
208
  };
137
209
  /**
@@ -162,7 +234,8 @@ var WorkerDispatcherManager = /** @class */ (function () {
162
234
  queuedAt: queuedAt,
163
235
  method: method,
164
236
  messageId: 0,
165
- userContext: { user: 'Internal System' }
237
+ userContext: { user: 'Internal System' },
238
+ params: params
166
239
  });
167
240
  _this._pendingTasks.set(taskId, {
168
241
  timeout: null,
@@ -171,6 +244,21 @@ var WorkerDispatcherManager = /** @class */ (function () {
171
244
  if (_this._methodManager.getEnableDebug()) {
172
245
  console.log(new Date(), 'Send Internal Promise', _this._taskQueue);
173
246
  }
247
+ if (_this.shouldDebug()) {
248
+ var publicationInfo = _this.getPublicationLogInfo(method, params);
249
+ var logPayload = {
250
+ taskId: taskId,
251
+ method: method,
252
+ messageId: 0,
253
+ priority: method === 'runPublication' ? 'publication' : 'method',
254
+ queueDepth: _this._taskQueue.length,
255
+ userContext: { user: 'Internal System' }
256
+ };
257
+ if (publicationInfo) {
258
+ Object.assign(logPayload, publicationInfo);
259
+ }
260
+ _this.logWorkerEvent('workerTaskQueued', logPayload);
261
+ }
174
262
  _this.dispatchQueue();
175
263
  });
176
264
  };
@@ -202,7 +290,8 @@ var WorkerDispatcherManager = /** @class */ (function () {
202
290
  queuedAt: queuedAt,
203
291
  method: method,
204
292
  messageId: 0,
205
- userContext: { user: 'Internal System' }
293
+ userContext: { user: 'Internal System' },
294
+ params: params
206
295
  });
207
296
  _this._pendingTasks.set(taskId, {
208
297
  timeout: null,
@@ -212,6 +301,21 @@ var WorkerDispatcherManager = /** @class */ (function () {
212
301
  if (_this._methodManager.getEnableDebug()) {
213
302
  console.log(new Date(), 'Send Internal Raw Promise', _this._taskQueue);
214
303
  }
304
+ if (_this.shouldDebug()) {
305
+ var publicationInfo = _this.getPublicationLogInfo(method, params);
306
+ var logPayload = {
307
+ taskId: taskId,
308
+ method: method,
309
+ messageId: 0,
310
+ priority: method === 'runPublication' ? 'publication' : 'method',
311
+ queueDepth: _this._taskQueue.length,
312
+ userContext: { user: 'Internal System' }
313
+ };
314
+ if (publicationInfo) {
315
+ Object.assign(logPayload, publicationInfo);
316
+ }
317
+ _this.logWorkerEvent('workerTaskQueued', logPayload);
318
+ }
215
319
  _this.dispatchQueue();
216
320
  });
217
321
  };
@@ -240,11 +344,27 @@ var WorkerDispatcherManager = /** @class */ (function () {
240
344
  queuedAt: queuedAt,
241
345
  method: method,
242
346
  messageId: 0,
243
- userContext: { user: 'Internal System' }
347
+ userContext: { user: 'Internal System' },
348
+ params: params
244
349
  });
245
350
  if (this._methodManager.getEnableDebug()) {
246
351
  console.log(new Date(), 'Send Internal Task', this._taskQueue);
247
352
  }
353
+ if (this.shouldDebug()) {
354
+ var publicationInfo = this.getPublicationLogInfo(method, params);
355
+ var logPayload = {
356
+ taskId: taskId,
357
+ method: method,
358
+ messageId: 0,
359
+ priority: method === 'runPublication' ? 'publication' : 'method',
360
+ queueDepth: this._taskQueue.length,
361
+ userContext: { user: 'Internal System' }
362
+ };
363
+ if (publicationInfo) {
364
+ Object.assign(logPayload, publicationInfo);
365
+ }
366
+ this.logWorkerEvent('workerTaskQueued', logPayload);
367
+ }
248
368
  this.dispatchQueue();
249
369
  };
250
370
  /**
@@ -257,14 +377,32 @@ var WorkerDispatcherManager = /** @class */ (function () {
257
377
  }
258
378
  while (this._taskQueue.length > 0) {
259
379
  var assigned = false;
260
- for (var i = 0; i < this._taskQueue.length; i++) {
261
- var task = this._taskQueue[i];
262
- var targetWorker = this.findWorkerForTask(task);
263
- if (targetWorker) {
264
- this._taskQueue.splice(i, 1);
265
- this.assignTaskToWorker(targetWorker, task);
266
- assigned = true;
267
- break;
380
+ var hasPublicationTask = this._taskQueue.some(function (task) { return task.method === 'runPublication'; });
381
+ if (hasPublicationTask) {
382
+ for (var i = 0; i < this._taskQueue.length; i++) {
383
+ var task = this._taskQueue[i];
384
+ if (task.method !== 'runPublication') {
385
+ continue;
386
+ }
387
+ var targetWorker = this.findWorkerForTask(task);
388
+ if (targetWorker) {
389
+ this._taskQueue.splice(i, 1);
390
+ this.assignTaskToWorker(targetWorker, task);
391
+ assigned = true;
392
+ break;
393
+ }
394
+ }
395
+ }
396
+ if (!assigned) {
397
+ for (var i = 0; i < this._taskQueue.length; i++) {
398
+ var task = this._taskQueue[i];
399
+ var targetWorker = this.findWorkerForTask(task);
400
+ if (targetWorker) {
401
+ this._taskQueue.splice(i, 1);
402
+ this.assignTaskToWorker(targetWorker, task);
403
+ assigned = true;
404
+ break;
405
+ }
268
406
  }
269
407
  }
270
408
  if (!assigned) {
@@ -381,6 +519,9 @@ var WorkerDispatcherManager = /** @class */ (function () {
381
519
  timing.method = task.method;
382
520
  timing.messageId = task.messageId;
383
521
  timing.userContext = task.userContext;
522
+ if (!timing.params) {
523
+ timing.params = task.params;
524
+ }
384
525
  this._taskTimings.set(task.taskId, timing);
385
526
  worker.activeTasks.push({
386
527
  taskId: task.taskId,
@@ -401,6 +542,25 @@ var WorkerDispatcherManager = /** @class */ (function () {
401
542
  queuedAt: timing.queuedAt,
402
543
  dispatchedAt: dispatchedAt
403
544
  };
545
+ if (this.shouldDebug()) {
546
+ var publicationInfo = this.getPublicationLogInfo(task.method, task.params);
547
+ var logPayload = {
548
+ taskId: task.taskId,
549
+ method: task.method,
550
+ messageId: task.messageId,
551
+ priority: task.method === 'runPublication' ? 'publication' : 'method',
552
+ worker: {
553
+ id: worker.id,
554
+ index: worker.workerIndex,
555
+ instance: worker.workerInstance
556
+ },
557
+ queueDepth: this._taskQueue.length
558
+ };
559
+ if (publicationInfo) {
560
+ Object.assign(logPayload, publicationInfo);
561
+ }
562
+ this.logWorkerEvent('workerTaskAssigned', logPayload);
563
+ }
404
564
  var timeoutHandle = setTimeout(function () {
405
565
  var pending = _this._pendingTasks.get(task.taskId);
406
566
  var timeoutAt = Date.now();
@@ -413,7 +573,8 @@ var WorkerDispatcherManager = /** @class */ (function () {
413
573
  }
414
574
  _this._pendingTasks.delete(task.taskId);
415
575
  }
416
- _this.logWorkerEvent('workerTaskTimeout', {
576
+ var publicationInfo = _this.getPublicationLogInfo(task.method, task.params);
577
+ var logPayload = {
417
578
  taskId: task.taskId,
418
579
  method: task.method,
419
580
  messageId: task.messageId,
@@ -426,7 +587,11 @@ var WorkerDispatcherManager = /** @class */ (function () {
426
587
  instance: worker.workerInstance
427
588
  },
428
589
  workers: _this._workers.map(function (a) { return a.activeTasks; })
429
- });
590
+ };
591
+ if (publicationInfo) {
592
+ Object.assign(logPayload, publicationInfo);
593
+ }
594
+ _this.logWorkerEvent('workerTaskTimeout', logPayload);
430
595
  worker.activeTasks = worker.activeTasks.filter(function (a) { return a.taskId !== task.taskId; });
431
596
  _this._taskTimings.delete(task.taskId);
432
597
  if (task.userContext && task.userContext.id_ws) {
@@ -516,7 +681,14 @@ var WorkerDispatcherManager = /** @class */ (function () {
516
681
  }
517
682
  };
518
683
  WorkerDispatcherManager.prototype.shouldDebug = function () {
519
- return this._methodManager.getEnableDebug() || process.env.WORKER_DISPATCH_DEBUG === 'true';
684
+ var _a;
685
+ if (this._methodManager.getEnableDebug()) {
686
+ return true;
687
+ }
688
+ if (this._workerTaskDebug) {
689
+ return true;
690
+ }
691
+ return this.parseDebugFlag((_a = process.env.WORKER_TASK_DEBUG) !== null && _a !== void 0 ? _a : process.env.WORKER_DISPATCH_DEBUG);
520
692
  };
521
693
  WorkerDispatcherManager.prototype.logWorkerEvent = function (event, details) {
522
694
  console.log(this.safeStringify(__assign({ ts: new Date().toISOString(), event: event }, details)));
@@ -558,6 +730,7 @@ var WorkerDispatcherManager = /** @class */ (function () {
558
730
  * Handle messages coming back from a worker (like 'taskComplete').
559
731
  */
560
732
  WorkerDispatcherManager.prototype.handleWorkerMessage = function (workerId, rawMessage) {
733
+ var _a;
561
734
  var data;
562
735
  try {
563
736
  if (typeof rawMessage === 'string') {
@@ -614,10 +787,39 @@ var WorkerDispatcherManager = /** @class */ (function () {
614
787
  }
615
788
  worker.activeTasks = worker.activeTasks.filter(function (a) { return a.taskId !== taskId_1; });
616
789
  this._taskTimings.delete(taskId_1);
617
- if (error || (dispatchToCompleteMs !== null && dispatchToCompleteMs >= this.WORKER_TASK_LOG_THRESHOLD_MS)) {
618
- this.logWorkerEvent('workerTaskComplete', {
790
+ var method = (_a = pendingTask === null || pendingTask === void 0 ? void 0 : pendingTask.method) !== null && _a !== void 0 ? _a : timing === null || timing === void 0 ? void 0 : timing.method;
791
+ var params = timing === null || timing === void 0 ? void 0 : timing.params;
792
+ var publicationInfo = this.getPublicationLogInfo(method, params);
793
+ var isSlowPublication = !!publicationInfo && totalMs !== null && totalMs >= this.WORKER_PUBLICATION_SLOW_MS;
794
+ var shouldLogComplete = !!error || (dispatchToCompleteMs !== null && dispatchToCompleteMs >= this.WORKER_TASK_LOG_THRESHOLD_MS);
795
+ if (isSlowPublication) {
796
+ var logPayload = {
619
797
  taskId: taskId_1,
620
- method: pendingTask === null || pendingTask === void 0 ? void 0 : pendingTask.method,
798
+ method: method,
799
+ messageId: messageId,
800
+ error: error,
801
+ dispatchToCompleteMs: dispatchToCompleteMs,
802
+ queueWaitMs: queueWaitMs,
803
+ totalMs: totalMs,
804
+ packedBytes: packedBytes,
805
+ userContext: timing === null || timing === void 0 ? void 0 : timing.userContext,
806
+ worker: {
807
+ id: worker.id,
808
+ index: worker.workerIndex,
809
+ instance: worker.workerInstance
810
+ },
811
+ queue: this.getQueueSnapshot(),
812
+ slowThresholdMs: this.WORKER_PUBLICATION_SLOW_MS
813
+ };
814
+ if (publicationInfo) {
815
+ Object.assign(logPayload, publicationInfo);
816
+ }
817
+ this.logWorkerEvent('workerPublicationSlow', logPayload);
818
+ }
819
+ else if (shouldLogComplete) {
820
+ var logPayload = {
821
+ taskId: taskId_1,
822
+ method: method,
621
823
  messageId: messageId,
622
824
  error: error,
623
825
  dispatchToCompleteMs: dispatchToCompleteMs,
@@ -631,7 +833,34 @@ var WorkerDispatcherManager = /** @class */ (function () {
631
833
  instance: worker.workerInstance
632
834
  },
633
835
  queue: this.getQueueSnapshot()
634
- });
836
+ };
837
+ if (publicationInfo) {
838
+ Object.assign(logPayload, publicationInfo);
839
+ }
840
+ this.logWorkerEvent('workerTaskComplete', logPayload);
841
+ }
842
+ else if (this.shouldDebug()) {
843
+ var logPayload = {
844
+ taskId: taskId_1,
845
+ method: method,
846
+ messageId: messageId,
847
+ error: error,
848
+ dispatchToCompleteMs: dispatchToCompleteMs,
849
+ queueWaitMs: queueWaitMs,
850
+ totalMs: totalMs,
851
+ packedBytes: packedBytes,
852
+ userContext: timing === null || timing === void 0 ? void 0 : timing.userContext,
853
+ worker: {
854
+ id: worker.id,
855
+ index: worker.workerIndex,
856
+ instance: worker.workerInstance
857
+ },
858
+ queue: this.getQueueSnapshot()
859
+ };
860
+ if (publicationInfo) {
861
+ Object.assign(logPayload, publicationInfo);
862
+ }
863
+ this.logWorkerEvent('workerTaskCompleteDebug', logPayload);
635
864
  }
636
865
  if (pendingTask) {
637
866
  if (pendingTask.promise) {