-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #4 from openfoodfacts:line-endings
Health check
- Loading branch information
Showing
9 changed files
with
293 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,4 @@ | ||
* text=auto eol=lf | ||
[core] | ||
# avoid line ending conversion on windows | ||
autocrlf=false |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,48 @@ | ||
import { HealthController } from './health.controller'; | ||
import { createTestingModule } from '../../test/test.helper'; | ||
import { AppModule } from '../app.module'; | ||
import { MongodbHealthIndicator } from './mongodb-health-indicator'; | ||
import { HealthCheckError, HealthIndicatorResult } from '@nestjs/terminus'; | ||
import { ServiceUnavailableException } from '@nestjs/common'; | ||
|
||
describe('HealthController', () => { | ||
it('should return healthy', async () => { | ||
await createTestingModule([AppModule], async (app) => { | ||
const controller = app.get(HealthController); | ||
expect(controller).toBeDefined(); | ||
|
||
const mongoIndicator = app.get(MongodbHealthIndicator); | ||
mongoIndicator.isHealthy = jest.fn(async () => { | ||
return { | ||
mongodb: { | ||
status: 'up', | ||
}, | ||
} as HealthIndicatorResult; | ||
}); | ||
const status = await controller.check(); | ||
expect(status.status).toBe('ok'); | ||
}); | ||
}); | ||
|
||
it('should return unhealthy if mongodb is down', async () => { | ||
await createTestingModule([AppModule], async (app) => { | ||
const controller = app.get(HealthController); | ||
|
||
const mongoIndicator = app.get(MongodbHealthIndicator); | ||
mongoIndicator.isHealthy = jest.fn(async () => { | ||
throw new HealthCheckError('Mongodb check failed', { | ||
mongodb: { | ||
status: 'down', | ||
}, | ||
}); | ||
}); | ||
try { | ||
await controller.check(); | ||
fail('should not get here'); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(ServiceUnavailableException); | ||
expect(e.response.status).toBe('error'); | ||
} | ||
}); | ||
}); | ||
}); |
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,22 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { HealthCheckService, HealthCheck } from '@nestjs/terminus'; | ||
import { PostgresHealthIndicator } from './postgres-health-indicator'; | ||
import { MongodbHealthIndicator } from './mongodb-health-indicator'; | ||
|
||
@Controller('health') | ||
export class HealthController { | ||
constructor( | ||
private health: HealthCheckService, | ||
private postgres: PostgresHealthIndicator, | ||
private mongodb: MongodbHealthIndicator, | ||
) {} | ||
|
||
@Get() | ||
@HealthCheck() | ||
check() { | ||
return this.health.check([ | ||
() => this.postgres.isHealthy(), | ||
() => this.mongodb.isHealthy(), | ||
]); | ||
} | ||
} |
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,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { HealthController } from './health.controller'; | ||
import { PostgresHealthIndicator } from './postgres-health-indicator'; | ||
import { TerminusModule } from '@nestjs/terminus'; | ||
import { DomainModule } from '../domain/domain.module'; | ||
import { MongodbHealthIndicator } from './mongodb-health-indicator'; | ||
|
||
@Module({ | ||
imports: [TerminusModule, DomainModule], | ||
controllers: [HealthController], | ||
providers: [PostgresHealthIndicator, MongodbHealthIndicator], | ||
}) | ||
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,35 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { | ||
HealthIndicator, | ||
HealthIndicatorResult, | ||
HealthCheckError, | ||
} from '@nestjs/terminus'; | ||
import { MongoClient } from 'mongodb'; | ||
|
||
@Injectable() | ||
export class MongodbHealthIndicator extends HealthIndicator { | ||
NAME = 'mongodb'; | ||
async isHealthy(): Promise<HealthIndicatorResult> { | ||
try { | ||
const client = new MongoClient(process.env.MONGO_URI, { | ||
serverSelectionTimeoutMS: 1000, | ||
connectTimeoutMS: 1000, | ||
socketTimeoutMS: 1000, | ||
}); | ||
await client.connect(); | ||
const db = client.db('off'); | ||
const products = db.collection('products'); | ||
const cursor = products.find({}, { projection: { code: 1 } }); | ||
await cursor.next(); | ||
await cursor.close(); | ||
await client.close(); | ||
|
||
return this.getStatus(this.NAME, true); | ||
} catch (e) { | ||
throw new HealthCheckError( | ||
'MongoDB check failed', | ||
this.getStatus(this.NAME, false, e), | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.