Skip to content

Commit

Permalink
improvement: register health checker only when one or more connection…
Browse files Browse the repository at this point in the history
…s are using it
  • Loading branch information
thetutlage committed Apr 27, 2020
1 parent 7611427 commit 117ec61
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions adonis-typings/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,8 @@ declare module '@ioc:Adonis/Lucid/Database' {
): SimplePaginatorContract<Row[]>
},

hasHealthChecksEnabled: boolean

/**
* Pretty print query logs
*/
Expand Down
6 changes: 5 additions & 1 deletion providers/DatabaseProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ export default class DatabaseServiceProvider {
private registerHealthChecker () {
this.$container.with(
['Adonis/Core/HealthCheck', 'Adonis/Lucid/Database'],
(HealthCheck) => HealthCheck.addChecker('lucid', 'Adonis/Lucid/Database'),
(HealthCheck, Db: Database) => {
if (Db.hasHealthChecksEnabled) {
HealthCheck.addChecker('lucid', 'Adonis/Lucid/Database')
}
},
)
}

Expand Down
18 changes: 17 additions & 1 deletion src/Database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ProfilerContract } from '@ioc:Adonis/Core/Profiler'

import {
DatabaseConfig,
ConnectionNode,
DatabaseContract,
DatabaseClientOptions,
TransactionClientContract,
Expand Down Expand Up @@ -60,6 +61,8 @@ export class Database implements DatabaseContract {
* A store of global transactions
*/
public connectionGlobalTransactions: Map<string, TransactionClientContract> = new Map()
public hasHealthChecksEnabled = false
public prettyPrint = prettyPrint

constructor (
private config: DatabaseConfig,
Expand All @@ -69,9 +72,22 @@ export class Database implements DatabaseContract {
) {
this.manager = new ConnectionManager(this.logger, this.emitter)
this.registerConnections()
this.findIfHealthChecksAreEnabled()
}

public prettyPrint = prettyPrint
/**
* Compute whether health check is enabled or not after registering the connections.
* There are chances that all pre-registered connections are not using health
* checks but a dynamic connection is using it. We don't support that use case
* for now, since it complicates things a lot and forces us to register the
* health checker on demand.
*/
private findIfHealthChecksAreEnabled () {
this.hasHealthChecksEnabled = !!Object.keys(this.manager.connections).find((key) => {
const connection = this.manager.connections[key] as ConnectionNode
return !!connection.config.healthCheck
})
}

/**
* Registering all connections with the manager, so that we can fetch
Expand Down

0 comments on commit 117ec61

Please sign in to comment.