@stemy/backend 3.0.1 → 3.1.0

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);
@@ -1100,6 +1099,10 @@
1100
1099
  falseColor: exports.ConsoleColor.FgRed,
1101
1100
  nullColor: exports.ConsoleColor.BgMagenta
1102
1101
  };
1102
+ var MAX_TIMEOUT = 120000;
1103
+ function colorize(input, color) {
1104
+ return "" + color + input + exports.ConsoleColor.Reset;
1105
+ }
1103
1106
  function jsonHighlight(input, colorOptions) {
1104
1107
  var colors = Object.assign({}, defaultColors, colorOptions);
1105
1108
  var json = (isString(input) ? input : JSON.stringify(input, null, 2)).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
@@ -2206,11 +2209,10 @@
2206
2209
  };
2207
2210
  var LazyAsset = /** @class */ (function (_super) {
2208
2211
  __extends(LazyAsset, _super);
2209
- function LazyAsset(id, data, collection, assets, progresses, jobMan) {
2212
+ function LazyAsset(id, data, collection, assets, progresses) {
2210
2213
  var _this = _super.call(this, id, data, collection) || this;
2211
2214
  _this.assets = assets;
2212
2215
  _this.progresses = progresses;
2213
- _this.jobMan = jobMan;
2214
2216
  return _this;
2215
2217
  }
2216
2218
  Object.defineProperty(LazyAsset.prototype, "jobName", {
@@ -2333,7 +2335,7 @@
2333
2335
  return [4 /*yield*/, this.save()];
2334
2336
  case 2:
2335
2337
  _b.sent();
2336
- return [4 /*yield*/, this.jobMan.enqueueWithName(this.data.jobName, Object.assign(Object.assign({}, this.data.jobParams), { lazyId: this.id, fromLoad: fromLoad }))];
2338
+ return [4 /*yield*/, this.progresses.jobMan.enqueueWithName(this.data.jobName, Object.assign(Object.assign({}, this.data.jobParams), { lazyId: this.id, fromLoad: fromLoad }))];
2337
2339
  case 3:
2338
2340
  _b.sent();
2339
2341
  return [2 /*return*/];
@@ -2389,11 +2391,23 @@
2389
2391
  this.jobs = this.jobTypes.reduce(function (res, jobType) {
2390
2392
  res[getConstructorName(jobType)] = function (jobParams) {
2391
2393
  var job = _this.resolveJobInstance(jobType, jobParams);
2392
- return job.process();
2394
+ return job.process(_this.messageBridge);
2393
2395
  };
2394
2396
  return res;
2395
2397
  }, {});
2398
+ this.messages = new rxjs.Subject();
2399
+ this.messageBridge = {
2400
+ sendMessage: function (message, params) {
2401
+ _this.workerPush.send([message, JSON.stringify(params)]);
2402
+ }
2403
+ };
2404
+ this.processing = false;
2396
2405
  }
2406
+ JobManager.prototype.on = function (message, cb) {
2407
+ return this.messages
2408
+ .pipe(operators.filter(function (t) { return t.message === message; }))
2409
+ .pipe(operators.map(function (t) { return t.params; })).subscribe(cb);
2410
+ };
2397
2411
  JobManager.prototype.process = function (jobType, params) {
2398
2412
  if (params === void 0) { params = {}; }
2399
2413
  return __awaiter$o(this, void 0, void 0, function () {
@@ -2414,44 +2428,16 @@
2414
2428
  JobManager.prototype.enqueueWithName = function (name, params) {
2415
2429
  if (params === void 0) { params = {}; }
2416
2430
  return __awaiter$o(this, void 0, void 0, function () {
2417
- var jobName;
2418
2431
  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
- }
2432
+ return [2 /*return*/, this.sendToWorkers(this.tryResolveFromName(name, params), params)];
2425
2433
  });
2426
2434
  });
2427
2435
  };
2428
2436
  JobManager.prototype.enqueue = function (jobType, params) {
2429
2437
  if (params === void 0) { params = {}; }
2430
2438
  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
2439
  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
- }
2440
+ return [2 /*return*/, this.sendToWorkers(this.tryResolveAndInit(jobType, params), params)];
2455
2441
  });
2456
2442
  });
2457
2443
  };
@@ -2480,47 +2466,73 @@
2480
2466
  });
2481
2467
  };
2482
2468
  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;
2469
+ return __awaiter$o(this, void 0, void 0, function () {
2470
+ var host, pushHost, pullHost;
2471
+ var _this = this;
2489
2472
  return __generator(this, function (_a) {
2490
2473
  switch (_a.label) {
2491
2474
  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;
2475
+ if (this.processing)
2476
+ return [2 /*return*/, null];
2477
+ this.processing = true;
2478
+ if (!this.config.resolve("isWorker")) {
2479
+ console.log(colorize("Processing can not be started because this is NOT a worker process!", exports.ConsoleColor.FgRed));
2480
+ return [2 /*return*/, null];
2481
+ }
2482
+ host = this.config.resolve("zmqRemoteHost");
2483
+ pushHost = host + ":" + this.config.resolve("zmqBackPort");
2484
+ this.workerPush = zeromq.socket("push");
2485
+ return [4 /*yield*/, this.workerPush.connect(pushHost)];
2501
2486
  case 1:
2502
- _a.trys.push([1, 3, , 4]);
2503
- return [4 /*yield*/, Promise.race([this.jobs[jobName](jobParams), promiseTimeout(15000, true)])];
2487
+ _a.sent();
2488
+ console.log("Worker producer connected to: " + pushHost);
2489
+ pullHost = host + ":" + this.config.resolve("zmqPort");
2490
+ this.workerPull = zeromq.socket("pull");
2491
+ return [4 /*yield*/, this.workerPull.connect(pullHost)];
2504
2492
  case 2:
2505
2493
  _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*/];
2494
+ console.log("Worker consumer connected to: " + pullHost);
2495
+ this.workerPull.on("message", function (name, args, uniqueId) { return __awaiter$o(_this, void 0, void 0, function () {
2496
+ var jobName, jobParams, timerId, jobNameLog, jobArgsLog, e_1, e_2;
2497
+ return __generator(this, function (_a) {
2498
+ switch (_a.label) {
2499
+ case 0:
2500
+ _a.trys.push([0, 5, , 6]);
2501
+ jobName = name.toString("utf8");
2502
+ jobParams = JSON.parse(args.toString("utf8"));
2503
+ timerId = uniqueId === null || uniqueId === void 0 ? void 0 : uniqueId.toString("utf8");
2504
+ jobNameLog = "\u001B[36m\"" + jobName + "\"\u001B[0m";
2505
+ jobArgsLog = "\n" + jsonHighlight(jobParams);
2506
+ console.time(timerId);
2507
+ console.timeLog(timerId, "Started working on background job: " + jobNameLog + " with args: " + jobArgsLog + "\n\n");
2508
+ this.messageBridge.sendMessage("job-started", { name: jobName });
2509
+ _a.label = 1;
2510
+ case 1:
2511
+ _a.trys.push([1, 3, , 4]);
2512
+ return [4 /*yield*/, Promise.race([this.jobs[jobName](jobParams), promiseTimeout(MAX_TIMEOUT, true)])];
2513
+ case 2:
2514
+ _a.sent();
2515
+ console.timeLog(timerId, "Finished working on background job: " + jobNameLog + "\n\n");
2516
+ return [3 /*break*/, 4];
2517
+ case 3:
2518
+ e_1 = _a.sent();
2519
+ console.timeLog(timerId, "Background job failed: " + jobNameLog + "\n" + e_1.message + "\n\n");
2520
+ return [3 /*break*/, 4];
2521
+ case 4:
2522
+ console.timeEnd(timerId);
2523
+ return [3 /*break*/, 6];
2524
+ case 5:
2525
+ e_2 = _a.sent();
2526
+ console.log("Failed to start job: " + e_2.message);
2527
+ return [3 /*break*/, 6];
2528
+ case 6: return [2 /*return*/];
2529
+ }
2530
+ });
2531
+ }); });
2532
+ return [2 /*return*/];
2520
2533
  }
2521
2534
  });
2522
- }); });
2523
- console.log("Waiting for jobs at: " + host);
2535
+ });
2524
2536
  };
2525
2537
  JobManager.prototype.tryResolve = function (jobType, params) {
2526
2538
  var jobName = getConstructorName(jobType);
@@ -2542,31 +2554,29 @@
2542
2554
  if (!jobType) {
2543
2555
  throw "Can't find job type with name: " + jobName + " so it can't be enqueued!";
2544
2556
  }
2545
- return this.tryResolveAndConnect(jobType, params);
2557
+ return this.tryResolveAndInit(jobType, params);
2546
2558
  };
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)];
2559
+ JobManager.prototype.tryResolveAndInit = function (jobType, params) {
2560
+ var _this = this;
2561
+ if (!this.apiPush) {
2562
+ var port = this.config.resolve("zmqPort");
2563
+ this.apiPush = zeromq.socket("push");
2564
+ this.apiPush.bind("tcp://0.0.0.0:" + port);
2565
+ console.log("API producer bound to port: " + port);
2566
+ }
2567
+ if (!this.apiPull) {
2568
+ var backPort = this.config.resolve("zmqBackPort");
2569
+ this.apiPull = zeromq.socket("pull");
2570
+ this.apiPull.bind("tcp://0.0.0.0:" + backPort);
2571
+ this.apiPull.on("message", function (name, args) {
2572
+ var message = name.toString("utf8");
2573
+ var params = JSON.parse((args === null || args === void 0 ? void 0 : args.toString("utf8")) || "{}");
2574
+ console.log("Received a message from worker: \"" + colorize(message, exports.ConsoleColor.FgCyan) + "\" with args: " + jsonHighlight(params) + "\n\n");
2575
+ _this.messages.next({ message: message, params: params });
2568
2576
  });
2569
- });
2577
+ console.log("API consumer bound to port: " + backPort);
2578
+ }
2579
+ return this.tryResolve(jobType, params);
2570
2580
  };
2571
2581
  JobManager.prototype.resolveJobInstance = function (jobType, params) {
2572
2582
  var container = this.container.createChildContainer();
@@ -2576,6 +2586,22 @@
2576
2586
  container.register(jobType, jobType);
2577
2587
  return container.resolve(jobType);
2578
2588
  };
2589
+ JobManager.prototype.sendToWorkers = function (jobName, params) {
2590
+ return __awaiter$o(this, void 0, void 0, function () {
2591
+ var publisher;
2592
+ return __generator(this, function (_a) {
2593
+ switch (_a.label) {
2594
+ case 0: return [4 /*yield*/, this.apiPush];
2595
+ case 1:
2596
+ publisher = _a.sent();
2597
+ return [4 /*yield*/, publisher.send([jobName, JSON.stringify(params), new bson.ObjectId().toHexString()])];
2598
+ case 2:
2599
+ _a.sent();
2600
+ return [2 /*return*/];
2601
+ }
2602
+ });
2603
+ });
2604
+ };
2579
2605
  return JobManager;
2580
2606
  }());
2581
2607
  exports.JobManager = __decorate$s([
@@ -2606,10 +2632,8 @@
2606
2632
  };
2607
2633
  var Progress = /** @class */ (function (_super) {
2608
2634
  __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;
2635
+ function Progress(id, data, collection) {
2636
+ return _super.call(this, id, data, collection) || this;
2613
2637
  }
2614
2638
  Object.defineProperty(Progress.prototype, "current", {
2615
2639
  get: function () {
@@ -2660,6 +2684,10 @@
2660
2684
  enumerable: false,
2661
2685
  configurable: true
2662
2686
  });
2687
+ Progress.prototype.setMessageBridge = function (messageBridge) {
2688
+ this.messageBridge = messageBridge || this.messageBridge;
2689
+ return this;
2690
+ };
2663
2691
  Progress.prototype.createSubProgress = function (progressValue, max, message) {
2664
2692
  return __awaiter$n(this, void 0, void 0, function () {
2665
2693
  return __generator(this, function (_a) {
@@ -2745,9 +2773,6 @@
2745
2773
  return [4 /*yield*/, this.save()];
2746
2774
  case 2:
2747
2775
  _a.sent();
2748
- if (!this.client)
2749
- return [2 /*return*/];
2750
- this.client.emit("background-progress", this.id);
2751
2776
  return [2 /*return*/];
2752
2777
  }
2753
2778
  });
@@ -2767,6 +2792,12 @@
2767
2792
  });
2768
2793
  });
2769
2794
  };
2795
+ Progress.prototype.save = function () {
2796
+ if (this.messageBridge) {
2797
+ this.messageBridge.sendMessage("progress-changed", this.toJSON());
2798
+ }
2799
+ return _super.prototype.save.call(this);
2800
+ };
2770
2801
  return Progress;
2771
2802
  }(BaseEntity));
2772
2803
  var SubProgress = /** @class */ (function () {
@@ -2840,6 +2871,12 @@
2840
2871
  enumerable: false,
2841
2872
  configurable: true
2842
2873
  });
2874
+ SubProgress.prototype.setMessageBridge = function (messageBridge) {
2875
+ if (!this.parent)
2876
+ return this;
2877
+ this.parent.setMessageBridge(messageBridge);
2878
+ return this;
2879
+ };
2843
2880
  SubProgress.prototype.createSubProgress = function (progressValue, max, message) {
2844
2881
  return __awaiter$n(this, void 0, void 0, function () {
2845
2882
  return __generator(this, function (_a) {
@@ -3006,44 +3043,48 @@
3006
3043
  step((generator = generator.apply(thisArg, _arguments || [])).next());
3007
3044
  });
3008
3045
  };
3009
- var socketIOClient = socket_io_client__default["default"];
3010
3046
  exports.Progresses = /** @class */ (function () {
3011
- function Progresses(connector, config) {
3047
+ function Progresses(connector, jobMan) {
3048
+ var _this = this;
3012
3049
  this.connector = connector;
3013
- this.config = config;
3014
- var mainEndpoint = this.config.resolve("mainEndpoint");
3015
- this.client = !mainEndpoint ? null : socketIOClient(mainEndpoint, { path: "/socket" });
3050
+ this.jobMan = jobMan;
3016
3051
  this.collection = connector.database.collection("progresses");
3052
+ this.progresses = {};
3053
+ this.jobMan.on("progress-changed", function (progress) {
3054
+ var id = progress.id;
3055
+ _this.progresses[id] = new Progress(new bson.ObjectId(id), progress, _this.collection);
3056
+ });
3017
3057
  }
3018
3058
  Progresses.prototype.waitToFinish = function (id) {
3019
3059
  return __awaiter$m(this, void 0, void 0, function () {
3020
- var isFinished, progress;
3060
+ var _this = this;
3021
3061
  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
- }
3062
+ return [2 /*return*/, Promise.race([
3063
+ this.waitForProgress(id, function () { return __awaiter$m(_this, void 0, void 0, function () {
3064
+ var progress;
3065
+ return __generator(this, function (_a) {
3066
+ switch (_a.label) {
3067
+ case 0:
3068
+ progress = this.progresses[id];
3069
+ if (!(!progress || progress.percent < 100)) return [3 /*break*/, 2];
3070
+ return [4 /*yield*/, this.get(id)];
3071
+ case 1:
3072
+ progress = _a.sent();
3073
+ _a.label = 2;
3074
+ case 2:
3075
+ if (!progress) {
3076
+ throw new Error("Progress does not exists with id: " + id);
3077
+ }
3078
+ return [2 /*return*/, progress];
3079
+ }
3080
+ });
3081
+ }); }, 500),
3082
+ this.waitForProgress(id, function () { return __awaiter$m(_this, void 0, void 0, function () {
3083
+ return __generator(this, function (_a) {
3084
+ return [2 /*return*/, this.progresses[id] || null];
3085
+ });
3086
+ }); }, 25)
3087
+ ])];
3047
3088
  });
3048
3089
  });
3049
3090
  };
@@ -3062,7 +3103,7 @@
3062
3103
  case 0: return [4 /*yield*/, this.collection.findOne(where)];
3063
3104
  case 1:
3064
3105
  data = _a.sent();
3065
- return [2 /*return*/, !data ? null : new Progress(data._id, data, this.collection, this.client)];
3106
+ return [2 /*return*/, !data ? null : new Progress(data._id, data, this.collection)];
3066
3107
  }
3067
3108
  });
3068
3109
  });
@@ -3075,7 +3116,7 @@
3075
3116
  switch (_a.label) {
3076
3117
  case 0:
3077
3118
  if (isNaN(max) || max <= 0) {
3078
- throw "Max progress value must be bigger than zero";
3119
+ throw new Error("Max progress value must be bigger than zero");
3079
3120
  }
3080
3121
  data = {
3081
3122
  current: 0,
@@ -3087,7 +3128,7 @@
3087
3128
  return [4 /*yield*/, this.collection.insertOne(data)];
3088
3129
  case 1:
3089
3130
  res = _a.sent();
3090
- return [2 /*return*/, new Progress(res.insertedId, data, this.collection, this.client)];
3131
+ return [2 /*return*/, new Progress(res.insertedId, data, this.collection)];
3091
3132
  }
3092
3133
  });
3093
3134
  });
@@ -3104,12 +3145,48 @@
3104
3145
  });
3105
3146
  });
3106
3147
  };
3148
+ Progresses.prototype.waitForProgress = function (id, cb, delay) {
3149
+ return __awaiter$m(this, void 0, void 0, function () {
3150
+ var isFinished, progress, waitTime;
3151
+ return __generator(this, function (_a) {
3152
+ switch (_a.label) {
3153
+ case 0:
3154
+ isFinished = false;
3155
+ progress = null;
3156
+ waitTime = 0;
3157
+ _a.label = 1;
3158
+ case 1:
3159
+ if (!!isFinished) return [3 /*break*/, 5];
3160
+ return [4 /*yield*/, cb()];
3161
+ case 2:
3162
+ progress = _a.sent();
3163
+ waitTime += delay;
3164
+ if (progress) {
3165
+ if (progress.error) {
3166
+ throw new Error(progress.error);
3167
+ }
3168
+ isFinished = progress.percent >= 100;
3169
+ }
3170
+ if (!!isFinished) return [3 /*break*/, 4];
3171
+ if (waitTime >= MAX_TIMEOUT) {
3172
+ throw new Error("Progress with id: " + id + " probably never will be finished!");
3173
+ }
3174
+ return [4 /*yield*/, promiseTimeout(delay)];
3175
+ case 3:
3176
+ _a.sent();
3177
+ _a.label = 4;
3178
+ case 4: return [3 /*break*/, 1];
3179
+ case 5: return [2 /*return*/, progress];
3180
+ }
3181
+ });
3182
+ });
3183
+ };
3107
3184
  return Progresses;
3108
3185
  }());
3109
3186
  exports.Progresses = __decorate$r([
3110
3187
  tsyringe.injectable(),
3111
3188
  tsyringe.singleton(),
3112
- __metadata$l("design:paramtypes", [exports.MongoConnector, exports.Configuration])
3189
+ __metadata$l("design:paramtypes", [exports.MongoConnector, exports.JobManager])
3113
3190
  ], exports.Progresses);
3114
3191
 
3115
3192
  var __decorate$q = (this && this.__decorate) || function (decorators, target, key, desc) {
@@ -3175,7 +3252,7 @@
3175
3252
  return [4 /*yield*/, this.collection.insertOne(data)];
3176
3253
  case 2:
3177
3254
  res = _a.sent();
3178
- return [2 /*return*/, new LazyAsset(res.insertedId, data, this.collection, this.assets, this.progresses, this.jobMan)];
3255
+ return [2 /*return*/, new LazyAsset(res.insertedId, data, this.collection, this.assets, this.progresses)];
3179
3256
  }
3180
3257
  });
3181
3258
  });
@@ -3197,7 +3274,7 @@
3197
3274
  data = _a.sent();
3198
3275
  return [2 /*return*/, !data
3199
3276
  ? null
3200
- : new LazyAsset(data._id, data, this.collection, this.assets, this.progresses, this.jobMan)];
3277
+ : new LazyAsset(data._id, data, this.collection, this.assets, this.progresses)];
3201
3278
  }
3202
3279
  });
3203
3280
  });
@@ -3222,8 +3299,10 @@
3222
3299
  exports.LazyAssets = __decorate$q([
3223
3300
  tsyringe.injectable(),
3224
3301
  tsyringe.scoped(tsyringe.Lifecycle.ContainerScoped),
3225
- __metadata$k("design:paramtypes", [exports.MongoConnector, exports.Assets,
3226
- exports.Progresses, exports.JobManager])
3302
+ __metadata$k("design:paramtypes", [exports.MongoConnector,
3303
+ exports.Assets,
3304
+ exports.Progresses,
3305
+ exports.JobManager])
3227
3306
  ], exports.LazyAssets);
3228
3307
 
3229
3308
  var __decorate$p = (this && this.__decorate) || function (decorators, target, key, desc) {
@@ -5720,7 +5799,7 @@
5720
5799
  enumerable: false,
5721
5800
  configurable: true
5722
5801
  });
5723
- LazyAssetGenerator.prototype.process = function () {
5802
+ LazyAssetGenerator.prototype.process = function (messaging) {
5724
5803
  return __awaiter$1(this, void 0, void 0, function () {
5725
5804
  var lazyAsset, progress, asset, e_1;
5726
5805
  return __generator(this, function (_a) {
@@ -5733,10 +5812,11 @@
5733
5812
  progress = _a.sent();
5734
5813
  if (!progress || progress.canceled)
5735
5814
  return [2 /*return*/, null];
5815
+ progress.setMessageBridge(messaging);
5736
5816
  _a.label = 3;
5737
5817
  case 3:
5738
5818
  _a.trys.push([3, 7, , 9]);
5739
- return [4 /*yield*/, this.generate(progress)];
5819
+ return [4 /*yield*/, this.generate(progress, messaging)];
5740
5820
  case 4:
5741
5821
  asset = _a.sent();
5742
5822
  return [4 /*yield*/, progress.load()];
@@ -5837,7 +5917,8 @@
5837
5917
  new Parameter("nodeEnv", "development"),
5838
5918
  new Parameter("appPort", 80),
5839
5919
  new Parameter("zmqPort", 3000),
5840
- new Parameter("zmqRemoteHost", "tcp://127.0.0.1:3000"),
5920
+ new Parameter("zmqBackPort", 3100),
5921
+ new Parameter("zmqRemoteHost", "tcp://127.0.0.1"),
5841
5922
  new Parameter("isWorker", false),
5842
5923
  new Parameter("mainEndpoint", ""),
5843
5924
  new Parameter("idChars", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
@@ -6095,6 +6176,7 @@
6095
6176
  exports.assign = assign;
6096
6177
  exports.broadcast = broadcast;
6097
6178
  exports.bufferToStream = bufferToStream;
6179
+ exports.colorize = colorize;
6098
6180
  exports.convertValue = convertValue;
6099
6181
  exports.copy = copy;
6100
6182
  exports.copyStream = copyStream;