-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add database health checks and remove legacy health check flag
Breaking: The config.healthCheck is no longer relevant
- Loading branch information
1 parent
32f377e
commit 0373e7f
Showing
8 changed files
with
507 additions
and
124 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
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,71 @@ | ||
/* | ||
* @adonisjs/lucid | ||
* | ||
* (c) AdonisJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { BaseCheck, Result } from '@adonisjs/core/health' | ||
import type { HealthCheckResult } from '@adonisjs/core/types/health' | ||
import type { QueryClientContract } from '../../types/database.js' | ||
|
||
/** | ||
* The DbCheck attempts to establish the database connection by | ||
* executing a sample query. | ||
*/ | ||
export class DbCheck extends BaseCheck { | ||
#client: QueryClientContract | ||
|
||
/** | ||
* Health check public name | ||
*/ | ||
name: string | ||
|
||
constructor(client: QueryClientContract) { | ||
super() | ||
this.#client = client | ||
this.name = `Database health check (${client.connectionName})` | ||
} | ||
|
||
/** | ||
* Returns connection metadata to be shared in the health checks | ||
* report | ||
*/ | ||
#getConnectionMetadata() { | ||
return { | ||
connection: { | ||
name: this.#client.connectionName, | ||
dialect: this.#client.dialect.name, | ||
}, | ||
} | ||
} | ||
|
||
/** | ||
* Internal method to ping the database server | ||
*/ | ||
async #ping() { | ||
if (this.#client.dialect.name === 'oracledb') { | ||
await this.#client.rawQuery('SELECT 1 + 1 AS result FROM dual') | ||
} else { | ||
await this.#client!.rawQuery('SELECT 1 + 1 AS result') | ||
} | ||
} | ||
|
||
/** | ||
* Executes the health check | ||
*/ | ||
async run(): Promise<HealthCheckResult> { | ||
try { | ||
await this.#ping() | ||
return Result.ok('Successfully connected to the database server').mergeMetaData( | ||
this.#getConnectionMetadata() | ||
) | ||
} catch (error) { | ||
return Result.failed(error.message || 'Connection failed', error).mergeMetaData( | ||
this.#getConnectionMetadata() | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.