UNPKG

container.ts

Version:
111 lines 4.96 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var lodash_1 = require("lodash"); var net = require("net"); var RxJS_1 = require("../../container/RxJS"); var Scripts_1 = require("./Scripts"); /** Node.js scripts with server interface. */ var ScriptsNet = /** @class */ (function (_super) { __extends(ScriptsNet, _super); function ScriptsNet(options) { var _this = _super.call(this, options) || this; /** Scripts server for connecting workers. */ _this.server = net.createServer(); _this.close$ = RxJS_1.Observable.fromEvent(_this.server, "close"); _this.connection$ = RxJS_1.Observable.fromEvent(_this.server, "connection"); _this.error$ = RxJS_1.Observable.fromEvent(_this.server, "error"); // Log server error events. _this.error$.subscribe(function (error) { return _this.log.error(new Scripts_1.ScriptsError(error)); }); return _this; } Object.defineProperty(ScriptsNet.prototype, "port", { /** Get server port number. */ get: function () { var address = this.server.address(); return address.port; }, enumerable: true, configurable: true }); ScriptsNet.prototype.moduleUp = function () { var _this = this; var up$ = _super.prototype.moduleUp.call(this) || RxJS_1.Observable.of(undefined); var listen$ = RxJS_1.Observable.bindNodeCallback(this.server.listen.bind(this.server))(); return RxJS_1.Observable.forkJoin(up$, listen$).map(function () { _this.log.info(ScriptsNet.LOG.UP, { port: _this.port }); }); }; ScriptsNet.prototype.moduleDown = function () { this.server.close(); this.log.info(ScriptsNet.LOG.DOWN); return _super.prototype.moduleDown.call(this); }; // TODO(HIGH): Fix worker restarts when using ScriptsNet module. ScriptsNet.prototype.startWorker = function (name, target, options) { var _this = this; if (options === void 0) { options = {}; } // Create socket connection to server and start worker when connected. return this.createConnection() .switchMap(function (sockets) { options.sockets = sockets; return _super.prototype.startWorker.call(_this, name, target, options); }); }; ScriptsNet.prototype.connectWorkers = function (channel, one, two) { var _this = this; var workerOne = this.workers[one]; var workerTwo = this.workers[two]; if ((workerOne != null) && (workerTwo != null)) { var observable = RxJS_1.Observable.combineLatest(workerOne.next$, workerTwo.next$) .switchMap(function (processes) { return RxJS_1.Observable.forkJoin(RxJS_1.Observable.of(processes), _this.createConnection()); }) .switchMap(function (_a) { var processes = _a[0], sockets = _a[1]; return RxJS_1.Observable.forkJoin(processes[0].sendChannel(channel, sockets.parent), processes[1].sendChannel(channel, sockets.child)); }) .map(function (_a) { var c1 = _a[0], c2 = _a[1]; return c1 && c2; }) .takeUntil(this.close$) .share(); observable.subscribe(function (connected) { _this.log.info(ScriptsNet.LOG.CONNECTION, { connected: connected }); }); return observable.take(1); } return RxJS_1.Observable.of(false); }; ScriptsNet.prototype.createConnection = function () { var parentSocket$ = this.connection$.take(1); var childSocket = net.createConnection(this.port); var childSocket$ = RxJS_1.Observable.fromEvent(childSocket, "connect").take(1); return RxJS_1.Observable.forkJoin(parentSocket$, childSocket$) .map(function (_a) { var parent = _a[0]; return ({ parent: parent, child: childSocket }); }); }; /** Default module name. */ ScriptsNet.moduleName = "Scripts"; /** Log names. */ ScriptsNet.LOG = lodash_1.assign({}, Scripts_1.Scripts.LOG, { UP: "ScriptsNetUp", DOWN: "ScriptsNetDown", CONNECTION: "ScriptsNetWorkerConnection", }); return ScriptsNet; }(Scripts_1.Scripts)); exports.ScriptsNet = ScriptsNet; //# sourceMappingURL=ScriptsNet.js.map