Skip to content

Commit

Permalink
Implement health check
Browse files Browse the repository at this point in the history
  • Loading branch information
Nizar-Rahme committed Mar 3, 2021
1 parent 554af1a commit f413c32
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 5 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ COPY . .

COPY --from=development /usr/src/app/dist ./dist

HEALTHCHECK CMD curl -f http://localhost:3000/health || exit 1

CMD ["node", "dist/main"]
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ services:
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- 3000:3000
command: yarn start:dev
env_file:
- .env
healthcheck:
test: ['CMD', 'curl', '-f', 'http://localhost:3000/health']
depends_on:
- mongo
mongo:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@nestjs/mongoose": "^7.2.3",
"@nestjs/platform-express": "^7.5.1",
"@nestjs/schedule": "^0.4.2",
"@nestjs/terminus": "^7.1.0",
"cache-manager": "^3.4.0",
"date-fns": "^2.17.0",
"joi": "^17.4.0",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AppService } from './app.service';
import { CitiesModule } from './cities/cities.module';
import { ForecastModule } from './forecast/forecast.module';
import { SchedulerModule } from './scheduler/scheduler.module';
import { HealthModule } from './health/health.module';

@Module({
imports: [
Expand All @@ -31,6 +32,7 @@ import { SchedulerModule } from './scheduler/scheduler.module';
CitiesModule,
ForecastModule,
SchedulerModule,
HealthModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
2 changes: 1 addition & 1 deletion src/config/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const configuration = () => ({
forecastAPI: {
uri: process.env.API_URL,
url: process.env.API_URL,
key: process.env.API_KEY,
},
database: {
Expand Down
6 changes: 3 additions & 3 deletions src/forecast/forecast.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ interface IForecast {

@Injectable()
export class ForecastService {
private readonly url = this.configService.get<string>('forecastAPI.url');
private readonly key = this.configService.get<string>('forecastAPI.key');

constructor(
private configService: ConfigService,
private httpService: HttpService,
@InjectModel(Forecast.name) private forecastModel: Model<ForecastDocument>,
) {}

private readonly url = this.configService.get<string>('forecastAPI.url');
private readonly key = this.configService.get<string>('forecastAPI.key');

async fetchForecast({ name, country }: CityDocument) {
try {
const { data } = await this.httpService
Expand Down
36 changes: 36 additions & 0 deletions src/health/health.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Controller, Get } from '@nestjs/common';
import {
HealthCheck,
HealthCheckService,
HttpHealthIndicator,
MongooseHealthIndicator,
} from '@nestjs/terminus';
import { ConfigService } from '@nestjs/config';

@Controller('health')
export class HealthController {
constructor(
private configService: ConfigService,
private health: HealthCheckService,
private http: HttpHealthIndicator,
private mongoose: MongooseHealthIndicator,
) {}

private readonly url = this.configService.get<string>('forecastAPI.url');
private readonly key = this.configService.get<string>('forecastAPI.key');

@Get()
@HealthCheck()
check() {
return this.health.check([
async () =>
this.http.pingCheck('openweathermap-api', this.url, {
params: {
q: `Helsinki,FI`,
appid: this.key,
},
}),
async () => this.mongoose.pingCheck('mongoose'),
]);
}
}
10 changes: 10 additions & 0 deletions src/health/health.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';

import { HealthController } from './health.controller';

@Module({
imports: [TerminusModule],
controllers: [HealthController],
})
export class HealthModule {}
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);

// Starts listening for shutdown hooks
app.enableShutdownHooks();

await app.listen(3000);
}
bootstrap();
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,14 @@
fs-extra "9.1.0"
pluralize "8.0.0"

"@nestjs/terminus@^7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@nestjs/terminus/-/terminus-7.1.0.tgz#fa9666e903a405541fe431a709690b678a24d8cb"
integrity sha512-5C6ZpC7DU9HunPmRwYZVK/DiQaNjQfH/KiDMKBLzWTiVG5cVaV+II5eTr0cdhmaaJk58u54S/5eb7lGaK5MGHQ==
dependencies:
check-disk-space "2.1.0"
deprecate "^1.1.1"

"@nestjs/testing@^7.6.13":
version "7.6.13"
resolved "https://registry.yarnpkg.com/@nestjs/testing/-/testing-7.6.13.tgz#a0cd208fb541aa9084eb55cdd120d1e67eec49c1"
Expand Down Expand Up @@ -1786,6 +1794,11 @@ chardet@^0.7.0:
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"
integrity sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==

check-disk-space@2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/check-disk-space/-/check-disk-space-2.1.0.tgz#2e77fe62f30d9676dc37a524ea2008f40c780295"
integrity sha512-f0nx9oJF/AVF8nhSYlF1EBvMNnO+CXyLwKhPvN1943iOMI9TWhQigLZm80jAf0wzQhwKkzA8XXjyvuVUeGGcVQ==

chokidar@3.5.1, chokidar@^3.4.2:
version "3.5.1"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.1.tgz#ee9ce7bbebd2b79f49f304799d5468e31e14e68a"
Expand Down Expand Up @@ -2186,6 +2199,11 @@ depd@~1.1.2:
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
integrity sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=

deprecate@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/deprecate/-/deprecate-1.1.1.tgz#4632e981fc815eeaf00be945a40359c0f8bf9913"
integrity sha512-ZGDXefq1xknT292LnorMY5s8UVU08/WKdzDZCUT6t9JzsiMSP4uzUhgpqugffNVcT5WC6wMBiSQ+LFjlv3v7iQ==

destroy@~1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80"
Expand Down

0 comments on commit f413c32

Please sign in to comment.