Skip to content

Commit

Permalink
refactor: remove legacy health checks
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jun 11, 2024
1 parent 44da18c commit 32f377e
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 140 deletions.
22 changes: 0 additions & 22 deletions src/connection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,26 +389,4 @@ export class Connection extends EventEmitter implements ConnectionContract {
}
}
}

/**
* Returns the healthcheck report for the connection
*/
async getReport(): Promise<HealthCheckResult> {
const error = await this.checkWriteHost()
let readError: Error | undefined

if (!error && this.hasReadWriteReplicas) {
readError = await this.checkReadHosts()
}

return {
connection: this.name,
message: readError
? 'Unable to reach one of the read hosts'
: error
? 'Unable to reach the database server'
: 'Connection is healthy',
error: error || readError || null,
}
}
}
48 changes: 0 additions & 48 deletions test/connection/connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,52 +187,4 @@ test.group('Health Checks', (group) => {

await connection.disconnect()
})

if (!['sqlite', 'better_sqlite'].includes(process.env.DB!)) {
test('get healthcheck report for un-healthy connection', async ({ assert }) => {
const connection = new Connection(
'primary',
Object.assign({}, getConfig(), {
connection: {
host: 'bad-host',
},
}),
logger
)
connection.connect()

const report = await connection.getReport()
assert.equal(report.message, 'Unable to reach the database server')
assert.exists(report.error)

await connection.disconnect()
}).timeout(0)

test('get healthcheck report for un-healthy read host', async ({ assert }) => {
const connection = new Connection(
'primary',
Object.assign({}, getConfig(), {
replicas: {
write: {
connection: getConfig().connection,
},
read: {
connection: [
getConfig().connection,
Object.assign({}, getConfig().connection, { host: 'bad-host', port: 8000 }),
],
},
},
}),
logger
)
connection.connect()

const report = await connection.getReport()
assert.equal(report.message, 'Unable to reach one of the read hosts')
assert.exists(report.error)

await connection.disconnect()
}).timeout(0)
}
})
51 changes: 0 additions & 51 deletions test/connection/connection_manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* file that was distributed with this source code.
*/

import { join } from 'node:path'
import { test } from '@japa/runner'

import { Connection } from '../../src/connection/index.js'
Expand All @@ -19,7 +18,6 @@ import {
mapToObj,
logger,
createEmitter,
SQLITE_BASE_PATH,
} from '../../test-helpers/index.js'

test.group('ConnectionManager', (group) => {
Expand Down Expand Up @@ -188,53 +186,4 @@ test.group('ConnectionManager', (group) => {
manager.patch('primary', getConfig())
manager.connect('primary')
}).waitForDone()

test('get health check report for connections that has enabled health checks', async ({
assert,
}) => {
const manager = new ConnectionManager(logger, createEmitter())
manager.add('primary', Object.assign({}, getConfig(), { healthCheck: true }))
manager.add('secondary', Object.assign({}, getConfig(), { healthCheck: true }))
manager.add('secondary-copy', Object.assign({}, getConfig(), { healthCheck: false }))

const report = await manager.report()
assert.equal(report.health.healthy, true)
assert.equal(report.health.message, 'All connections are healthy')
assert.lengthOf(report.meta, 2)
assert.deepEqual(
report.meta.map((node: any) => node.connection),
['primary', 'secondary']
)

await manager.closeAll()
})

test('get health check report when one of the connection is unhealthy', async ({ assert }) => {
const manager = new ConnectionManager(logger, createEmitter())
manager.add('primary', Object.assign({}, getConfig(), { healthCheck: true }))
manager.add(
'secondary',
Object.assign({}, getConfig(), {
healthCheck: true,
connection: ['sqlite', 'better_sqlite'].includes(process.env.DB!)
? {
filename: join(SQLITE_BASE_PATH, 'nested', 'db.sqlite'),
}
: {
host: 'bad-host',
},
})
)
manager.add('secondary-copy', Object.assign({}, getConfig(), { healthCheck: false }))

const report = await manager.report()
assert.equal(report.health.healthy, false)
assert.equal(report.health.message, 'One or more connections are not healthy')
assert.lengthOf(report.meta, 2)
assert.deepEqual(
report.meta.map((node: any) => node.connection),
['primary', 'secondary']
)
await manager.closeAll()
}).timeout(0)
})
13 changes: 0 additions & 13 deletions test/database/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,6 @@ test.group('Database', (group) => {
assert.isDefined(result)
await db.manager.closeAll()
})

test('set hasHealthChecks enabled flag to true, when one ore more connections are using health checks', async ({
assert,
}) => {
const config = {
connection: 'primary',
connections: { primary: Object.assign({}, getConfig(), { healthCheck: true }) },
}

const db = new Database(config, logger, createEmitter())
assert.isTrue(db.hasHealthChecksEnabled)
await db.manager.closeAll()
})
})

test.group('Database | global transaction', (group) => {
Expand Down
12 changes: 6 additions & 6 deletions test/orm/model_many_to_many.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,8 +775,8 @@ test.group('Model | ManyToMany | bulk operations', (group) => {
assert.equal(skills[0].name, 'Programming')
assert.equal(skills[0].$extras.pivot_user_id, 1)
assert.equal(skills[0].$extras.pivot_skill_id, 1)
assert.instanceOf(skills[0].$extras.pivot_created_at, DateTime)
assert.instanceOf(skills[0].$extras.pivot_updated_at, DateTime)
assert.instanceOf(skills[0].$extras.pivot_created_at, DateTime as any)
assert.instanceOf(skills[0].$extras.pivot_updated_at, DateTime as any)
})
})

Expand Down Expand Up @@ -1337,8 +1337,8 @@ test.group('Model | ManyToMany | preload', (group) => {
assert.equal(users[0].skills[0].name, 'Programming')
assert.equal(users[0].skills[0].$extras.pivot_user_id, 1)
assert.equal(users[0].skills[0].$extras.pivot_skill_id, 1)
assert.instanceOf(users[0].skills[0].$extras.pivot_created_at, DateTime)
assert.instanceOf(users[0].skills[0].$extras.pivot_updated_at, DateTime)
assert.instanceOf(users[0].skills[0].$extras.pivot_created_at, DateTime as any)
assert.instanceOf(users[0].skills[0].$extras.pivot_updated_at, DateTime as any)
})

test('preload relation for many', async ({ fs, assert }) => {
Expand Down Expand Up @@ -1540,8 +1540,8 @@ test.group('Model | ManyToMany | preload', (group) => {
assert.equal(users[0].skills[0].name, 'Programming')
assert.equal(users[0].skills[0].$extras.pivot_user_id, 1)
assert.equal(users[0].skills[0].$extras.pivot_skill_id, 1)
assert.instanceOf(users[0].skills[0].$extras.pivot_created_at, DateTime)
assert.instanceOf(users[0].skills[0].$extras.pivot_updated_at, DateTime)
assert.instanceOf(users[0].skills[0].$extras.pivot_created_at, DateTime as any)
assert.instanceOf(users[0].skills[0].$extras.pivot_updated_at, DateTime as any)
})

test('select extra pivot columns', async ({ fs, assert }) => {
Expand Down

0 comments on commit 32f377e

Please sign in to comment.