@logzio-node-toolbox/consul
Advanced tools
Comparing version 0.0.11 to 0.0.12
@@ -35,3 +35,3 @@ interface AnyObject { | ||
export interface RegisterOptionsInner { | ||
export interface RegisterRetryOptions { | ||
factor?: number; | ||
@@ -49,8 +49,9 @@ retries?: number; | ||
} | ||
export interface RegisterOptions { | ||
data: RegisterData; | ||
validateRegisteredInterval?: number; | ||
export interface RegisterIntervalOptions { | ||
registerData: RegisterData; | ||
registerRetryOptions?: RegisterRetryOptions; | ||
interval: number; | ||
onError?: VoidFunction; | ||
registerOptions?: RegisterOptionsInner; | ||
} | ||
export declare class Consul { | ||
@@ -64,4 +65,5 @@ public constructor(consulOptions: ConsulOptions); | ||
public merge(key: string, values: AnyObject): AnyObject; | ||
public watch(watchOptions: WatchOptions): void; | ||
public register(registerOptions: RegisterOptions): void; | ||
public watch(watchOptions?: WatchOptions): void; | ||
public register(registerData: RegisterData, registerRetryOptions?: RegisterRetryOptions): void; | ||
public registerInterval(registerIntervalOptions: RegisterIntervalOptions): void; | ||
public close(): void; | ||
@@ -68,0 +70,0 @@ } |
@@ -144,12 +144,7 @@ import retry from 'async-retry'; | ||
watcher.on('change', data => data && onChange(parseValue(data))); | ||
watcher.on('error', err => err && onError(err)); | ||
watcher.on('error', err => err && onError(err, key)); | ||
this.openWatchersToClose.push(watcher); | ||
} | ||
async register({ | ||
data = {}, | ||
validateRegisteredInterval, | ||
onError, | ||
registerRetryOptions = {} | ||
} = {}) { | ||
async register(data, registerRetryOptions = {}) { | ||
if (!data.name || !data.id) throw new Error('must provide name and id to register for consul service discovery'); | ||
@@ -160,22 +155,34 @@ if (this.registerParams.id) return; | ||
}; | ||
const list = await retry(async () => this.consulInstance.agent.service.list(), options); | ||
const isRegistered = Object.entries(list).some(([id, { | ||
Service | ||
}]) => id === data.id && Service === data.name); | ||
const startRegister = async () => { | ||
try { | ||
const list = await retry(async () => this.consulInstance.agent.service.list(), options); | ||
const isRegistered = Object.entries(list).some(([id, { | ||
Service | ||
}]) => id === data.id && Service === data.name); | ||
if (!isRegistered) { | ||
await retry(async () => this.consulInstance.agent.service.register(data), options); | ||
this.registerParams.id = data.id; | ||
} | ||
} | ||
if (!isRegistered) { | ||
await retry(async () => this.consulInstance.agent.service.register(data), options); | ||
this.registerParams.id = data.id; | ||
} | ||
async registerInterval({ | ||
data, | ||
interval, | ||
onError, | ||
registerRetryOptions | ||
}) { | ||
const options = { ...this.registerRetryOptions, | ||
...registerRetryOptions | ||
}; | ||
if (validateRegisteredInterval) this.registerParams.timeoutId = setTimeout(startRegister, validateRegisteredInterval); | ||
const startInterval = async () => { | ||
try { | ||
await this.register(data, options); | ||
} catch (err) { | ||
onError(err); | ||
} | ||
this.registerParams.timeoutId = setTimeout(startInterval, interval); | ||
}; | ||
await startRegister(); | ||
startInterval(); | ||
} | ||
@@ -182,0 +189,0 @@ |
{ | ||
"name": "@logzio-node-toolbox/consul", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "Consul easy use for json configs and service discovery", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs", |
@@ -30,3 +30,3 @@ <p align="center"> | ||
receive same params as validateOptions and will merge with th one passed to the initializer | ||
``javascript | ||
```javascript | ||
await consul.validateConnected(validateOptions) | ||
@@ -33,0 +33,0 @@ ``` |
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
71919
1965