@stemy/backend 3.0.1 → 3.1.2

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.
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('path'), require('body-parser'), require('jsonwebtoken'), require('tsyringe'), require('routing-controllers'), require('socket-controllers'), require('routing-controllers-openapi'), require('class-transformer/storage'), require('class-validator'), require('class-validator-jsonschema'), require('child_process'), require('crypto'), require('rxjs'), require('rxjs/internal/util/canReportError'), require('fs'), require('mongoose'), require('mongoose/lib/utils'), require('stream'), require('bson'), require('fontkit'), require('file-type'), require('sharp'), require('axios'), require('mongodb'), require('dotenv'), require('node-cron'), require('zeromq'), require('socket.io-client'), require('http'), require('express'), require('socket.io'), require('uuid'), require('nodemailer'), require('handlebars'), require('bcrypt'), require('moment')) :
3
- typeof define === 'function' && define.amd ? define('@stemy/backend', ['exports', 'path', 'body-parser', 'jsonwebtoken', 'tsyringe', 'routing-controllers', 'socket-controllers', 'routing-controllers-openapi', 'class-transformer/storage', 'class-validator', 'class-validator-jsonschema', 'child_process', 'crypto', 'rxjs', 'rxjs/internal/util/canReportError', 'fs', 'mongoose', 'mongoose/lib/utils', 'stream', 'bson', 'fontkit', 'file-type', 'sharp', 'axios', 'mongodb', 'dotenv', 'node-cron', 'zeromq', 'socket.io-client', 'http', 'express', 'socket.io', 'uuid', 'nodemailer', 'handlebars', 'bcrypt', 'moment'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.stemy = global.stemy || {}, global.stemy.backend = {}), global.path, global["body-parser"], global.jsonwebtoken, global.tsyringe, global["routing-controllers"], global["socket-controllers"], global["routing-controllers-openapi"], global["class-transformer/storage"], global["class-validator"], global["class-validator-jsonschema"], global.child_process, global.crypto, global.rxjs, global.rxjs["internal/util/canReportError"], global.fs, global.mongoose, global["mongoose-lib-utils"], global.stream, global.bson, global.fontkit, global["file-type"], global.sharp, global.axios, global.mongodb, global.dotenv, global["node-cron"], global.zeromq, global.socket["io-client"], global.http, global.express, global.socket.io, global.uuid, global.nodemailer, global.handlebars, global.bcrypt, global.moment));
5
- })(this, (function (exports, path, bodyParser, jsonwebtoken, tsyringe, routingControllers, socketControllers, routingControllersOpenapi, storage, classValidator, classValidatorJsonschema, child_process, crypto, rxjs, canReportError, fs, mongoose, utils, stream, bson, fontKit_, fileType, sharp_, axios, mongodb, dotenv, nodeCron, zeromq, socket_io_client, http, express_, socket_io, uuid, nodemailer, Handlebars, bcrypt, moment) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('path'), require('body-parser'), require('jsonwebtoken'), require('tsyringe'), require('routing-controllers'), require('socket-controllers'), require('routing-controllers-openapi'), require('class-transformer/storage'), require('class-validator'), require('class-validator-jsonschema'), require('child_process'), require('crypto'), require('rxjs'), require('rxjs/internal/util/canReportError'), require('fs'), require('mongoose'), require('mongoose/lib/utils'), require('stream'), require('bson'), require('fontkit'), require('file-type'), require('sharp'), require('axios'), require('mongodb'), require('dotenv'), require('node-cron'), require('zeromq'), require('rxjs/operators'), require('http'), require('express'), require('socket.io'), require('uuid'), require('nodemailer'), require('handlebars'), require('bcrypt'), require('moment')) :
3
+ typeof define === 'function' && define.amd ? define('@stemy/backend', ['exports', 'path', 'body-parser', 'jsonwebtoken', 'tsyringe', 'routing-controllers', 'socket-controllers', 'routing-controllers-openapi', 'class-transformer/storage', 'class-validator', 'class-validator-jsonschema', 'child_process', 'crypto', 'rxjs', 'rxjs/internal/util/canReportError', 'fs', 'mongoose', 'mongoose/lib/utils', 'stream', 'bson', 'fontkit', 'file-type', 'sharp', 'axios', 'mongodb', 'dotenv', 'node-cron', 'zeromq', 'rxjs/operators', 'http', 'express', 'socket.io', 'uuid', 'nodemailer', 'handlebars', 'bcrypt', 'moment'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.stemy = global.stemy || {}, global.stemy.backend = {}), global.path, global["body-parser"], global.jsonwebtoken, global.tsyringe, global["routing-controllers"], global["socket-controllers"], global["routing-controllers-openapi"], global["class-transformer/storage"], global["class-validator"], global["class-validator-jsonschema"], global.child_process, global.crypto, global.rxjs, global.rxjs["internal/util/canReportError"], global.fs, global.mongoose, global["mongoose-lib-utils"], global.stream, global.bson, global.fontkit, global["file-type"], global.sharp, global.axios, global.mongodb, global.dotenv, global["node-cron"], global.zeromq, global.rxjs.operators, global.http, global.express, global.socket.io, global.uuid, global.nodemailer, global.handlebars, global.bcrypt, global.moment));
5
+ })(this, (function (exports, path, bodyParser, jsonwebtoken, tsyringe, routingControllers, socketControllers, routingControllersOpenapi, storage, classValidator, classValidatorJsonschema, child_process, crypto, rxjs, canReportError, fs, mongoose, utils, stream, bson, fontKit_, fileType, sharp_, axios, mongodb, dotenv, nodeCron, zeromq, operators, http, express_, socket_io, uuid, nodemailer, Handlebars, bcrypt, moment) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
@@ -28,7 +28,6 @@
28
28
  var sharp___default = /*#__PURE__*/_interopDefaultLegacy(sharp_);
29
29
  var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
30
30
  var dotenv__default = /*#__PURE__*/_interopDefaultLegacy(dotenv);
31
- var socket_io_client__default = /*#__PURE__*/_interopDefaultLegacy(socket_io_client);
32
31
  var express___default = /*#__PURE__*/_interopDefaultLegacy(express_);
33
32
  var socket_io__default = /*#__PURE__*/_interopDefaultLegacy(socket_io);
34
33
  var Handlebars__namespace = /*#__PURE__*/_interopNamespace(Handlebars);
@@ -456,6 +455,22 @@
456
455
  function lastItem(value) {
457
456
  return value[value.length - 1];
458
457
  }
458
+ function regroup(value, comparator) {
459
+ var result = [];
460
+ if (!isArray(value) || value.length == 0)
461
+ return result;
462
+ value = Array.from(value);
463
+ result.push([value.shift()]);
464
+ value.forEach(function (item) {
465
+ var group = result.find(function (g) { return g.some(function (a) { return comparator(a, item); }); });
466
+ if (group) {
467
+ group.push(item);
468
+ return;
469
+ }
470
+ result.push([item]);
471
+ });
472
+ return result;
473
+ }
459
474
  function getValue(obj, key, defaultValue, treeFallback) {
460
475
  if (treeFallback === void 0) { treeFallback = false; }
461
476
  key = key || "";
@@ -1100,6 +1115,9 @@
1100
1115
  falseColor: exports.ConsoleColor.FgRed,
1101
1116
  nullColor: exports.ConsoleColor.BgMagenta
1102
1117
  };
1118
+ function colorize(input, color) {
1119
+ return "" + color + input + exports.ConsoleColor.Reset;
1120
+ }
1103
1121
  function jsonHighlight(input, colorOptions) {
1104
1122
  var colors = Object.assign({}, defaultColors, colorOptions);
1105
1123
  var json = (isString(input) ? input : JSON.stringify(input, null, 2)).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
@@ -2206,11 +2224,10 @@
2206
2224
  };
2207
2225
  var LazyAsset = /** @class */ (function (_super) {
2208
2226
  __extends(LazyAsset, _super);
2209
- function LazyAsset(id, data, collection, assets, progresses, jobMan) {
2227
+ function LazyAsset(id, data, collection, assets, progresses) {
2210
2228
  var _this = _super.call(this, id, data, collection) || this;
2211
2229
  _this.assets = assets;
2212
2230
  _this.progresses = progresses;
2213
- _this.jobMan = jobMan;
2214
2231
  return _this;
2215
2232
  }
2216
2233
  Object.defineProperty(LazyAsset.prototype, "jobName", {
@@ -2333,7 +2350,7 @@
2333
2350
  return [4 /*yield*/, this.save()];
2334
2351
  case 2:
2335
2352
  _b.sent();
2336
- return [4 /*yield*/, this.jobMan.enqueueWithName(this.data.jobName, Object.assign(Object.assign({}, this.data.jobParams), { lazyId: this.id, fromLoad: fromLoad }))];
2353
+ return [4 /*yield*/, this.progresses.jobMan.enqueueWithName(this.data.jobName, Object.assign(Object.assign({}, this.data.jobParams), { lazyId: this.id, fromLoad: fromLoad }))];
2337
2354
  case 3:
2338
2355
  _b.sent();
2339
2356
  return [2 /*return*/];
@@ -2389,11 +2406,24 @@
2389
2406
  this.jobs = this.jobTypes.reduce(function (res, jobType) {
2390
2407
  res[getConstructorName(jobType)] = function (jobParams) {
2391
2408
  var job = _this.resolveJobInstance(jobType, jobParams);
2392
- return job.process();
2409
+ return job.process(_this.messageBridge);
2393
2410
  };
2394
2411
  return res;
2395
2412
  }, {});
2413
+ this.messages = new rxjs.Subject();
2414
+ this.messageBridge = {
2415
+ sendMessage: function (message, params) {
2416
+ _this.workerPush.send([message, JSON.stringify(params)]);
2417
+ }
2418
+ };
2419
+ this.processing = false;
2420
+ this.maxTimeout = this.config.resolve("jobTimeout");
2396
2421
  }
2422
+ JobManager.prototype.on = function (message, cb) {
2423
+ return this.messages
2424
+ .pipe(operators.filter(function (t) { return t.message === message; }))
2425
+ .pipe(operators.map(function (t) { return t.params; })).subscribe(cb);
2426
+ };
2397
2427
  JobManager.prototype.process = function (jobType, params) {
2398
2428
  if (params === void 0) { params = {}; }
2399
2429
  return __awaiter$o(this, void 0, void 0, function () {
@@ -2414,44 +2444,16 @@
2414
2444
  JobManager.prototype.enqueueWithName = function (name, params) {
2415
2445
  if (params === void 0) { params = {}; }
2416
2446
  return __awaiter$o(this, void 0, void 0, function () {
2417
- var jobName;
2418
2447
  return __generator(this, function (_a) {
2419
- switch (_a.label) {
2420
- case 0: return [4 /*yield*/, this.tryResolveFromName(name, params)];
2421
- case 1:
2422
- jobName = _a.sent();
2423
- return [2 /*return*/, this.sendToWorkers(jobName, params)];
2424
- }
2448
+ return [2 /*return*/, this.sendToWorkers(this.tryResolveFromName(name, params), params)];
2425
2449
  });
2426
2450
  });
2427
2451
  };
2428
2452
  JobManager.prototype.enqueue = function (jobType, params) {
2429
2453
  if (params === void 0) { params = {}; }
2430
2454
  return __awaiter$o(this, void 0, void 0, function () {
2431
- var jobName;
2432
- return __generator(this, function (_a) {
2433
- switch (_a.label) {
2434
- case 0: return [4 /*yield*/, this.tryResolveAndConnect(jobType, params)];
2435
- case 1:
2436
- jobName = _a.sent();
2437
- return [2 /*return*/, this.sendToWorkers(jobName, params)];
2438
- }
2439
- });
2440
- });
2441
- };
2442
- JobManager.prototype.sendToWorkers = function (jobName, params) {
2443
- return __awaiter$o(this, void 0, void 0, function () {
2444
- var publisher;
2445
2455
  return __generator(this, function (_a) {
2446
- switch (_a.label) {
2447
- case 0: return [4 /*yield*/, this.scheduler];
2448
- case 1:
2449
- publisher = _a.sent();
2450
- return [4 /*yield*/, publisher.send([jobName, JSON.stringify(params), new bson.ObjectId().toHexString()])];
2451
- case 2:
2452
- _a.sent();
2453
- return [2 /*return*/];
2454
- }
2456
+ return [2 /*return*/, this.sendToWorkers(this.tryResolveAndInit(jobType, params), params)];
2455
2457
  });
2456
2458
  });
2457
2459
  };
@@ -2480,47 +2482,71 @@
2480
2482
  });
2481
2483
  };
2482
2484
  JobManager.prototype.startProcessing = function () {
2483
- var _this = this;
2484
- var host = this.config.resolve("zmqRemoteHost");
2485
- this.worker = zeromq.socket("pull");
2486
- this.worker.connect(host);
2487
- this.worker.on("message", function (name, args, uniqueId) { return __awaiter$o(_this, void 0, void 0, function () {
2488
- var jobName, jobParams, timerId, jobNameLog, jobArgsLog, e_1, e_2;
2485
+ return __awaiter$o(this, void 0, void 0, function () {
2486
+ var host, pushHost, pullHost;
2487
+ var _this = this;
2489
2488
  return __generator(this, function (_a) {
2490
2489
  switch (_a.label) {
2491
2490
  case 0:
2492
- _a.trys.push([0, 5, , 6]);
2493
- jobName = name.toString("utf8");
2494
- jobParams = JSON.parse(args.toString("utf8"));
2495
- timerId = uniqueId === null || uniqueId === void 0 ? void 0 : uniqueId.toString("utf8");
2496
- jobNameLog = "\u001B[36m\"" + jobName + "\"\u001B[0m";
2497
- jobArgsLog = "\n" + jsonHighlight(jobParams);
2498
- console.time(timerId);
2499
- console.timeLog(timerId, "Started working on background job: " + jobNameLog + " with args: " + jobArgsLog + "\n\n");
2500
- _a.label = 1;
2491
+ if (this.processing)
2492
+ return [2 /*return*/, null];
2493
+ this.processing = true;
2494
+ if (!this.config.resolve("isWorker")) {
2495
+ console.log(colorize("Processing can not be started because this is NOT a worker process!", exports.ConsoleColor.FgRed));
2496
+ return [2 /*return*/, null];
2497
+ }
2498
+ host = this.config.resolve("zmqRemoteHost");
2499
+ pushHost = host + ":" + this.config.resolve("zmqBackPort");
2500
+ this.workerPush = zeromq.socket("push");
2501
+ return [4 /*yield*/, this.workerPush.connect(pushHost)];
2501
2502
  case 1:
2502
- _a.trys.push([1, 3, , 4]);
2503
- return [4 /*yield*/, Promise.race([this.jobs[jobName](jobParams), promiseTimeout(15000, true)])];
2503
+ _a.sent();
2504
+ console.log("Worker producer connected to: " + pushHost);
2505
+ pullHost = host + ":" + this.config.resolve("zmqPort");
2506
+ this.workerPull = zeromq.socket("pull");
2507
+ return [4 /*yield*/, this.workerPull.connect(pullHost)];
2504
2508
  case 2:
2505
2509
  _a.sent();
2506
- console.timeLog(timerId, "Finished working on background job: " + jobNameLog + "\n\n");
2507
- return [3 /*break*/, 4];
2508
- case 3:
2509
- e_1 = _a.sent();
2510
- console.timeLog(timerId, "Background job failed: " + jobNameLog + "\n" + e_1.message + "\n\n");
2511
- return [3 /*break*/, 4];
2512
- case 4:
2513
- console.timeEnd(timerId);
2514
- return [3 /*break*/, 6];
2515
- case 5:
2516
- e_2 = _a.sent();
2517
- console.log("Failed to start job: " + e_2.message);
2518
- return [3 /*break*/, 6];
2519
- case 6: return [2 /*return*/];
2510
+ console.log("Worker consumer connected to: " + pullHost);
2511
+ this.workerPull.on("message", function (name, args, uniqueId) { return __awaiter$o(_this, void 0, void 0, function () {
2512
+ var jobName, jobParams, timerId, e_1, e_2;
2513
+ return __generator(this, function (_a) {
2514
+ switch (_a.label) {
2515
+ case 0:
2516
+ _a.trys.push([0, 5, , 6]);
2517
+ jobName = name.toString("utf8");
2518
+ jobParams = JSON.parse(args.toString("utf8"));
2519
+ timerId = uniqueId === null || uniqueId === void 0 ? void 0 : uniqueId.toString("utf8");
2520
+ console.time(timerId);
2521
+ console.timeLog(timerId, "Started working on background job: " + colorize(jobName, exports.ConsoleColor.FgCyan) + " with args: \n" + jsonHighlight(jobParams) + "\n\n");
2522
+ this.messageBridge.sendMessage("job-started", { name: jobName });
2523
+ _a.label = 1;
2524
+ case 1:
2525
+ _a.trys.push([1, 3, , 4]);
2526
+ return [4 /*yield*/, Promise.race([this.jobs[jobName](jobParams), promiseTimeout(this.maxTimeout, true)])];
2527
+ case 2:
2528
+ _a.sent();
2529
+ console.timeLog(timerId, "Finished working on background job: " + colorize(jobName, exports.ConsoleColor.FgCyan) + "\n\n");
2530
+ return [3 /*break*/, 4];
2531
+ case 3:
2532
+ e_1 = _a.sent();
2533
+ console.timeLog(timerId, "Background job failed: " + colorize(jobName, exports.ConsoleColor.FgRed) + "\n" + e_1 + "\n\n");
2534
+ return [3 /*break*/, 4];
2535
+ case 4:
2536
+ console.timeEnd(timerId);
2537
+ return [3 /*break*/, 6];
2538
+ case 5:
2539
+ e_2 = _a.sent();
2540
+ console.log("Failed to start job: " + e_2.message);
2541
+ return [3 /*break*/, 6];
2542
+ case 6: return [2 /*return*/];
2543
+ }
2544
+ });
2545
+ }); });
2546
+ return [2 /*return*/];
2520
2547
  }
2521
2548
  });
2522
- }); });
2523
- console.log("Waiting for jobs at: " + host);
2549
+ });
2524
2550
  };
2525
2551
  JobManager.prototype.tryResolve = function (jobType, params) {
2526
2552
  var jobName = getConstructorName(jobType);
@@ -2542,31 +2568,29 @@
2542
2568
  if (!jobType) {
2543
2569
  throw "Can't find job type with name: " + jobName + " so it can't be enqueued!";
2544
2570
  }
2545
- return this.tryResolveAndConnect(jobType, params);
2571
+ return this.tryResolveAndInit(jobType, params);
2546
2572
  };
2547
- JobManager.prototype.tryResolveAndConnect = function (jobType, params) {
2548
- return __awaiter$o(this, void 0, void 0, function () {
2549
- var _this = this;
2550
- return __generator(this, function (_a) {
2551
- this.scheduler = this.scheduler || new Promise(function (resolve) { return __awaiter$o(_this, void 0, void 0, function () {
2552
- var port, publisher;
2553
- return __generator(this, function (_a) {
2554
- switch (_a.label) {
2555
- case 0:
2556
- port = this.config.resolve("zmqPort");
2557
- publisher = zeromq.socket("push");
2558
- return [4 /*yield*/, publisher.bind("tcp://0.0.0.0:" + port)];
2559
- case 1:
2560
- _a.sent();
2561
- console.log("Publisher bound to port: " + port);
2562
- resolve(publisher);
2563
- return [2 /*return*/];
2564
- }
2565
- });
2566
- }); });
2567
- return [2 /*return*/, this.tryResolve(jobType, params)];
2573
+ JobManager.prototype.tryResolveAndInit = function (jobType, params) {
2574
+ var _this = this;
2575
+ if (!this.apiPush) {
2576
+ var port = this.config.resolve("zmqPort");
2577
+ this.apiPush = zeromq.socket("push");
2578
+ this.apiPush.bind("tcp://0.0.0.0:" + port);
2579
+ console.log("API producer bound to port: " + port);
2580
+ }
2581
+ if (!this.apiPull) {
2582
+ var backPort = this.config.resolve("zmqBackPort");
2583
+ this.apiPull = zeromq.socket("pull");
2584
+ this.apiPull.bind("tcp://0.0.0.0:" + backPort);
2585
+ this.apiPull.on("message", function (name, args) {
2586
+ var message = name.toString("utf8");
2587
+ var params = JSON.parse((args === null || args === void 0 ? void 0 : args.toString("utf8")) || "{}");
2588
+ console.log("Received a message from worker: \"" + colorize(message, exports.ConsoleColor.FgCyan) + "\" with args: " + jsonHighlight(params) + "\n\n");
2589
+ _this.messages.next({ message: message, params: params });
2568
2590
  });
2569
- });
2591
+ console.log("API consumer bound to port: " + backPort);
2592
+ }
2593
+ return this.tryResolve(jobType, params);
2570
2594
  };
2571
2595
  JobManager.prototype.resolveJobInstance = function (jobType, params) {
2572
2596
  var container = this.container.createChildContainer();
@@ -2576,6 +2600,22 @@
2576
2600
  container.register(jobType, jobType);
2577
2601
  return container.resolve(jobType);
2578
2602
  };
2603
+ JobManager.prototype.sendToWorkers = function (jobName, params) {
2604
+ return __awaiter$o(this, void 0, void 0, function () {
2605
+ var publisher;
2606
+ return __generator(this, function (_a) {
2607
+ switch (_a.label) {
2608
+ case 0: return [4 /*yield*/, this.apiPush];
2609
+ case 1:
2610
+ publisher = _a.sent();
2611
+ return [4 /*yield*/, publisher.send([jobName, JSON.stringify(params), new bson.ObjectId().toHexString()])];
2612
+ case 2:
2613
+ _a.sent();
2614
+ return [2 /*return*/];
2615
+ }
2616
+ });
2617
+ });
2618
+ };
2579
2619
  return JobManager;
2580
2620
  }());
2581
2621
  exports.JobManager = __decorate$s([
@@ -2606,10 +2646,8 @@
2606
2646
  };
2607
2647
  var Progress = /** @class */ (function (_super) {
2608
2648
  __extends(Progress, _super);
2609
- function Progress(id, data, collection, client) {
2610
- var _this = _super.call(this, id, data, collection) || this;
2611
- _this.client = client;
2612
- return _this;
2649
+ function Progress(id, data, collection) {
2650
+ return _super.call(this, id, data, collection) || this;
2613
2651
  }
2614
2652
  Object.defineProperty(Progress.prototype, "current", {
2615
2653
  get: function () {
@@ -2660,6 +2698,10 @@
2660
2698
  enumerable: false,
2661
2699
  configurable: true
2662
2700
  });
2701
+ Progress.prototype.setMessageBridge = function (messageBridge) {
2702
+ this.messageBridge = messageBridge || this.messageBridge;
2703
+ return this;
2704
+ };
2663
2705
  Progress.prototype.createSubProgress = function (progressValue, max, message) {
2664
2706
  return __awaiter$n(this, void 0, void 0, function () {
2665
2707
  return __generator(this, function (_a) {
@@ -2745,9 +2787,6 @@
2745
2787
  return [4 /*yield*/, this.save()];
2746
2788
  case 2:
2747
2789
  _a.sent();
2748
- if (!this.client)
2749
- return [2 /*return*/];
2750
- this.client.emit("background-progress", this.id);
2751
2790
  return [2 /*return*/];
2752
2791
  }
2753
2792
  });
@@ -2767,6 +2806,12 @@
2767
2806
  });
2768
2807
  });
2769
2808
  };
2809
+ Progress.prototype.save = function () {
2810
+ if (this.messageBridge) {
2811
+ this.messageBridge.sendMessage("progress-changed", this.toJSON());
2812
+ }
2813
+ return _super.prototype.save.call(this);
2814
+ };
2770
2815
  return Progress;
2771
2816
  }(BaseEntity));
2772
2817
  var SubProgress = /** @class */ (function () {
@@ -2840,6 +2885,12 @@
2840
2885
  enumerable: false,
2841
2886
  configurable: true
2842
2887
  });
2888
+ SubProgress.prototype.setMessageBridge = function (messageBridge) {
2889
+ if (!this.parent)
2890
+ return this;
2891
+ this.parent.setMessageBridge(messageBridge);
2892
+ return this;
2893
+ };
2843
2894
  SubProgress.prototype.createSubProgress = function (progressValue, max, message) {
2844
2895
  return __awaiter$n(this, void 0, void 0, function () {
2845
2896
  return __generator(this, function (_a) {
@@ -3006,44 +3057,48 @@
3006
3057
  step((generator = generator.apply(thisArg, _arguments || [])).next());
3007
3058
  });
3008
3059
  };
3009
- var socketIOClient = socket_io_client__default["default"];
3010
3060
  exports.Progresses = /** @class */ (function () {
3011
- function Progresses(connector, config) {
3061
+ function Progresses(connector, jobMan) {
3062
+ var _this = this;
3012
3063
  this.connector = connector;
3013
- this.config = config;
3014
- var mainEndpoint = this.config.resolve("mainEndpoint");
3015
- this.client = !mainEndpoint ? null : socketIOClient(mainEndpoint, { path: "/socket" });
3064
+ this.jobMan = jobMan;
3016
3065
  this.collection = connector.database.collection("progresses");
3066
+ this.progresses = {};
3067
+ this.jobMan.on("progress-changed", function (progress) {
3068
+ var id = progress.id;
3069
+ _this.progresses[id] = new Progress(new bson.ObjectId(id), progress, _this.collection);
3070
+ });
3017
3071
  }
3018
3072
  Progresses.prototype.waitToFinish = function (id) {
3019
3073
  return __awaiter$m(this, void 0, void 0, function () {
3020
- var isFinished, progress;
3074
+ var _this = this;
3021
3075
  return __generator(this, function (_a) {
3022
- switch (_a.label) {
3023
- case 0:
3024
- isFinished = false;
3025
- progress = null;
3026
- _a.label = 1;
3027
- case 1:
3028
- if (!!isFinished) return [3 /*break*/, 5];
3029
- return [4 /*yield*/, this.get(id)];
3030
- case 2:
3031
- progress = _a.sent();
3032
- if (!progress) {
3033
- throw "Progress does not exists with id: " + id;
3034
- }
3035
- if (progress.error) {
3036
- throw progress.error;
3037
- }
3038
- isFinished = progress.percent == 100;
3039
- if (!!isFinished) return [3 /*break*/, 4];
3040
- return [4 /*yield*/, promiseTimeout(50)];
3041
- case 3:
3042
- _a.sent();
3043
- _a.label = 4;
3044
- case 4: return [3 /*break*/, 1];
3045
- case 5: return [2 /*return*/, progress];
3046
- }
3076
+ return [2 /*return*/, Promise.race([
3077
+ this.waitForProgress(id, function () { return __awaiter$m(_this, void 0, void 0, function () {
3078
+ var progress;
3079
+ return __generator(this, function (_a) {
3080
+ switch (_a.label) {
3081
+ case 0:
3082
+ progress = this.progresses[id];
3083
+ if (!(!progress || progress.percent < 100)) return [3 /*break*/, 2];
3084
+ return [4 /*yield*/, this.get(id)];
3085
+ case 1:
3086
+ progress = _a.sent();
3087
+ _a.label = 2;
3088
+ case 2:
3089
+ if (!progress) {
3090
+ throw new Error("Progress does not exists with id: " + id);
3091
+ }
3092
+ return [2 /*return*/, progress];
3093
+ }
3094
+ });
3095
+ }); }, 500),
3096
+ this.waitForProgress(id, function () { return __awaiter$m(_this, void 0, void 0, function () {
3097
+ return __generator(this, function (_a) {
3098
+ return [2 /*return*/, this.progresses[id] || null];
3099
+ });
3100
+ }); }, 25)
3101
+ ])];
3047
3102
  });
3048
3103
  });
3049
3104
  };
@@ -3062,7 +3117,7 @@
3062
3117
  case 0: return [4 /*yield*/, this.collection.findOne(where)];
3063
3118
  case 1:
3064
3119
  data = _a.sent();
3065
- return [2 /*return*/, !data ? null : new Progress(data._id, data, this.collection, this.client)];
3120
+ return [2 /*return*/, !data ? null : new Progress(data._id, data, this.collection)];
3066
3121
  }
3067
3122
  });
3068
3123
  });
@@ -3075,7 +3130,7 @@
3075
3130
  switch (_a.label) {
3076
3131
  case 0:
3077
3132
  if (isNaN(max) || max <= 0) {
3078
- throw "Max progress value must be bigger than zero";
3133
+ throw new Error("Max progress value must be bigger than zero");
3079
3134
  }
3080
3135
  data = {
3081
3136
  current: 0,
@@ -3087,7 +3142,7 @@
3087
3142
  return [4 /*yield*/, this.collection.insertOne(data)];
3088
3143
  case 1:
3089
3144
  res = _a.sent();
3090
- return [2 /*return*/, new Progress(res.insertedId, data, this.collection, this.client)];
3145
+ return [2 /*return*/, new Progress(res.insertedId, data, this.collection)];
3091
3146
  }
3092
3147
  });
3093
3148
  });
@@ -3104,12 +3159,48 @@
3104
3159
  });
3105
3160
  });
3106
3161
  };
3162
+ Progresses.prototype.waitForProgress = function (id, cb, delay) {
3163
+ return __awaiter$m(this, void 0, void 0, function () {
3164
+ var isFinished, progress, waitTime;
3165
+ return __generator(this, function (_a) {
3166
+ switch (_a.label) {
3167
+ case 0:
3168
+ isFinished = false;
3169
+ progress = null;
3170
+ waitTime = 0;
3171
+ _a.label = 1;
3172
+ case 1:
3173
+ if (!!isFinished) return [3 /*break*/, 5];
3174
+ return [4 /*yield*/, cb()];
3175
+ case 2:
3176
+ progress = _a.sent();
3177
+ waitTime += delay;
3178
+ if (progress) {
3179
+ if (progress.error) {
3180
+ throw new Error(progress.error);
3181
+ }
3182
+ isFinished = progress.percent >= 100;
3183
+ }
3184
+ if (!!isFinished) return [3 /*break*/, 4];
3185
+ if (waitTime >= this.jobMan.maxTimeout) {
3186
+ throw new Error("Progress with id: " + id + " probably never will be finished!");
3187
+ }
3188
+ return [4 /*yield*/, promiseTimeout(delay)];
3189
+ case 3:
3190
+ _a.sent();
3191
+ _a.label = 4;
3192
+ case 4: return [3 /*break*/, 1];
3193
+ case 5: return [2 /*return*/, progress];
3194
+ }
3195
+ });
3196
+ });
3197
+ };
3107
3198
  return Progresses;
3108
3199
  }());
3109
3200
  exports.Progresses = __decorate$r([
3110
3201
  tsyringe.injectable(),
3111
3202
  tsyringe.singleton(),
3112
- __metadata$l("design:paramtypes", [exports.MongoConnector, exports.Configuration])
3203
+ __metadata$l("design:paramtypes", [exports.MongoConnector, exports.JobManager])
3113
3204
  ], exports.Progresses);
3114
3205
 
3115
3206
  var __decorate$q = (this && this.__decorate) || function (decorators, target, key, desc) {
@@ -3175,7 +3266,7 @@
3175
3266
  return [4 /*yield*/, this.collection.insertOne(data)];
3176
3267
  case 2:
3177
3268
  res = _a.sent();
3178
- return [2 /*return*/, new LazyAsset(res.insertedId, data, this.collection, this.assets, this.progresses, this.jobMan)];
3269
+ return [2 /*return*/, new LazyAsset(res.insertedId, data, this.collection, this.assets, this.progresses)];
3179
3270
  }
3180
3271
  });
3181
3272
  });
@@ -3197,7 +3288,7 @@
3197
3288
  data = _a.sent();
3198
3289
  return [2 /*return*/, !data
3199
3290
  ? null
3200
- : new LazyAsset(data._id, data, this.collection, this.assets, this.progresses, this.jobMan)];
3291
+ : new LazyAsset(data._id, data, this.collection, this.assets, this.progresses)];
3201
3292
  }
3202
3293
  });
3203
3294
  });
@@ -3222,8 +3313,10 @@
3222
3313
  exports.LazyAssets = __decorate$q([
3223
3314
  tsyringe.injectable(),
3224
3315
  tsyringe.scoped(tsyringe.Lifecycle.ContainerScoped),
3225
- __metadata$k("design:paramtypes", [exports.MongoConnector, exports.Assets,
3226
- exports.Progresses, exports.JobManager])
3316
+ __metadata$k("design:paramtypes", [exports.MongoConnector,
3317
+ exports.Assets,
3318
+ exports.Progresses,
3319
+ exports.JobManager])
3227
3320
  ], exports.LazyAssets);
3228
3321
 
3229
3322
  var __decorate$p = (this && this.__decorate) || function (decorators, target, key, desc) {
@@ -5720,7 +5813,7 @@
5720
5813
  enumerable: false,
5721
5814
  configurable: true
5722
5815
  });
5723
- LazyAssetGenerator.prototype.process = function () {
5816
+ LazyAssetGenerator.prototype.process = function (messaging) {
5724
5817
  return __awaiter$1(this, void 0, void 0, function () {
5725
5818
  var lazyAsset, progress, asset, e_1;
5726
5819
  return __generator(this, function (_a) {
@@ -5733,10 +5826,11 @@
5733
5826
  progress = _a.sent();
5734
5827
  if (!progress || progress.canceled)
5735
5828
  return [2 /*return*/, null];
5829
+ progress.setMessageBridge(messaging);
5736
5830
  _a.label = 3;
5737
5831
  case 3:
5738
5832
  _a.trys.push([3, 7, , 9]);
5739
- return [4 /*yield*/, this.generate(progress)];
5833
+ return [4 /*yield*/, this.generate(progress, messaging)];
5740
5834
  case 4:
5741
5835
  asset = _a.sent();
5742
5836
  return [4 /*yield*/, progress.load()];
@@ -5834,10 +5928,11 @@
5834
5928
  new Parameter("mongoDb", "node-backend"),
5835
5929
  new Parameter("mongoUser", null),
5836
5930
  new Parameter("mongoPassword", null),
5837
- new Parameter("nodeEnv", "development"),
5931
+ new Parameter("nodeEnv", "production"),
5838
5932
  new Parameter("appPort", 80),
5839
5933
  new Parameter("zmqPort", 3000),
5840
- new Parameter("zmqRemoteHost", "tcp://127.0.0.1:3000"),
5934
+ new Parameter("zmqBackPort", 3100),
5935
+ new Parameter("zmqRemoteHost", "tcp://127.0.0.1"),
5841
5936
  new Parameter("isWorker", false),
5842
5937
  new Parameter("mainEndpoint", ""),
5843
5938
  new Parameter("idChars", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
@@ -5845,6 +5940,7 @@
5845
5940
  new Parameter("idPrefix", "ID-"),
5846
5941
  new Parameter("idParts", [4, 4]),
5847
5942
  new Parameter("jsonLimit", "250mb"),
5943
+ new Parameter("jobTimeout", 5 * 60 * 1000),
5848
5944
  new Parameter("cacheCollection", "cache"),
5849
5945
  ];
5850
5946
  // Convert parameters to providers
@@ -6095,6 +6191,7 @@
6095
6191
  exports.assign = assign;
6096
6192
  exports.broadcast = broadcast;
6097
6193
  exports.bufferToStream = bufferToStream;
6194
+ exports.colorize = colorize;
6098
6195
  exports.convertValue = convertValue;
6099
6196
  exports.copy = copy;
6100
6197
  exports.copyStream = copyStream;
@@ -6145,6 +6242,7 @@
6145
6242
  exports.random = random;
6146
6243
  exports.readAndDeleteFile = readAndDeleteFile;
6147
6244
  exports.readFile = readFile;
6245
+ exports.regroup = regroup;
6148
6246
  exports.runCommand = runCommand;
6149
6247
  exports.setupBackend = setupBackend;
6150
6248
  exports.streamToBuffer = streamToBuffer;