Skip to content

Commit

Permalink
refactor: move project into dedicated folder
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunnerLivio committed Mar 15, 2020
1 parent 4325090 commit 00b32cc
Show file tree
Hide file tree
Showing 33 changed files with 460 additions and 359 deletions.
55 changes: 55 additions & 0 deletions lib/health-check/health-check-executor.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Injectable } from '@nestjs/common';
import { HealthIndicatorFunction } from '../health-indicator';
import { HealthCheckError } from '@godaddy/terminus';
import { HealthCheckResult } from './health-check-result.interface';

/**
* Takes care of the execution of health indicators
* @internal
*/
@Injectable()
export class HealthCheckExecutor {
/**
* Executes the given health indicators.
*
* @throws {Error} All errors which are not inherited by the `HealthCheckError`-class
*
* @returns the result of given health indicators
* @param healthIndicators The health indicators which should get executed
*/
async execute(
healthIndicators: HealthIndicatorFunction[],
): Promise<HealthCheckResult> {
const results: any[] = [];
const errors: any[] = [];
await Promise.all(
healthIndicators
// Register all promises
.map(healthIndicator => healthIndicator())
.map((p: Promise<any>) =>
p.catch((error: any) => {
// Is not an expected error. Throw further!
if (!error.causes) throw error;
// Is a expected health check error
errors.push((error as HealthCheckError).causes);
}),
)
.map((p: Promise<any>) =>
p.then((result: any) => result && results.push(result)),
),
);

const info = (results || [])
.concat(errors || [])
.reduce(
(previous: Object, current: Object) => Object.assign(previous, current),
{},
);

if (errors.length) {
throw new HealthCheckError('Healthcheck failed', info);
} else {
return info;
}
}
}
7 changes: 7 additions & 0 deletions lib/health-check/health-check-result.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* The result of a health check
*/
export interface HealthCheckResult {
results: any[];
errors: any[];
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Injectable, BeforeApplicationShutdown } from '@nestjs/common';
import { HealthIndicatorFunction } from './interfaces';
import { HealthIndicatorExecutor } from './health-indicator-executor.service';
import { HealthIndicatorFunction } from '../health-indicator';
import { HealthCheckExecutor } from './health-check-executor.service';

@Injectable()
export class HealthService implements BeforeApplicationShutdown {
export class HealthCheckService implements BeforeApplicationShutdown {
private isShuttingDown: boolean = false;
constructor(private healthIndicatorExecutor: HealthIndicatorExecutor) {}
constructor(private healthCheckExecutor: HealthCheckExecutor) {}

public async check(healthIndicators: HealthIndicatorFunction[]) {
const status = await this.healthIndicatorExecutor.execute(healthIndicators);
const status = await this.healthCheckExecutor.execute(healthIndicators);
if (this.isShuttingDown) {
// TODO: Omit "status" and then prepend the new status so JSON order does not change
// TODO: Omit "status" and then prepend the new status so JSON order does not change
return { ...status, status: 'shutting_down' };
}
return status;
Expand Down
3 changes: 3 additions & 0 deletions lib/health-check/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './health-check.service';
export * from './health-check.decorator';
export * from './health-check-result.interface';
31 changes: 0 additions & 31 deletions lib/health-indicator-executor.service.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
HealthIndicatorResult,
TimeoutError,
ConnectionNotFoundError,
} from '../../';
} from '../..';
import { HealthIndicator } from '../health-indicator';

export interface MongoosePingCheckSettings {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { HealthCheckError } from '@godaddy/terminus';

import * as NestJSTypeOrm from '@nestjs/typeorm';

import { HealthIndicatorResult } from '../../interfaces/health-indicator.interface';
import {
TimeoutError,
ConnectionNotFoundError,
Expand All @@ -16,7 +15,7 @@ import {
promiseTimeout,
checkPackages,
} from '../../utils';
import { HealthIndicator } from '../health-indicator';
import { HealthIndicator, HealthIndicatorResult } from '../health-indicator';

export interface TypeOrmPingCheckSettings {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { Injectable, Inject } from '@nestjs/common';
import { isNil } from '@nestjs/common/utils/shared.utils';
import * as checkDiskSpace from 'check-disk-space';

import { HealthIndicatorResult } from '../../interfaces';
import { HealthIndicator } from '../health-indicator';
import { HealthIndicator, HealthIndicatorResult } from '../health-indicator';
import { CHECKDISKSPACE_LIB } from '../../terminus.constants';
import { StorageExceededError } from '../../errors';
import { STORAGE_EXCEEDED } from '../../errors/messages.constant';
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Injectable, HttpService } from '@nestjs/common';
import { HealthIndicatorResult } from '../../interfaces';
import { AxiosResponse, AxiosRequestConfig, AxiosError } from 'axios';
import { HealthIndicator } from '../health-indicator';
import { HealthIndicator, HealthIndicatorResult } from '../health-indicator';
import { HealthCheckError } from '@godaddy/terminus';

/**
* The DNSHealthIndicator contains health indicators
* which are used for health checks related to HTTP requests
* and DNS
*
*
* @publicApi
* @module TerminusModule
*/
Expand Down
1 change: 1 addition & 0 deletions lib/health-indicator/dns/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dns.health';
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
import { HealthIndicatorResult } from '..';
/**
* The result object of a health indicator
* @publicApi
*/
export type HealthIndicatorResult = {
/**
* The key of the health indicator which should be uniqe
*/
[key: string]: {
/**
* The status if the given health indicator was successful or not
*/
status: string;
/**
* Optional settings of the health indicator result
*/
[optionalKeys: string]: any;
};
};

/**
* A health indicator function for a health check
*
* @publicApi
*/
export type HealthIndicatorFunction = () => Promise<HealthIndicatorResult>;

/**
* Represents an abstract health indicator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
MicroserviceHealthIndicator,
HealthIndicator,
GRPCHealthIndicator,
} from './health-indicators';
} from '.';

/**
* All the health indicators terminus provides as array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export * from './microservice/grpc.health';
export * from './disk';
export * from './memory';

export * from './health-indicator';
export * from './health-indicator';
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Injectable } from '@nestjs/common';

import { HealthIndicatorResult } from '../../interfaces';
import { HealthIndicator } from '../health-indicator';
import { HealthIndicator, HealthIndicatorResult } from '../health-indicator';
import { STORAGE_EXCEEDED } from '../../errors/messages.constant';
import { StorageExceededError } from '../../errors';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
HealthIndicatorResult,
TimeoutError,
UnhealthyResponseCodeError,
} from '../../';
} from '../..';
import {
checkPackages,
promiseTimeout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import {
TcpOptions,
} from '@nestjs/microservices';
import { TimeoutError } from '../../errors';
import { HealthIndicatorResult } from '../../interfaces';
import {
checkPackages,
promiseTimeout,
TimeoutError as PromiseTimeoutError,
} from '../../utils';
import { HealthIndicator } from '../health-indicator';
import { HealthIndicator, HealthIndicatorResult } from '../health-indicator';

type ClientOptions =
| RedisOptions
Expand Down
7 changes: 4 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './interfaces';
export * from './terminus.module';
export * from './health-indicators';
export { TerminusModule } from './terminus.module';
export * from './health-indicator';
export * from './errors';
export { HealthCheck, HealthCheckService } from './health-check';
export * from './terminus-module-options.interface';
26 changes: 0 additions & 26 deletions lib/interfaces/health-indicator.interface.ts

This file was deleted.

2 changes: 0 additions & 2 deletions lib/interfaces/index.ts

This file was deleted.

Loading

0 comments on commit 00b32cc

Please sign in to comment.