Skip to content

Commit

Permalink
refactor: All remaining health indicators to new internal api
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunnerLivio committed Feb 13, 2024
1 parent dba23e0 commit 07b2e75
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 292 deletions.
80 changes: 40 additions & 40 deletions lib/health-indicator/database/mikro-orm.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,46 +38,6 @@ export class MikroOrmHealthIndicator extends HealthIndicator {
this.checkDependantPackages();
}

/**
* Checks if responds in (default) 1000ms and
* returns a result object corresponding to the result
* @param key The key which will be used for the result object
* @param options The options for the ping
*
* @example
* MikroOrmHealthIndicator.pingCheck('database', { timeout: 1500 });
*/
public async pingCheck(
key: string,
options: MikroOrmPingCheckSettings = {},
): Promise<HealthIndicatorResult> {
this.checkDependantPackages();
const check = this.healthIndicatorService.check(key);

const timeout = options.timeout || 1000;
const connection = options.connection || this.getContextConnection();

if (!connection) {
return check.down();
}

try {
await this.pingDb(connection, timeout);
} catch (error) {
// Check if the error is a timeout error
if (error instanceof PromiseTimeoutError) {
return check.down(`timeout of ${timeout}ms exceeded`);
}
if (error instanceof DatabaseNotConnectedError) {
return check.down(error.message);
}

return check.down();
}

return check.up();
}

private checkDependantPackages() {
checkPackages(
['@mikro-orm/nestjs', '@mikro-orm/core'],
Expand Down Expand Up @@ -118,4 +78,44 @@ export class MikroOrmHealthIndicator extends HealthIndicator {

return await promiseTimeout(timeout, checker());
}

/**
* Checks if responds in (default) 1000ms and
* returns a result object corresponding to the result
* @param key The key which will be used for the result object
* @param options The options for the ping
*
* @example
* MikroOrmHealthIndicator.pingCheck('database', { timeout: 1500 });
*/
public async pingCheck<Key extends string = string>(
key: Key,
options: MikroOrmPingCheckSettings = {},
): Promise<HealthIndicatorResult<Key>> {
this.checkDependantPackages();
const check = this.healthIndicatorService.check(key);

const timeout = options.timeout || 1000;
const connection = options.connection || this.getContextConnection();

if (!connection) {
return check.down();
}

try {
await this.pingDb(connection, timeout);
} catch (error) {
// Check if the error is a timeout error
if (error instanceof PromiseTimeoutError) {
return check.down(`timeout of ${timeout}ms exceeded`);
}
if (error instanceof DatabaseNotConnectedError) {
return check.down(error.message);
}

return check.down();
}

return check.up();
}
}
45 changes: 14 additions & 31 deletions lib/health-indicator/database/mongoose.health.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { Injectable, Scope } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import type * as NestJSMongoose from '@nestjs/mongoose';
import {
type HealthIndicatorResult,
TimeoutError,
ConnectionNotFoundError,
} from '../..';
import { HealthCheckError } from '../../health-check/health-check.error';
import { type HealthIndicatorResult } from '../..';
import {
promiseTimeout,
TimeoutError as PromiseTimeoutError,
checkPackages,
} from '../../utils';
import { HealthIndicator } from '../health-indicator';
import { HealthIndicatorService } from '../health-indicator.service';

export interface MongoosePingCheckSettings {
/**
Expand All @@ -34,12 +30,10 @@ export interface MongoosePingCheckSettings {
*/
@Injectable({ scope: Scope.TRANSIENT })
export class MongooseHealthIndicator extends HealthIndicator {
/**
* Initializes the MongooseHealthIndicator
*
* @param {ModuleRef} moduleRef The NestJS module reference
*/
constructor(private moduleRef: ModuleRef) {
constructor(
private readonly moduleRef: ModuleRef,
private readonly healthIndicatorService: HealthIndicatorService,
) {
super();
this.checkDependantPackages();
}
Expand Down Expand Up @@ -92,41 +86,30 @@ export class MongooseHealthIndicator extends HealthIndicator {
* @example
* mongooseHealthIndicator.pingCheck('mongodb', { timeout: 1500 });
*/
public async pingCheck(
key: string,
public async pingCheck<Key extends string = string>(
key: Key,
options: MongoosePingCheckSettings = {},
): Promise<HealthIndicatorResult> {
): Promise<HealthIndicatorResult<Key>> {
this.checkDependantPackages();
const check = this.healthIndicatorService.check(key);

const connection = options.connection || this.getContextConnection();
const timeout = options.timeout || 1000;

if (!connection) {
throw new ConnectionNotFoundError(
this.getStatus(key, false, {
message: 'Connection provider not found in application context',
}),
);
return check.down('Connection provider not found in application context');
}

try {
await this.pingDb(connection, timeout);
} catch (err) {
if (err instanceof PromiseTimeoutError) {
throw new TimeoutError(
timeout,
this.getStatus(key, false, {
message: `timeout of ${timeout}ms exceeded`,
}),
);
return check.down(`timeout of ${timeout}ms exceeded`);
}

throw new HealthCheckError(
`${key} is not available`,
this.getStatus(key, false),
);
return check.down();
}

return this.getStatus(key, true);
return check.up();
}
}
27 changes: 10 additions & 17 deletions lib/health-indicator/database/prisma.health.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable } from '@nestjs/common';
import { TimeoutError } from '../../errors';
import { HealthCheckError } from '../../health-check';
import {
promiseTimeout,
TimeoutError as PromiseTimeoutError,
} from '../../utils';
import { HealthIndicator } from '../health-indicator';
import { type HealthIndicatorResult } from '../health-indicator-result.interface';
import { HealthIndicatorService } from '../health-indicator.service';

type PingCommandSignature = { [Key in string]?: number };

Expand Down Expand Up @@ -35,7 +35,7 @@ export interface PrismaClientPingCheckSettings {
*/
@Injectable()
export class PrismaHealthIndicator extends HealthIndicator {
constructor() {
constructor(private readonly healthIndicatorService: HealthIndicatorService) {
super();
}

Expand Down Expand Up @@ -69,31 +69,24 @@ export class PrismaHealthIndicator extends HealthIndicator {
* @param prismaClient PrismaClient
* @param options The options for the ping
*/
public async pingCheck(
key: string,
public async pingCheck<Key extends string = string>(
key: Key,
prismaClient: PrismaClient,
options: PrismaClientPingCheckSettings = {},
): Promise<any> {
): Promise<HealthIndicatorResult<Key>> {
const check = this.healthIndicatorService.check(key);
const timeout = options.timeout || 1000;

try {
await this.pingDb(timeout, prismaClient);
} catch (error) {
if (error instanceof PromiseTimeoutError) {
throw new TimeoutError(
timeout,
this.getStatus(key, false, {
message: `timeout of ${timeout}ms exceeded`,
}),
);
return check.down(`timeout of ${timeout}ms exceeded`);
}

throw new HealthCheckError(
`${key} is not available`,
this.getStatus(key, false),
);
return check.down();
}

return this.getStatus(key, true);
return check.up();
}
}
45 changes: 14 additions & 31 deletions lib/health-indicator/database/sequelize.health.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { Injectable, Scope } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import type * as NestJSSequelize from '@nestjs/sequelize';
import {
type HealthIndicatorResult,
TimeoutError,
ConnectionNotFoundError,
} from '../..';
import { HealthCheckError } from '../../health-check/health-check.error';
import { type HealthIndicatorResult } from '../..';
import {
promiseTimeout,
TimeoutError as PromiseTimeoutError,
checkPackages,
} from '../../utils';
import { HealthIndicator } from '../health-indicator';
import { HealthIndicatorService } from '../health-indicator.service';

export interface SequelizePingCheckSettings {
/**
Expand All @@ -34,12 +30,10 @@ export interface SequelizePingCheckSettings {
*/
@Injectable({ scope: Scope.TRANSIENT })
export class SequelizeHealthIndicator extends HealthIndicator {
/**
* Initializes the SequelizeHealthIndicator
*
* @param {ModuleRef} moduleRef The NestJS module reference
*/
constructor(private moduleRef: ModuleRef) {
constructor(
private readonly moduleRef: ModuleRef,
private readonly healthIndicatorService: HealthIndicatorService,
) {
super();
this.checkDependantPackages();
}
Expand Down Expand Up @@ -88,41 +82,30 @@ export class SequelizeHealthIndicator extends HealthIndicator {
* @example
* sequelizeHealthIndicator.pingCheck('database', { timeout: 1500 });
*/
public async pingCheck(
key: string,
public async pingCheck<Key extends string = string>(
key: Key,
options: SequelizePingCheckSettings = {},
): Promise<HealthIndicatorResult> {
): Promise<HealthIndicatorResult<Key>> {
this.checkDependantPackages();
const check = this.healthIndicatorService.check(key);

const connection = options.connection || this.getContextConnection();
const timeout = options.timeout || 1000;

if (!connection) {
throw new ConnectionNotFoundError(
this.getStatus(key, false, {
message: 'Connection provider not found in application context',
}),
);
return check.down('Connection provider not found in application context');
}

try {
await this.pingDb(connection, timeout);
} catch (err) {
if (err instanceof PromiseTimeoutError) {
throw new TimeoutError(
timeout,
this.getStatus(key, false, {
message: `timeout of ${timeout}ms exceeded`,
}),
);
return check.down(`timeout of ${timeout}ms exceeded`);
}

throw new HealthCheckError(
`${key} is not available`,
this.getStatus(key, false),
);
return check.down();
}

return this.getStatus(key, true);
return check.up();
}
}
6 changes: 3 additions & 3 deletions lib/health-indicator/database/typeorm.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, Scope } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import type * as NestJSTypeOrm from '@nestjs/typeorm';
import type * as TypeOrm from 'typeorm';
import { HealthIndicator } from '../';
import { HealthIndicator, type HealthIndicatorResult } from '../';
import { MongoConnectionError } from '../../errors';
import {
TimeoutError as PromiseTimeoutError,
Expand Down Expand Up @@ -124,7 +124,7 @@ export class TypeOrmHealthIndicator extends HealthIndicator {
async pingCheck<Key extends string>(
key: Key,
options: TypeOrmPingCheckSettings = {},
) {
): Promise<HealthIndicatorResult<Key>> {
const check = this.healthIndicatorService.check(key);
this.checkDependantPackages();

Expand All @@ -146,7 +146,7 @@ export class TypeOrmHealthIndicator extends HealthIndicator {
return check.down(err.message);
}

return check.down(`${key} is not available`);
return check.down();
}

return check.up();
Expand Down
Loading

0 comments on commit 07b2e75

Please sign in to comment.