ziron-worker
Version:
1 lines • 4.29 kB
JavaScript
"use strict";var __awaiter=this&&this.__awaiter||function(t,e,i,s){return new(i||(i=Promise))((function(r,o){function n(t){try{h(s.next(t))}catch(t){o(t)}}function a(t){try{h(s.throw(t))}catch(t){o(t)}}function h(t){var e;t.done?r(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(n,a)}h((s=s.apply(t,e||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0});const ziron_client_1=require("ziron-client"),emitix_1=require("emitix"),ip_1=require("ip"),Utils_1=require("./Utils"),ClusterVersion_1=require("./ClusterVersion");class StateClient{get connected(){var t;return null===(t=this._stateSocket)||void 0===t?void 0:t.isConnected()}invoke(t,e,i={}){return this._stateSocket.invoke(t,e,i)}transmit(t,e,i={}){return this._stateSocket.transmit(t,e,i)}get brokers(){return this._currentBrokerUpdate.uris}constructor(t,e){this.options=t,this._logger=e,this._localEmitter=new emitix_1.default,this.once=this._localEmitter.once.bind(this._localEmitter),this.on=this._localEmitter.on.bind(this._localEmitter),this.off=this._localEmitter.off.bind(this._localEmitter),this._emit=this._localEmitter.emit.bind(this._localEmitter),this._initJoinCalled=!1,this.initJoined=!1,this.initJoin=new Promise((t,e)=>{this.initJoinResolve=t,this.initJoinReject=e}),this.sessionShared={},this.clusterSessionId="/",this.leader=!1,this._currentBrokerUpdate={time:-1,uris:[]},this._joinData={shared:t.sharedData,payload:t.joinPayload};const i=new ziron_client_1.Socket(t.joinTokenUri,{responseTimeout:3e3,connectTimeout:3e3,autoReconnect:{active:!0,initialDelay:1e3,randomness:1e3,multiplier:1,maxDelay:2e3},handshakeAttachment:{secret:t.joinTokenSecret,clusterVersion:ClusterVersion_1.CLUSTER_VERSION,node:{id:t.id,type:0,ip:(0,ip_1.address)(),port:t.port,path:t.path}}});i.on("error",t=>{this._logger.logError("Error in state socket: "+t.stack),this._emit("error",new Error("Error in state socket: "+t.stack))}),i.procedures.addLeadership=(t,e)=>{this._updateLeadership(!0),e()},i.receivers.updateBrokers=t=>{this._handleBrokerUpdate(t)},i.on("disconnect",()=>{this.stateId=void 0,this._updateLeadership(!1)});const s=()=>__awaiter(this,void 0,void 0,(function*(){try{yield this._invokeJoin(),this._logger.logInfo(`Worker has ${this.initJoined?"re":""}joined the cluster.`),this.initJoined=!0,this.initJoinResolve()}catch(t){const e=(0,Utils_1.ensureError)(t);if(this.initJoinReject(e),"IdAlreadyUsedInClusterError"===e.name?this._logger.logWarning(`Attempt to join the cluster failed, the server-id: "${this.options.id}" already exists in the cluster.`):e.stack&&this._logger.logError(`Attempt to join the cluster failed: ${e.stack}.`),!i.isConnected())return;this._invokeJoinRetryTicker=setTimeout(s,2e3),this._emit("error",e)}}));i.on("connect",t=>{t&&(this.stateId=t),clearTimeout(this._invokeJoinRetryTicker),s()}),this._stateSocket=i}_invokeJoin(){return __awaiter(this,void 0,void 0,(function*(){const t=yield this._stateSocket.invoke("#join",this._joinData);null!=t.brokers&&this._handleBrokerUpdate(t.brokers),this._updateClusterSessionId(t.session.id),this._updateClusterSessionShared(t.session.shared)}))}join(){return __awaiter(this,void 0,void 0,(function*(){if(this._initJoinCalled)throw new Error("Join should only be invoked once. The server will automatically retry to rejoin the cluster in case of disconnection.");return this._initJoinCalled=!0,this._stateSocket.connect().catch(t=>{this.initJoinReject(t),this._logger.logError(`Attempt to join the cluster failed: ${t.stack}.`)}),this.initJoin}))}_handleBrokerUpdate(t){if(this._currentBrokerUpdate.time<=t.time){const e=this._currentBrokerUpdate;this._currentBrokerUpdate=t,(0,Utils_1.arrayContentEquals)(e.uris,this._currentBrokerUpdate.uris)||this._emit("brokersChange",this._currentBrokerUpdate.uris)}}_updateLeadership(t){const e=this.leader;this.leader=t,e!==this.leader&&this._emit("leadershipChange",this.leader)}_updateClusterSessionId(t){const e=this.clusterSessionId;this.clusterSessionId=t,e!==this.clusterSessionId&&this._emit("sessionIdChange",this.clusterSessionId)}_updateClusterSessionShared(t){const e=this.sessionShared;this.sessionShared=t,this._emit("sessionSharedUpdate",t,e)}terminate(){this._stateSocket.disconnect(),clearTimeout(this._invokeJoinRetryTicker)}}exports.default=StateClient;