diff --git a/README.md b/README.md index 996cb52..eb95861 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ npm install @thetypefounders/container-well-being --save ## Usage ```javascript -import express from 'express'; import { Status } from '@thetypefounders/container-well-being'; +import express from 'express'; const status = new Status({ // The host to bind to. diff --git a/package-lock.json b/package-lock.json index 9de245f..200341d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,22 +1,22 @@ { "name": "@thetypefounders/container-well-being", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@thetypefounders/container-well-being", - "version": "1.0.0", + "version": "1.0.1", "license": "Apache-2.0", "dependencies": { "express": "^4.21.2" }, "devDependencies": { - "@trivago/prettier-plugin-sort-imports": "^5.2.1", + "@trivago/prettier-plugin-sort-imports": "^5.2.2", "@types/express": "^5.0.0", - "@types/node": "^22.13.2", - "prettier": "^3.4.2", - "typescript": "^5.7.3" + "@types/node": "^22.13.9", + "prettier": "^3.5.3", + "typescript": "^5.8.2" } }, "node_modules/@babel/code-frame": { @@ -285,9 +285,9 @@ "license": "MIT" }, "node_modules/@types/node": { - "version": "22.13.2", - "resolved": "https://registry.npmjs.org/@types/node/-/node-22.13.2.tgz", - "integrity": "sha512-Z+r8y3XL9ZpI2EY52YYygAFmo2/oWfNSj4BCpAXE2McAexDk8VcnBMGC9Djn9gTKt4d2T/hhXqmPzo4hfIXtTg==", + "version": "22.13.9", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.13.9.tgz", + "integrity": "sha512-acBjXdRJ3A6Pb3tqnw9HZmyR3Fiol3aGxRCK1x3d+6CDAMjl7I649wpSd+yNURCjbOUGu9tqtLKnTGxmK6CyGw==", "dev": true, "license": "MIT", "dependencies": { @@ -986,9 +986,9 @@ "license": "ISC" }, "node_modules/prettier": { - "version": "3.5.0", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.5.0.tgz", - "integrity": "sha512-quyMrVt6svPS7CjQ9gKb3GLEX/rl3BCL2oa/QkNcXv4YNVBC9olt3s+H7ukto06q7B1Qz46PbrKLO34PR6vXcA==", + "version": "3.5.3", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.5.3.tgz", + "integrity": "sha512-QQtaxnoDJeAkDvDKWCLiwIXkTgRhwYDEQCghU9Z6q03iyek/rxRh/2lC3HB7P8sWT2xC/y5JDctPLBIGzHKbhw==", "dev": true, "license": "MIT", "bin": { @@ -1252,9 +1252,9 @@ } }, "node_modules/typescript": { - "version": "5.7.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.7.3.tgz", - "integrity": "sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw==", + "version": "5.8.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.8.2.tgz", + "integrity": "sha512-aJn6wq13/afZp/jT9QZmwEjDqqvSGp1VT5GVg+f/t6/oVyrgXM6BY1h9BRh/O5p3PlUPAe+WuiEZOmb/49RqoQ==", "dev": true, "license": "Apache-2.0", "bin": { diff --git a/package.json b/package.json index cbbca68..3c7a950 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@thetypefounders/container-well-being", - "version": "1.0.0", + "version": "1.0.1", "license": "Apache-2.0", "author": "Ivan Ukhov ", "description": "Standard checks and probes for containers on Kubernetes", @@ -15,11 +15,11 @@ "express": "^4.21.2" }, "devDependencies": { - "@trivago/prettier-plugin-sort-imports": "^5.2.1", + "@trivago/prettier-plugin-sort-imports": "^5.2.2", "@types/express": "^5.0.0", - "@types/node": "^22.13.2", - "prettier": "^3.4.2", - "typescript": "^5.7.3" + "@types/node": "^22.13.9", + "prettier": "^3.5.3", + "typescript": "^5.8.2" }, "scripts": { "build": "tsc", diff --git a/src/index.ts b/src/index.ts index db0334c..bd5b41b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,33 +38,17 @@ export class Status { const app = express(); - app.get( - '/health', - (request: express.Request, response: express.Response) => { - response - .status(self.options.successCode!) - .send(self.options.successBody); - } - ); + app.get('/health', (request, response) => { + self.onReady(request, response); + }); - app.get('/live', (request: express.Request, response: express.Response) => { - response.status(self.options.successCode!).send(self.options.successBody); + app.get('/live', (request, response) => { + self.onLive(request, response); }); - app.get( - '/ready', - (request: express.Request, response: express.Response) => { - if (!self.started || self.stopped) { - response - .status(self.options.failureCode!) - .send(self.options.failureBody); - } else { - response - .status(self.options.successCode!) - .send(self.options.successBody); - } - } - ); + app.get('/ready', (request, response) => { + self.onReady(request, response); + }); process.on('SIGTERM', () => { self.stop(); @@ -78,6 +62,7 @@ export class Status { } start(): void { + this.options.logger?.info('Starting to pass the readiness probe...'); this.started = true; } @@ -88,6 +73,7 @@ export class Status { `Sleeping for ${self.options.graceBeforeSeconds} seconds...` ); setTimeout(() => { + self.options.logger?.info('Starting to fail the readiness probe...'); self.stopped = true; self.options.logger?.info( `Sleeping for ${self.options.graceAfterSeconds} seconds...` @@ -98,4 +84,16 @@ export class Status { }, 1000 * self.options.graceAfterSeconds!); }, 1000 * self.options.graceBeforeSeconds!); } + + private onLive(request: express.Request, response: express.Response): void { + response.status(this.options.successCode!).send(this.options.successBody); + } + + private onReady(request: express.Request, response: express.Response): void { + if (!this.started || this.stopped) { + response.status(this.options.failureCode!).send(this.options.failureBody); + } else { + response.status(this.options.successCode!).send(this.options.successBody); + } + } }