/** * Send data buffer. * * @param {String} topic * @param {Buffer} data * @param {Object} meta * * @returns {Promise} */ send(topic, data) { /* istanbul ignore next*/ if (!this.client) return this.broker.Promise.resolve(); return new this.broker.Promise(resolve => { this.client.publish(topic, data, resolve); }); }
nats.publish( 'math', JSON.stringify({
disconnect(cb = noop) { this[storage].close(noop) this.transports[shutdown](() => { this.nats.publish('skyring:node', { node: this[kNode] , type: EVENT_STATUS.SHUTDOWN }, noop) this.nats.drainSubscription(this._sid, (err) => { if (err) return cb(err) this.nats.quit(cb) }) }) }
nc.publish(subject, msg, function() { console.log('Published [' + subject + '] : "' + msg + '"'); process.exit(); });
nats.on('connect', () => { nats.subscribe("ping", (msg)=>{ console.log("Server1 got a ping:", msg); nats.publish("pong", "Hello from server1"); }); });
/** * Starts an internal nats queue * @method module:skyring/lib/timer#watch * @param {String} key The name of the nats queue to create * @param {Nodeback} callback Node style callback to execute when the function has finished execution **/ watch(key, cb) { if (this._bail) return const opts = {queue: key} this._sid = this.nats.subscribe(REBALANCE_SUB, opts, (data, reply) => { if (reply) this.nats.publish(reply, {node: this[kNode], timer: data.id}) if (this._bail) return cb(null, data) }) return this._sid }
recover(cb = noop) { this.nats.publish('skyring:node', { node: this[kNode] , type: EVENT_STATUS.RECOVERY }, noop) const fn = (data) => { store.trace('recover timer %s', data.key) const out = Object.assign({}, data.value.payload, { id: data.value.id , created: data.value.created }) // pass noop to `create` so the single callback isn't called multiple times this.create(data.key, out, noop) } const stream = this[storage].createReadStream() stream .on('data', fn) .once('close', function() { store.trace('recover stream close') stream.removeListener('data', fn) // call the callback when the stream is processed + complete cb() }) }
/** * Clears the respective timer from storage and publishes a cancelled event via nats * @method module:skyring/lib/timer#cancelled * @param {String} id the is of the time to acknowledge as delivered successfully * @param {Nodeback} [callback] Callback to execute when the acknowledge is complete * @example timers.cancel('2e2f6dad-9678-4caf-bc41-8e62ca07d551') **/ cancel(id, cb = noop) { this[kRemove](id, (err) => { if (err) { log.error(err) return cb(err) } this.nats.publish('skyring:events', { type: EVENT_STATUS.CANCELLED , timer: id , node: this[kNode] }, cb) }) }
/** * Clears the respective timer from storage and publishes a success event via nats * @method module:skyring/lib/timer#success * @param {String} id the is of the time to acknowledge as delivered successfully * @param {Nodeback} [callback] Callback to execute when the acknowledge is complete * @example timers.success('2e2f6dad-9678-4caf-bc41-8e62ca07d551') **/ success(id, cb = noop) { this[kRemove](id, (err) => { if (err) { log.error(err) cb(err) return } this.nats.publish('skyring:events', { type: EVENT_STATUS.SUCCESS , timer: id , node: this[kNode] }, cb) }) }
disconnect(cb = noop) { this[storage].close(noop) this.transports[shutdown](() => { this.nats.publish('skyring:node', { node: this[kNode] , type: EVENT_STATUS.SHUTDOWN }, noop) this.nats.drainSubscription(this._sid, (err) => { if (err) return cb(err) this.nats.quit(cb) }) }) }
nc.publish(subject, msg, function() { console.log('Published [' + subject + '] : "' + msg + '"'); process.exit(); });
/** * Starts an internal nats queue * @method module:skyring/lib/timer#watch * @param {String} key The name of the nats queue to create * @param {Nodeback} callback Node style callback to execute when the function has finished execution **/ watch(key, cb) { if (this._bail) return const opts = {queue: key} this._sid = this.nats.subscribe(REBALANCE_SUB, opts, (data, reply) => { if (reply) this.nats.publish(reply, {node: this[kNode], timer: data.id}) if (this._bail) return cb(null, data) }) return this._sid }
/** * Clears the respective timer from storage and publishes a cancelled event via nats * @method module:skyring/lib/timer#cancelled * @param {String} id the is of the time to acknowledge as delivered successfully * @param {Nodeback} [callback] Callback to execute when the acknowledge is complete * @example timers.cancel('2e2f6dad-9678-4caf-bc41-8e62ca07d551') **/ cancel(id, cb = noop) { this[kRemove](id, (err) => { if (err) { log.error(err) return cb(err) } this.nats.publish('skyring:events', { type: EVENT_STATUS.CANCELLED , timer: id , node: this[kNode] }, cb) }) }
/** * Clears the respective timer from storage and publishes a success event via nats * @method module:skyring/lib/timer#success * @param {String} id the is of the time to acknowledge as delivered successfully * @param {Nodeback} [callback] Callback to execute when the acknowledge is complete * @example timers.success('2e2f6dad-9678-4caf-bc41-8e62ca07d551') **/ success(id, cb = noop) { this[kRemove](id, (err) => { if (err) { log.error(err) cb(err) return } this.nats.publish('skyring:events', { type: EVENT_STATUS.SUCCESS , timer: id , node: this[kNode] }, cb) }) }