-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(elasticsearch-plugin): Add health check
Relates to #289
- Loading branch information
1 parent
97b5aed
commit 47a8cb9
Showing
2 changed files
with
56 additions
and
9 deletions.
There are no files selected for viewing
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,36 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { HealthCheckError, HealthIndicator, HealthIndicatorResult } from '@nestjs/terminus'; | ||
|
||
import { ElasticsearchService } from './elasticsearch.service'; | ||
|
||
@Injectable() | ||
export class ElasticsearchHealthIndicator extends HealthIndicator { | ||
constructor(private elasticsearchService: ElasticsearchService) { | ||
super(); | ||
} | ||
|
||
async isHealthy(): Promise<HealthIndicatorResult> { | ||
let isHealthy = false; | ||
let error = ''; | ||
try { | ||
await this.elasticsearchService.checkConnection(); | ||
isHealthy = true; | ||
} catch (e) { | ||
error = e.message; | ||
} | ||
const result = this.getStatus('elasticsearch', isHealthy, { message: error }); | ||
if (isHealthy) { | ||
return result; | ||
} | ||
this.throwHealthCheckError(result); | ||
} | ||
|
||
startupCheckFailed(message: string): never { | ||
const result = this.getStatus('elasticsearch', false, { message }); | ||
return this.throwHealthCheckError(result); | ||
} | ||
|
||
private throwHealthCheckError(result: HealthIndicatorResult): never { | ||
throw new HealthCheckError('Elasticsearch not available', result); | ||
} | ||
} |
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