Skip to content

Commit 133b153

Browse files
committed
Make the health behave like the ready
1 parent d06d77a commit 133b153

File tree

4 files changed

+43
-45
lines changed

4 files changed

+43
-45
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ npm install @thetypefounders/container-well-being --save
1111
## Usage
1212

1313
```javascript
14-
import express from 'express';
1514
import { Status } from '@thetypefounders/container-well-being';
15+
import express from 'express';
1616

1717
const status = new Status({
1818
// The host to bind to.

package-lock.json

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@thetypefounders/container-well-being",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"license": "Apache-2.0",
55
"author": "Ivan Ukhov <ivan.ukhov@gmail.com>",
66
"description": "Standard checks and probes for containers on Kubernetes",
@@ -15,11 +15,11 @@
1515
"express": "^4.21.2"
1616
},
1717
"devDependencies": {
18-
"@trivago/prettier-plugin-sort-imports": "^5.2.1",
18+
"@trivago/prettier-plugin-sort-imports": "^5.2.2",
1919
"@types/express": "^5.0.0",
20-
"@types/node": "^22.13.2",
21-
"prettier": "^3.4.2",
22-
"typescript": "^5.7.3"
20+
"@types/node": "^22.13.9",
21+
"prettier": "^3.5.3",
22+
"typescript": "^5.8.2"
2323
},
2424
"scripts": {
2525
"build": "tsc",

src/index.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,17 @@ export class Status {
3838

3939
const app = express();
4040

41-
app.get(
42-
'/health',
43-
(request: express.Request, response: express.Response) => {
44-
response
45-
.status(self.options.successCode!)
46-
.send(self.options.successBody);
47-
}
48-
);
41+
app.get('/health', (request, response) => {
42+
self.onReady(request, response);
43+
});
4944

50-
app.get('/live', (request: express.Request, response: express.Response) => {
51-
response.status(self.options.successCode!).send(self.options.successBody);
45+
app.get('/live', (request, response) => {
46+
self.onLive(request, response);
5247
});
5348

54-
app.get(
55-
'/ready',
56-
(request: express.Request, response: express.Response) => {
57-
if (!self.started || self.stopped) {
58-
response
59-
.status(self.options.failureCode!)
60-
.send(self.options.failureBody);
61-
} else {
62-
response
63-
.status(self.options.successCode!)
64-
.send(self.options.successBody);
65-
}
66-
}
67-
);
49+
app.get('/ready', (request, response) => {
50+
self.onReady(request, response);
51+
});
6852

6953
process.on('SIGTERM', () => {
7054
self.stop();
@@ -78,6 +62,7 @@ export class Status {
7862
}
7963

8064
start(): void {
65+
this.options.logger?.info('Starting to pass the readiness probe...');
8166
this.started = true;
8267
}
8368

@@ -88,6 +73,7 @@ export class Status {
8873
`Sleeping for ${self.options.graceBeforeSeconds} seconds...`
8974
);
9075
setTimeout(() => {
76+
self.options.logger?.info('Starting to fail the readiness probe...');
9177
self.stopped = true;
9278
self.options.logger?.info(
9379
`Sleeping for ${self.options.graceAfterSeconds} seconds...`
@@ -98,4 +84,16 @@ export class Status {
9884
}, 1000 * self.options.graceAfterSeconds!);
9985
}, 1000 * self.options.graceBeforeSeconds!);
10086
}
87+
88+
private onLive(request: express.Request, response: express.Response): void {
89+
response.status(this.options.successCode!).send(this.options.successBody);
90+
}
91+
92+
private onReady(request: express.Request, response: express.Response): void {
93+
if (!this.started || this.stopped) {
94+
response.status(this.options.failureCode!).send(this.options.failureBody);
95+
} else {
96+
response.status(this.options.successCode!).send(this.options.successBody);
97+
}
98+
}
10199
}

0 commit comments

Comments
 (0)