-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from lidofinance/develop
Develop
- Loading branch information
Showing
11 changed files
with
174 additions
and
24 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
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 @@ | ||
export const MAX_BLOCK_DELAY_SECONDS = 5 * 60; |
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,25 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { | ||
HealthCheckService, | ||
MemoryHealthIndicator, | ||
HealthCheck, | ||
} from '@nestjs/terminus'; | ||
import { ProviderHealthIndicator } from './provider.health'; | ||
|
||
@Controller('health') | ||
export class HealthController { | ||
constructor( | ||
private health: HealthCheckService, | ||
private memory: MemoryHealthIndicator, | ||
private provider: ProviderHealthIndicator, | ||
) {} | ||
|
||
@Get() | ||
@HealthCheck() | ||
check() { | ||
return this.health.check([ | ||
async () => this.memory.checkHeap('memoryHeap', 1024 * 1024 * 1024), | ||
async () => this.provider.isHealthy('RPCProvider'), | ||
]); | ||
} | ||
} |
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,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TerminusModule } from '@nestjs/terminus'; | ||
import { HealthController } from './health.controller'; | ||
import { ProviderHealthIndicator } from './provider.health'; | ||
|
||
@Module({ | ||
providers: [ProviderHealthIndicator], | ||
controllers: [HealthController], | ||
imports: [TerminusModule], | ||
}) | ||
export class HealthModule {} |
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,2 @@ | ||
export * from './health.controller'; | ||
export * from './health.module'; |
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,43 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { | ||
HealthIndicator, | ||
HealthIndicatorResult, | ||
HealthCheckError, | ||
} from '@nestjs/terminus'; | ||
import { ProviderService } from 'provider'; | ||
import { MAX_BLOCK_DELAY_SECONDS } from './health.constants'; | ||
|
||
@Injectable() | ||
export class ProviderHealthIndicator extends HealthIndicator { | ||
constructor(private providerService: ProviderService) { | ||
super(); | ||
} | ||
|
||
async getBlockTimestamp() { | ||
try { | ||
const block = await this.providerService.getBlock(); | ||
return block.timestamp; | ||
} catch (error) { | ||
return -1; | ||
} | ||
} | ||
|
||
getNowTimestamp() { | ||
return Math.floor(Date.now() / 1000); | ||
} | ||
|
||
async isHealthy(key: string): Promise<HealthIndicatorResult> { | ||
const blockTimestamp = await this.getBlockTimestamp(); | ||
const nowTimestamp = this.getNowTimestamp(); | ||
const deltaTimestamp = Math.abs(nowTimestamp - blockTimestamp); | ||
|
||
const isHealthy = deltaTimestamp < MAX_BLOCK_DELAY_SECONDS; | ||
const result = this.getStatus(key, isHealthy, { | ||
blockTimestamp, | ||
nowTimestamp, | ||
}); | ||
|
||
if (isHealthy) return result; | ||
throw new HealthCheckError('Provider check failed', 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
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 |
---|---|---|
|
@@ -1028,6 +1028,13 @@ | |
jsonc-parser "3.0.0" | ||
pluralize "8.0.0" | ||
|
||
"@nestjs/terminus@^8.0.1": | ||
version "8.0.1" | ||
resolved "https://registry.yarnpkg.com/@nestjs/terminus/-/terminus-8.0.1.tgz#96403795cbb6f14621c7504ee534fba336066643" | ||
integrity sha512-UUSKK3/kO2gMUdXGJaOsj89pYA6OJMoKQPDx+zoEKQhA3+TPp0Js4ndWrtms5g6ydf3BfQqGQgEBEBjiq+0LWQ== | ||
dependencies: | ||
check-disk-space "3.0.1" | ||
|
||
"@nestjs/testing@^8.0.11": | ||
version "8.0.11" | ||
resolved "https://registry.yarnpkg.com/@nestjs/testing/-/testing-8.0.11.tgz#d41fc20a149dd919c9dfacb668f1c9cbc131f57c" | ||
|
@@ -1985,6 +1992,11 @@ chardet@^0.7.0: | |
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e" | ||
integrity sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA== | ||
|
||
[email protected]: | ||
version "3.0.1" | ||
resolved "https://registry.yarnpkg.com/check-disk-space/-/check-disk-space-3.0.1.tgz#c3798f3e4ec56dcc4d4b98dac0b770e2634efad7" | ||
integrity sha512-wRG5ZihEZUXAKVCio5YkLgrHsHDrD+GjRBjxJvoJvN9wrze7EdvYGnOYJSsCyZ4b40jBa3lDmWDmESY9QDQGcg== | ||
|
||
[email protected], chokidar@^3.4.2: | ||
version "3.5.2" | ||
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.2.tgz#dba3976fcadb016f66fd365021d91600d01c1e75" | ||
|