[enh] don't repeatedly alert about a failed state

This commit is contained in:
2020-05-08 11:15:09 +02:00
parent de43be347f
commit 8ecca50268
2 changed files with 13 additions and 1 deletions

View File

@@ -1,20 +1,25 @@
export class Watcher { export class Watcher {
private _state: 'INIT'|'OK'|'FAILURE'|'RECOVERED' private _state: 'INIT'|'OK'|'FAILURE'|'RECOVERED'
private _stateChanged: boolean
private _error?: any private _error?: any
constructor(public readonly name: string) { constructor(public readonly name: string) {
this._stateChanged = false
this._state = "INIT" this._state = "INIT"
} }
public stateOK() { public stateOK() {
this._stateChanged = this._state !== "OK"
this._state = "OK" this._state = "OK"
} }
public stateRecovered() { public stateRecovered() {
this._stateChanged = this._state !== "RECOVERED"
this._state = "RECOVERED" this._state = "RECOVERED"
} }
public stateFailure(error: any) { public stateFailure(error: any) {
this._stateChanged = this._state !== "FAILURE"
this._state = "FAILURE" this._state = "FAILURE"
this._error = error this._error = error
} }
@@ -23,6 +28,10 @@ export class Watcher {
return this._state return this._state
} }
public get stateChanged() {
return this._stateChanged
}
public get error() { public get error() {
return this._error return this._error
} }

View File

@@ -47,7 +47,10 @@ export function watcherLoop(
} }
// Wait before reconnecting // Wait before reconnecting
const waitingDelay = reconnectionDelays[Math.min(reconnectionDelays.length - 1, i)] const waitingDelay = reconnectionDelays[Math.min(reconnectionDelays.length - 1, i)]
await onDisconnection(waitingDelay, watcher.error) if (watcher.stateChanged) {
// Notify only if state changed since
await onDisconnection(waitingDelay, watcher.error)
}
await new Promise(resolve => setTimeout(resolve, waitingDelay)) await new Promise(resolve => setTimeout(resolve, waitingDelay))
i++ i++
} }