Skip to content

Commit 6fc19a3

Browse files
committed
feat: add support to configure health checks via configure command
1 parent dede44f commit 6fc19a3

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

commands/configure.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* file that was distributed with this source code.
88
*/
99

10+
import { stubsRoot } from '../stubs/main.js'
1011
import type { CommandOptions } from '../types/ace.js'
1112
import { args, BaseCommand, flags } from '../modules/ace/main.js'
1213

@@ -97,6 +98,17 @@ export default class Configure extends BaseCommand {
9798
})
9899
}
99100

101+
/**
102+
* Configure health checks
103+
*/
104+
async #configureHealthChecks() {
105+
const codemods = await this.createCodemods()
106+
await codemods.makeUsingStub(stubsRoot, 'make/health/main.stub', {
107+
flags: this.parsed.flags,
108+
entity: this.app.generators.createEntity('health'),
109+
})
110+
}
111+
100112
/**
101113
* Creates codemods as per configure command options
102114
*/
@@ -117,6 +129,9 @@ export default class Configure extends BaseCommand {
117129
if (this.name === 'edge') {
118130
return this.#configureEdge()
119131
}
132+
if (this.name === 'health_checks') {
133+
return this.#configureHealthChecks()
134+
}
120135

121136
const packageExports = await this.#getPackageSource(this.name)
122137
if (!packageExports) {

stubs/make/health/main.stub

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{#var preloadFileName = string(entity.name).snakeCase().removeExtension().ext('.ts').toString()}}
2+
{{{
3+
exports({
4+
to: app.startPath(entity.path, preloadFileName)
5+
})
6+
}}}
7+
import {
8+
HealthChecks,
9+
DiskSpaceHealthCheck,
10+
MemoryHeapHealthCheck,
11+
} from '@adonisjs/core/health'
12+
13+
export const healthChecks = new HealthChecks().register([
14+
new DiskSpaceHealthCheck(),
15+
new MemoryHeapHealthCheck(),
16+
])

tests/commands/configure.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,33 @@ test.group('Configure command | edge', (group) => {
411411
)
412412
})
413413
})
414+
415+
test.group('Configure command | health checks', (group) => {
416+
group.each.disableTimeout()
417+
418+
test('create start/health file with some default checks', async ({ assert, fs }) => {
419+
const ace = await new AceFactory().make(fs.baseUrl)
420+
await ace.app.init()
421+
ace.ui.switchMode('raw')
422+
423+
await fs.createJson('tsconfig.json', {})
424+
await fs.create('adonisrc.ts', 'export default defineConfig({})')
425+
426+
const command = await ace.create(Configure, ['health_checks'])
427+
428+
await command.run()
429+
430+
assert.deepEqual(command.ui.logger.getLogs(), [
431+
{
432+
message: 'green(DONE:) create start/health.ts',
433+
stream: 'stdout',
434+
},
435+
])
436+
437+
await assert.fileContains('start/health.ts', [
438+
'new DiskSpaceHealthCheck()',
439+
'new MemoryHeapHealthCheck()',
440+
'export const healthChecks = ',
441+
])
442+
})
443+
})

0 commit comments

Comments
 (0)