Skip to content

Commit

Permalink
fix: mikroorm is indicated as up even when disconnected (#2509) (#2511)
Browse files Browse the repository at this point in the history
fixes #2460
  • Loading branch information
BrunnerLivio authored Jan 31, 2024
1 parent 72ad705 commit 069b998
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
37 changes: 37 additions & 0 deletions e2e/health-checks/mikro-orm.health.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MikroORM } from '@mikro-orm/core';
import { type INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import {
Expand Down Expand Up @@ -76,6 +77,42 @@ describe('MikroOrmHealthIndicator', () => {
details,
});
});

it('should indicate that mikroOrm is down if the connection has been closed after startup', async () => {
app = await setHealthEndpoint(({ healthCheck, mikroOrm }) =>
healthCheck.check([async () => mikroOrm.pingCheck('mikroOrm')]),
).start();

const up = {
mikroOrm: {
status: 'up',
},
};

request(app.getHttpServer()).get('/health').expect(200).expect({
status: 'ok',
info: up,
error: {},
details: up,
});

const orm = app.get(MikroORM);
await orm.close();

const down = {
mikroOrm: {
status: 'down',
message: 'Not connected to database',
},
};

return request(app.getHttpServer()).get('/health').expect(503).expect({
status: 'error',
info: {},
error: down,
details: down,
});
});
});
});

Expand Down
11 changes: 11 additions & 0 deletions lib/errors/database-not-connected.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { DATABASE_NOT_CONNECTED } from './messages.constant';

/**
* Error which gets thrown when the database is not connected
* @publicApi
*/
export class DatabaseNotConnectedError extends Error {
constructor() {
super(DATABASE_NOT_CONNECTED);
}
}
7 changes: 6 additions & 1 deletion lib/errors/messages.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const CONNECTION_NOT_FOUND =
* @internal
*/
export const TIMEOUT_EXCEEDED = (timeout: number) =>
`timeout of ${timeout.toString()}ms exceeded`;
`Timeout of ${timeout.toString()}ms exceeded`;

/**
* @internal
Expand All @@ -21,3 +21,8 @@ export const STORAGE_EXCEEDED = (keyword: string) =>
*/
export const UNHEALTHY_RESPONSE_CODE = (responseCode: string | number) =>
`The service returned an unhealthy response code: ${responseCode}`;

/**
* @internal
*/
export const DATABASE_NOT_CONNECTED = `Not connected to database`;
22 changes: 16 additions & 6 deletions lib/health-indicator/database/mikro-orm.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { Injectable, Scope } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { HealthIndicator, type HealthIndicatorResult } from '..';
import { TimeoutError } from '../../errors';
import { HealthCheckError } from '../../health-check';
import { DatabaseNotConnectedError } from '../../errors/database-not-connected.error';
import { HealthCheckError } from '../../health-check/health-check.error';
import {
TimeoutError as PromiseTimeoutError,
promiseTimeout,
Expand Down Expand Up @@ -74,16 +75,19 @@ export class MikroOrmHealthIndicator extends HealthIndicator {
}),
);
}

// Check if the error is a connection not found error
if (error instanceof Error) {
if (error instanceof DatabaseNotConnectedError) {
throw new HealthCheckError(
error.message,
this.getStatus(key, false, {
message: error.message,
}),
);
}

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

return this.getStatus(key, true);
Expand Down Expand Up @@ -120,7 +124,13 @@ export class MikroOrmHealthIndicator extends HealthIndicator {
*
*/
private async pingDb(connection: MikroOrm.Connection, timeout: number) {
const isConnected = connection.isConnected();
return await promiseTimeout(timeout, isConnected);
const checker = async () => {
const isConnected = await connection.isConnected();
if (!isConnected) {
throw new DatabaseNotConnectedError();
}
};

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

0 comments on commit 069b998

Please sign in to comment.