Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bluetooth-classic): monitor command health
Too many successive errors from the hcitool commands will now make the room-assistant instance report as unhealthy. Closes #194
- Loading branch information
Showing
10 changed files
with
166 additions
and
13 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/integrations/bluetooth-classic/bluetooth-classic.health.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { BluetoothClassicHealthIndicator } from './bluetooth-classic.health'; | ||
import { HealthCheckError } from '@nestjs/terminus'; | ||
|
||
describe('BluetoothClassicHealthIndicator', () => { | ||
let healthIndicator: BluetoothClassicHealthIndicator; | ||
|
||
beforeEach(() => { | ||
healthIndicator = new BluetoothClassicHealthIndicator(); | ||
}); | ||
|
||
it('should report healthy by default', () => { | ||
const result = healthIndicator.successiveErrorCheck(3); | ||
expect(result['bt_successive_errors'].status).toEqual('up'); | ||
}); | ||
|
||
it('should report unhealthy after meeting the threshold', () => { | ||
healthIndicator.reportError(); | ||
healthIndicator.reportError(); | ||
healthIndicator.reportError(); | ||
|
||
expect(() => healthIndicator.successiveErrorCheck(3)).toThrow( | ||
HealthCheckError | ||
); | ||
}); | ||
|
||
it('should reset the error count on success', () => { | ||
healthIndicator.reportError(); | ||
healthIndicator.reportError(); | ||
healthIndicator.reportError(); | ||
healthIndicator.reportSuccess(); | ||
|
||
const result = healthIndicator.successiveErrorCheck(3); | ||
expect(result['bt_successive_errors'].status).toEqual('up'); | ||
}); | ||
}); |
40 changes: 40 additions & 0 deletions
40
src/integrations/bluetooth-classic/bluetooth-classic.health.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
HealthCheckError, | ||
HealthIndicator, | ||
HealthIndicatorResult, | ||
} from '@nestjs/terminus'; | ||
import { Injectable, Optional } from '@nestjs/common'; | ||
import { HealthIndicatorService } from '../../status/health-indicator.service'; | ||
|
||
@Injectable() | ||
export class BluetoothClassicHealthIndicator extends HealthIndicator { | ||
private errorsOccurred = 0; | ||
|
||
constructor(@Optional() healthIndicatorService?: HealthIndicatorService) { | ||
super(); | ||
healthIndicatorService?.registerHealthIndicator(async () => | ||
this.successiveErrorCheck(3) | ||
); | ||
} | ||
|
||
successiveErrorCheck(threshold: number): HealthIndicatorResult { | ||
const isHealthy = this.errorsOccurred < threshold; | ||
const result = this.getStatus(`bt_successive_errors`, isHealthy); | ||
|
||
if (isHealthy) { | ||
return result; | ||
} | ||
throw new HealthCheckError( | ||
'BT Classic successive error check failed', | ||
result | ||
); | ||
} | ||
|
||
reportError(): void { | ||
this.errorsOccurred++; | ||
} | ||
|
||
reportSuccess(): void { | ||
this.errorsOccurred = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { HealthIndicatorFunction } from '@nestjs/terminus'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class HealthIndicatorService { | ||
private healthIndicators: HealthIndicatorFunction[] = []; | ||
|
||
getIndicators(): HealthIndicatorFunction[] { | ||
return this.healthIndicators; | ||
} | ||
|
||
registerHealthIndicator( | ||
healthIndicatorFunction: HealthIndicatorFunction | ||
): void { | ||
this.healthIndicators.push(healthIndicatorFunction); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,23 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { StatusController } from './status.controller'; | ||
import { TerminusModule } from '@nestjs/terminus'; | ||
import { HealthIndicatorService } from './health-indicator.service'; | ||
|
||
describe('Status Controller', () => { | ||
let controller: StatusController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [TerminusModule], | ||
controllers: [StatusController], | ||
providers: [HealthIndicatorService], | ||
}).compile(); | ||
|
||
controller = module.get<StatusController>(StatusController); | ||
}); | ||
|
||
it('should return healthy', () => { | ||
expect(controller.health()).toEqual('healthy'); | ||
it('should return healthy', async () => { | ||
const status = await controller.check(); | ||
expect(status.status).toEqual('ok'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,21 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { | ||
HealthCheck, | ||
HealthCheckResult, | ||
HealthCheckService, | ||
} from '@nestjs/terminus'; | ||
import { HealthIndicatorService } from './health-indicator.service'; | ||
|
||
@Controller('status') | ||
export class StatusController { | ||
constructor( | ||
private health: HealthCheckService, | ||
private healthIndicatorService: HealthIndicatorService | ||
) {} | ||
|
||
@Get() | ||
health(): string { | ||
return 'healthy'; | ||
@HealthCheck() | ||
check(): Promise<HealthCheckResult> { | ||
return this.health.check(this.healthIndicatorService.getIndicators()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters