UNPKG

ziron-worker

Version:
1 lines 3.92 kB
"use strict";var __awaiter=this&&this.__awaiter||function(e,t,i,r){return new(i||(i=Promise))((function(n,o){function s(e){try{a(r.next(e))}catch(e){o(e)}}function l(e){try{a(r.throw(e))}catch(e){o(e)}}function a(e){var t;e.done?n(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(s,l)}a((r=r.apply(e,t||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0});const Utils_1=require("./Utils"),StateClient_1=require("./StateClient"),BrokerClusterClient_1=require("./externalBroker/BrokerClusterClient"),ziron_server_1=require("ziron-server"),Logger_1=require("./Logger");class WorkerServer extends ziron_server_1.Server{get leader(){var e,t;return null!==(t=null===(e=this.stateClient)||void 0===e?void 0:e.leader)&&void 0!==t&&t}get shared(){var e,t;return null===(t=null===(e=this.stateClient)||void 0===e?void 0:e.sessionShared)||void 0===t?void 0:t.payload}get brokerClients(){var e,t;return null!==(t=null===(e=this.brokerClusterClient)||void 0===e?void 0:e.brokerClients)&&void 0!==t?t:[]}constructor(e={}){var t,i,r;super(e),this._logger=new Logger_1.default(null!==(t=e.logLevel)&&void 0!==t?t:Logger_1.LogLevel.Everything),this._rawJoinToken=e.join||null,this.brokerClusterClientMaxPoolSize=e.brokerClusterClientMaxPoolSize||12,this.clusterJoinPayload=e.clusterJoinPayload||{},this.clusterShared=e.clusterShared,this.clusterShareAuth=void 0===e.clusterShareAuth||e.clusterShareAuth,this.joinToken=(0,Utils_1.parseJoinToken)(this._rawJoinToken||""),this.stateClient=this._setUpStateClient(),null===(i=this.stateClient)||void 0===i||i.on("sessionSharedUpdate",(e,t)=>{if((0,Utils_1.deepEqual)(e.payload,t.payload)||this.emitter.emit("sharedChange",e.payload),this.clusterShareAuth&&e.auth&&!(0,Utils_1.deepEqual)(e.auth,t.auth)){const t=e.auth;try{this.auth.updateOptions({algorithm:t.algorithm,publicKey:t.publicKey,privateKey:t.privateKey})}catch(e){this.emitter.emit("error",(0,Utils_1.ensureError)(e))}}}),null===(r=this.stateClient)||void 0===r||r.on("leadershipChange",e=>{this.emitter.emit("leadershipChange",e)}),null!=this.stateClient&&(this.brokerClusterClient=new BrokerClusterClient_1.default(this.stateClient,this.internalBroker,{joinTokenSecret:this.joinToken.secret,maxClientPoolSize:this.brokerClusterClientMaxPoolSize}),this.brokerClusterClient.on("brokerClientPoolsUpdate",()=>this.emitter.emit("brokerClientsUpdate")),this.internalBroker.externalBrokerClient=this.brokerClusterClient)}isConnectedToState(){var e;return!!(null===(e=this.stateClient)||void 0===e?void 0:e.connected)}join(){return __awaiter(this,void 0,void 0,(function*(){var e;if(null==this._rawJoinToken)throw new Error("Join token was not provided.");return null===(e=this.stateClient)||void 0===e?void 0:e.join()}))}listen(){const e=Object.create(null,{isListening:{get:()=>super.isListening},listen:{get:()=>super.listen}});return __awaiter(this,void 0,void 0,(function*(){if(!e.isListening.call(this))try{this._logger.logBusy("Launching worker server..."),yield e.listen.call(this),this._logger.logActive(`Worker server launched successfully on port: ${this.port}.`)}catch(e){throw e instanceof ziron_server_1.FailedToListenError&&this._logger.logFailed(`Failed to listen on port: ${this.port}. Maybe the port is already in use.`),e}}))}joinAndListen(){return __awaiter(this,void 0,void 0,(function*(){yield this.join(),yield this.listen()}))}_setUpStateClient(){if(null==this._rawJoinToken)return;const e=this.auth.options;return new StateClient_1.default({id:this.id,port:this.port,path:this.path,joinTokenUri:this.joinToken.uri,joinTokenSecret:this.joinToken.secret,joinPayload:this.clusterJoinPayload,sharedData:{payload:this.clusterShared,auth:this.clusterShareAuth?{algorithm:e.algorithm,publicKey:e.publicKey,privateKey:e.privateKey}:void 0}},this._logger)}terminate(){var e;super.terminate(),null===(e=this.stateClient)||void 0===e||e.terminate()}}exports.default=WorkerServer;