@stemy/backend 3.1.3 → 3.1.6

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.
@@ -2413,18 +2413,21 @@
2413
2413
  this.container = container;
2414
2414
  this.jobTypes = jobTypes || [];
2415
2415
  this.jobs = this.jobTypes.reduce(function (res, jobType) {
2416
- res[getConstructorName(jobType)] = function (jobParams) {
2417
- var job = _this.resolveJobInstance(jobType, jobParams);
2418
- return job.process(_this.messageBridge);
2416
+ var jobName = getConstructorName(jobType);
2417
+ res[jobName] = function (jobParams, uniqueId) {
2418
+ var job = _this.resolveJobInstance(jobType, jobParams, uniqueId);
2419
+ var messageBridge = {
2420
+ sendMessage: function (message, params) {
2421
+ params.uniqueId = uniqueId;
2422
+ _this.workerPush.send([message, JSON.stringify(params)]);
2423
+ }
2424
+ };
2425
+ messageBridge.sendMessage("job-started", { name: jobName });
2426
+ return job.process(messageBridge);
2419
2427
  };
2420
2428
  return res;
2421
2429
  }, {});
2422
2430
  this.messages = new rxjs.Subject();
2423
- this.messageBridge = {
2424
- sendMessage: function (message, params) {
2425
- _this.workerPush.send([message, JSON.stringify(params)]);
2426
- }
2427
- };
2428
2431
  this.processing = false;
2429
2432
  this.maxTimeout = this.config.resolve("jobTimeout");
2430
2433
  }
@@ -2517,32 +2520,31 @@
2517
2520
  case 2:
2518
2521
  _a.sent();
2519
2522
  console.log("Worker consumer connected to: " + pullHost);
2520
- this.workerPull.on("message", function (name, args, uniqueId) { return __awaiter$o(_this, void 0, void 0, function () {
2521
- var jobName, jobParams, timerId, e_1, e_2;
2523
+ this.workerPull.on("message", function (name, args, uniqId) { return __awaiter$o(_this, void 0, void 0, function () {
2524
+ var jobName, jobParams, uniqueId, e_1, e_2;
2522
2525
  return __generator(this, function (_a) {
2523
2526
  switch (_a.label) {
2524
2527
  case 0:
2525
2528
  _a.trys.push([0, 5, , 6]);
2526
2529
  jobName = name.toString("utf8");
2527
2530
  jobParams = JSON.parse(args.toString("utf8"));
2528
- timerId = uniqueId === null || uniqueId === void 0 ? void 0 : uniqueId.toString("utf8");
2529
- console.time(timerId);
2530
- console.timeLog(timerId, "Started working on background job: " + colorize(jobName, exports.ConsoleColor.FgCyan) + " with args: \n" + jsonHighlight(jobParams) + "\n\n");
2531
- this.messageBridge.sendMessage("job-started", { name: jobName });
2531
+ uniqueId = uniqId === null || uniqId === void 0 ? void 0 : uniqId.toString("utf8");
2532
+ console.time(uniqueId);
2533
+ console.timeLog(uniqueId, "Started working on background job: " + colorize(jobName, exports.ConsoleColor.FgCyan) + " with args: \n" + jsonHighlight(jobParams) + "\n\n");
2532
2534
  _a.label = 1;
2533
2535
  case 1:
2534
2536
  _a.trys.push([1, 3, , 4]);
2535
- return [4 /*yield*/, Promise.race([this.jobs[jobName](jobParams), promiseTimeout(this.maxTimeout, true)])];
2537
+ return [4 /*yield*/, Promise.race([this.jobs[jobName](jobParams, uniqueId), promiseTimeout(this.maxTimeout, true)])];
2536
2538
  case 2:
2537
2539
  _a.sent();
2538
- console.timeLog(timerId, "Finished working on background job: " + colorize(jobName, exports.ConsoleColor.FgCyan) + "\n\n");
2540
+ console.timeLog(uniqueId, "Finished working on background job: " + colorize(jobName, exports.ConsoleColor.FgCyan) + "\n\n");
2539
2541
  return [3 /*break*/, 4];
2540
2542
  case 3:
2541
2543
  e_1 = _a.sent();
2542
- console.timeLog(timerId, "Background job failed: " + colorize(jobName, exports.ConsoleColor.FgRed) + "\n" + e_1 + "\n\n");
2544
+ console.timeLog(uniqueId, "Background job failed: " + colorize(jobName, exports.ConsoleColor.FgRed) + "\n" + e_1 + "\n\n");
2543
2545
  return [3 /*break*/, 4];
2544
2546
  case 4:
2545
- console.timeEnd(timerId);
2547
+ console.timeEnd(uniqueId);
2546
2548
  return [3 /*break*/, 6];
2547
2549
  case 5:
2548
2550
  e_2 = _a.sent();
@@ -2594,33 +2596,40 @@
2594
2596
  this.apiPull.on("message", function (name, args) {
2595
2597
  var message = name.toString("utf8");
2596
2598
  var params = JSON.parse((args === null || args === void 0 ? void 0 : args.toString("utf8")) || "{}");
2597
- console.log("Received a message from worker: \"" + colorize(message, exports.ConsoleColor.FgCyan) + "\" with args: " + jsonHighlight(params) + "\n\n");
2599
+ var paramTypes = Object.keys(params).reduce(function (res, key) {
2600
+ res[key] = getType(params[key]);
2601
+ return res;
2602
+ }, {});
2603
+ console.log("Received a message from worker: \"" + colorize(message, exports.ConsoleColor.FgCyan) + "\" with args: " + jsonHighlight(paramTypes) + "\n\n");
2598
2604
  _this.messages.next({ message: message, params: params });
2599
2605
  });
2600
2606
  console.log("API consumer bound to port: " + backPort);
2601
2607
  }
2602
2608
  return this.tryResolve(jobType, params);
2603
2609
  };
2604
- JobManager.prototype.resolveJobInstance = function (jobType, params) {
2610
+ JobManager.prototype.resolveJobInstance = function (jobType, params, uniqueId) {
2611
+ if (uniqueId === void 0) { uniqueId = ""; }
2605
2612
  var container = this.container.createChildContainer();
2606
2613
  Object.keys(params).map(function (name) {
2607
2614
  container.register(name, { useValue: params[name] });
2608
2615
  });
2616
+ container.register("uniqueId", { useValue: uniqueId });
2609
2617
  container.register(jobType, jobType);
2610
2618
  return container.resolve(jobType);
2611
2619
  };
2612
2620
  JobManager.prototype.sendToWorkers = function (jobName, params) {
2613
2621
  return __awaiter$o(this, void 0, void 0, function () {
2614
- var publisher;
2622
+ var publisher, uniqueId;
2615
2623
  return __generator(this, function (_a) {
2616
2624
  switch (_a.label) {
2617
2625
  case 0: return [4 /*yield*/, this.apiPush];
2618
2626
  case 1:
2619
2627
  publisher = _a.sent();
2620
- return [4 /*yield*/, publisher.send([jobName, JSON.stringify(params), new bson.ObjectId().toHexString()])];
2628
+ uniqueId = new bson.ObjectId().toHexString();
2629
+ return [4 /*yield*/, publisher.send([jobName, JSON.stringify(params), uniqueId])];
2621
2630
  case 2:
2622
2631
  _a.sent();
2623
- return [2 /*return*/];
2632
+ return [2 /*return*/, uniqueId];
2624
2633
  }
2625
2634
  });
2626
2635
  });