nats-micro
Advanced tools
Comparing version 0.22.0 to 0.23.0
@@ -28,3 +28,3 @@ "use strict"; | ||
class Monitor { | ||
constructor(broker, systemBroker) { | ||
constructor(broker, systemBroker, options = {}) { | ||
this.broker = broker; | ||
@@ -35,2 +35,3 @@ this.systemBroker = systemBroker; | ||
this.ee = new events_1.default(); | ||
this.options = Object.assign({ discoveryTimeout: 5000 }, options); | ||
const handleServiceRegistration = (0, utils_js_1.wrapMethod)(this.broker, (0, utils_js_1.wrapThread)('monitor', this.handleServiceRegistration.bind(this)), 'handleServiceStatus'); | ||
@@ -45,6 +46,4 @@ broker.on(index_js_1.MicroserviceRegistrationSubject, handleServiceRegistration); | ||
} | ||
this.discoverConnections() | ||
.then(() => { | ||
this.discover(30000); | ||
}); | ||
this.discoverConnections(); | ||
this.discover(this.options.discoveryTimeout); | ||
} | ||
@@ -89,8 +88,11 @@ discoverConnections() { | ||
}; | ||
for (const service of this.services) { | ||
const clientId = this.getServiceClientId(service); | ||
if (clientId) { | ||
const conn = this.connections[clientId]; | ||
if (conn) | ||
service.connection = conn; | ||
} | ||
for (const service of this.services) { | ||
const clientId = this.getServiceClientId(service); | ||
if (clientId) { | ||
const conn = this.connections[clientId]; | ||
if (conn) { | ||
service.connection = conn; | ||
debug_js_1.debug.monitor.info(`Updated microservice ${service.name}.${service.id} to client ${clientId}'s connection`); | ||
this.emit('added', service); | ||
} | ||
@@ -161,11 +163,10 @@ } | ||
getServiceConnectionInfo(service) { | ||
const clientId = Number(service.metadata['_nats.client.id']); | ||
if (Number.isNaN(clientId)) | ||
return undefined; | ||
return this.connections[clientId]; | ||
const clientId = this.getServiceClientId(service); | ||
return clientId ? this.connections[clientId] : undefined; | ||
} | ||
saveService(service) { | ||
const idx = this.services.findIndex((svc) => svc.id === service.id); | ||
const clientId = this.getServiceClientId(service); | ||
const connection = this.getServiceConnectionInfo(service); | ||
debug_js_1.debug.monitor.info(`${idx >= 0 ? 'Updated' : 'New'} microservice ${service.name}.${service.id} on client ${connection === null || connection === void 0 ? void 0 : connection.client.id}`); | ||
debug_js_1.debug.monitor.info(`${idx >= 0 ? 'Updated' : 'New'} microservice ${service.name}.${service.id} on client ${clientId}${connection ? '' : ' (unknown connection)'}`); | ||
if (idx >= 0) { | ||
@@ -193,3 +194,3 @@ this.services[idx] = Object.assign(Object.assign(Object.assign({}, this.services[idx]), service), { lastFoundAt: new Date() }); | ||
limit: -1, | ||
timeout, | ||
timeout: timeout !== null && timeout !== void 0 ? timeout : this.options.discoveryTimeout, | ||
}); | ||
@@ -204,2 +205,3 @@ const services = []; | ||
services.push(service.data); | ||
this.saveService(service.data); | ||
} | ||
@@ -218,4 +220,2 @@ finally { | ||
} | ||
for (const service of services) | ||
this.saveService(service); | ||
if (!(options === null || options === void 0 ? void 0 : options.doNotClear)) { | ||
@@ -225,3 +225,3 @@ const servicesToForget = this.services | ||
if (servicesToForget.length > 0) { | ||
debug_js_1.debug.monitor.info(`Removing ${servicesToForget} microservices that haven't responded during discovery`); | ||
debug_js_1.debug.monitor.info(`Removing microservices ${servicesToForget.map((svc) => `${svc.name}.${svc.id}`)} that have not responded during discovery`); | ||
for (const serviceToForger of servicesToForget) | ||
@@ -228,0 +228,0 @@ this.removeService(serviceToForger); |
@@ -85,3 +85,3 @@ "use strict"; | ||
]; | ||
broker.send(msg.replyTo, 'undefined', { | ||
broker.send(msg.replyTo, undefined, { | ||
headers, | ||
@@ -88,0 +88,0 @@ }); |
@@ -22,3 +22,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
export class Monitor { | ||
constructor(broker, systemBroker) { | ||
constructor(broker, systemBroker, options = {}) { | ||
this.broker = broker; | ||
@@ -29,2 +29,3 @@ this.systemBroker = systemBroker; | ||
this.ee = new EventEmitter(); | ||
this.options = Object.assign({ discoveryTimeout: 5000 }, options); | ||
const handleServiceRegistration = wrapMethod(this.broker, wrapThread('monitor', this.handleServiceRegistration.bind(this)), 'handleServiceStatus'); | ||
@@ -39,6 +40,4 @@ broker.on(MicroserviceRegistrationSubject, handleServiceRegistration); | ||
} | ||
this.discoverConnections() | ||
.then(() => { | ||
this.discover(30000); | ||
}); | ||
this.discoverConnections(); | ||
this.discover(this.options.discoveryTimeout); | ||
} | ||
@@ -83,8 +82,11 @@ discoverConnections() { | ||
}; | ||
for (const service of this.services) { | ||
const clientId = this.getServiceClientId(service); | ||
if (clientId) { | ||
const conn = this.connections[clientId]; | ||
if (conn) | ||
service.connection = conn; | ||
} | ||
for (const service of this.services) { | ||
const clientId = this.getServiceClientId(service); | ||
if (clientId) { | ||
const conn = this.connections[clientId]; | ||
if (conn) { | ||
service.connection = conn; | ||
debug.monitor.info(`Updated microservice ${service.name}.${service.id} to client ${clientId}'s connection`); | ||
this.emit('added', service); | ||
} | ||
@@ -155,11 +157,10 @@ } | ||
getServiceConnectionInfo(service) { | ||
const clientId = Number(service.metadata['_nats.client.id']); | ||
if (Number.isNaN(clientId)) | ||
return undefined; | ||
return this.connections[clientId]; | ||
const clientId = this.getServiceClientId(service); | ||
return clientId ? this.connections[clientId] : undefined; | ||
} | ||
saveService(service) { | ||
const idx = this.services.findIndex((svc) => svc.id === service.id); | ||
const clientId = this.getServiceClientId(service); | ||
const connection = this.getServiceConnectionInfo(service); | ||
debug.monitor.info(`${idx >= 0 ? 'Updated' : 'New'} microservice ${service.name}.${service.id} on client ${connection === null || connection === void 0 ? void 0 : connection.client.id}`); | ||
debug.monitor.info(`${idx >= 0 ? 'Updated' : 'New'} microservice ${service.name}.${service.id} on client ${clientId}${connection ? '' : ' (unknown connection)'}`); | ||
if (idx >= 0) { | ||
@@ -187,3 +188,3 @@ this.services[idx] = Object.assign(Object.assign(Object.assign({}, this.services[idx]), service), { lastFoundAt: new Date() }); | ||
limit: -1, | ||
timeout, | ||
timeout: timeout !== null && timeout !== void 0 ? timeout : this.options.discoveryTimeout, | ||
}); | ||
@@ -198,2 +199,3 @@ const services = []; | ||
services.push(service.data); | ||
this.saveService(service.data); | ||
} | ||
@@ -212,4 +214,2 @@ finally { | ||
} | ||
for (const service of services) | ||
this.saveService(service); | ||
if (!(options === null || options === void 0 ? void 0 : options.doNotClear)) { | ||
@@ -219,3 +219,3 @@ const servicesToForget = this.services | ||
if (servicesToForget.length > 0) { | ||
debug.monitor.info(`Removing ${servicesToForget} microservices that haven't responded during discovery`); | ||
debug.monitor.info(`Removing microservices ${servicesToForget.map((svc) => `${svc.name}.${svc.id}`)} that have not responded during discovery`); | ||
for (const serviceToForger of servicesToForget) | ||
@@ -222,0 +222,0 @@ this.removeService(serviceToForger); |
@@ -78,3 +78,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
]; | ||
broker.send(msg.replyTo, 'undefined', { | ||
broker.send(msg.replyTo, undefined, { | ||
headers, | ||
@@ -81,0 +81,0 @@ }); |
@@ -27,5 +27,9 @@ import { Broker } from './broker.js'; | ||
}; | ||
export type MonitorOptions = { | ||
discoveryTimeout: number; | ||
}; | ||
export declare class Monitor { | ||
private readonly broker; | ||
private readonly systemBroker?; | ||
private readonly options; | ||
readonly services: DiscoveredMicroservice[]; | ||
@@ -35,3 +39,3 @@ private discoveryInterval; | ||
private readonly ee; | ||
constructor(broker: Broker, systemBroker?: Broker | undefined); | ||
constructor(broker: Broker, systemBroker?: Broker | undefined, options?: Partial<MonitorOptions>); | ||
private discoverConnections; | ||
@@ -48,6 +52,6 @@ private handleServiceRegistration; | ||
private removeService; | ||
discover(timeout: number, options?: Partial<MonitorDiscoveryOptions>): Promise<void>; | ||
startPeriodicDiscovery(interval: number, discoveryTimeout: number): void; | ||
discover(timeout?: number, options?: Partial<MonitorDiscoveryOptions>): Promise<void>; | ||
startPeriodicDiscovery(interval: number, discoveryTimeout?: number): void; | ||
stopPeriodicDiscovery(): void; | ||
} | ||
export {}; |
{ | ||
"name": "nats-micro", | ||
"version": "0.22.0", | ||
"version": "0.23.0", | ||
"description": "NATS micro compatible extra-lightweight microservice library", | ||
@@ -5,0 +5,0 @@ "main": "lib/cjs/index.js", |
@@ -75,4 +75,10 @@ import EventEmitter from 'events'; | ||
export type MonitorOptions = { | ||
discoveryTimeout: number, | ||
} | ||
export class Monitor { | ||
private readonly options: MonitorOptions; | ||
public readonly services: DiscoveredMicroservice[] = []; | ||
@@ -86,3 +92,9 @@ private discoveryInterval: NodeJS.Timer | undefined; | ||
private readonly systemBroker?: Broker, | ||
options: Partial<MonitorOptions> = {}, | ||
) { | ||
this.options = { | ||
discoveryTimeout: 5000, | ||
...options, | ||
}; | ||
const handleServiceRegistration = wrapMethod(this.broker, wrapThread('monitor', this.handleServiceRegistration.bind(this)), 'handleServiceStatus'); | ||
@@ -99,6 +111,4 @@ broker.on(MicroserviceRegistrationSubject, handleServiceRegistration); | ||
this.discoverConnections() | ||
.then(() => { | ||
this.discover(30000); | ||
}); | ||
this.discoverConnections(); | ||
this.discover(this.options.discoveryTimeout); | ||
} | ||
@@ -151,9 +161,12 @@ | ||
}; | ||
} | ||
for (const service of this.services) { | ||
const clientId = this.getServiceClientId(service); | ||
if (clientId) { | ||
const conn = this.connections[clientId]; | ||
if (conn) | ||
service.connection = conn; | ||
for (const service of this.services) { | ||
const clientId = this.getServiceClientId(service); | ||
if (clientId) { | ||
const conn = this.connections[clientId]; | ||
if (conn) { | ||
service.connection = conn; | ||
debug.monitor.info(`Updated microservice ${service.name}.${service.id} to client ${clientId}'s connection`); | ||
this.emit('added', service); | ||
} | ||
@@ -223,7 +236,5 @@ } | ||
private getServiceConnectionInfo(service: MicroserviceInfo): UserConnectEvent | undefined { | ||
const clientId = Number(service.metadata['_nats.client.id']); | ||
if (Number.isNaN(clientId)) | ||
return undefined; | ||
const clientId = this.getServiceClientId(service); | ||
return this.connections[clientId]; | ||
return clientId ? this.connections[clientId] : undefined; | ||
} | ||
@@ -233,5 +244,6 @@ | ||
const idx = this.services.findIndex((svc) => svc.id === service.id); | ||
const clientId = this.getServiceClientId(service); | ||
const connection = this.getServiceConnectionInfo(service); | ||
debug.monitor.info(`${idx >= 0 ? 'Updated' : 'New'} microservice ${service.name}.${service.id} on client ${connection?.client.id}`); | ||
debug.monitor.info(`${idx >= 0 ? 'Updated' : 'New'} microservice ${service.name}.${service.id} on client ${clientId}${connection ? '' : ' (unknown connection)'}`); | ||
@@ -272,3 +284,3 @@ if (idx >= 0) { | ||
public async discover( | ||
timeout: number, | ||
timeout?: number, | ||
options?: Partial<MonitorDiscoveryOptions>, | ||
@@ -282,3 +294,3 @@ ): Promise<void> { | ||
limit: -1, | ||
timeout, | ||
timeout: timeout ?? this.options.discoveryTimeout, | ||
}, | ||
@@ -288,8 +300,7 @@ ); | ||
const services: MicroserviceInfo[] = []; | ||
for await (const service of servicesIterator) | ||
for await (const service of servicesIterator) { | ||
services.push(service.data); | ||
this.saveService(service.data); | ||
} | ||
for (const service of services) | ||
this.saveService(service); | ||
if (!options?.doNotClear) { | ||
@@ -301,3 +312,3 @@ | ||
if (servicesToForget.length > 0) { | ||
debug.monitor.info(`Removing ${servicesToForget} microservices that haven't responded during discovery`); | ||
debug.monitor.info(`Removing microservices ${servicesToForget.map((svc) => `${svc.name}.${svc.id}`)} that have not responded during discovery`); | ||
for (const serviceToForger of servicesToForget) | ||
@@ -311,3 +322,3 @@ this.removeService(serviceToForger); | ||
interval: number, | ||
discoveryTimeout: number, | ||
discoveryTimeout?: number, | ||
) { | ||
@@ -314,0 +325,0 @@ this.stopPeriodicDiscovery(); |
@@ -111,3 +111,3 @@ import { threadContext } from 'debug-threads-ns'; | ||
msg.replyTo, | ||
'undefined', | ||
undefined, | ||
{ | ||
@@ -114,0 +114,0 @@ headers, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
313818
5525