在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):godaddy/terminus开源软件地址(OpenSource Url):https://github.com/godaddy/terminus开源编程语言(OpenSource Language):JavaScript 95.8%开源软件介绍(OpenSource Introduction):terminusAdds graceful shutdown and Kubernetes readiness / liveness checks for any HTTP applications. InstallationInstall via npm: npm i @godaddy/terminus --save Usageconst http = require('http');
const { createTerminus } = require('@godaddy/terminus');
function onSignal () {
console.log('server is starting cleanup');
return Promise.all([
// your clean logic, like closing database connections
]);
}
function onShutdown () {
console.log('cleanup finished, server is shutting down');
}
function healthCheck ({ state }) {
// `state.isShuttingDown` (boolean) shows whether the server is shutting down or not
return Promise.resolve(
// optionally include a resolve value to be included as
// info in the health check response
)
}
const server = http.createServer((request, response) => {
response.end(
`<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>`
);
})
const options = {
// health check options
healthChecks: {
'/healthcheck': healthCheck, // a function accepting a state and returning a promise indicating service health,
verbatim: true, // [optional = false] use object returned from /healthcheck verbatim in response,
__unsafeExposeStackTraces: true // [optional = false] return stack traces in error response if healthchecks throw errors
},
caseInsensitive, // [optional] whether given health checks routes are case insensitive (defaults to false)
statusOk, // [optional = 200] status to be returned for successful healthchecks
statusError, // [optional = 503] status to be returned for unsuccessful healthchecks
// cleanup options
timeout: 1000, // [optional = 1000] number of milliseconds before forceful exiting
signal, // [optional = 'SIGTERM'] what signal to listen for relative to shutdown
signals, // [optional = []] array of signals to listen for relative to shutdown
useExit0, // [optional = false] instead of sending the received signal again without beeing catched, the process will exit(0)
sendFailuresDuringShutdown, // [optional = true] whether or not to send failure (503) during shutdown
beforeShutdown, // [optional] called before the HTTP server starts its shutdown
onSignal, // [optional] cleanup function, returning a promise (used to be onSigterm)
onShutdown, // [optional] called right before exiting
onSendFailureDuringShutdown, // [optional] called before sending each 503 during shutdowns
// both
logger // [optional] logger function to be called with errors. Example logger call: ('error happened during shutdown', error). See terminus.js for more details.
};
createTerminus(server, options);
server.listen(PORT || 3000); With custom error messagesconst http = require('http');
const { createTerminus, HealthCheckError } = require('@godaddy/terminus');
createTerminus(server, {
healthChecks: {
'/healthcheck': async function () {
const errors = []
return Promise.all([
// all your health checks goes here
].map(p => p.catch((error) => {
// silently collecting all the errors
errors.push(error)
return undefined
}))).then(() => {
if (errors.length) {
throw new HealthCheckError('healthcheck failed', errors)
}
})
}
}
}); With custom headersconst http = require("http");
const express = require("express");
const { createTerminus, HealthCheckError } = require('@godaddy/terminus');
const app = express();
app.get("/", (req, res) => {
res.send("ok");
});
const server = http.createServer(app);
function healthCheck({ state }) {
return Promise.resolve();
}
const options = {
healthChecks: {
"/healthcheck": healthCheck,
verbatim: true,
__unsafeExposeStackTraces: true,
},
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS, POST, GET",
},
};
terminus.createTerminus(server, options);
server.listen(3000); With expressconst http = require('http');
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('ok');
});
const server = http.createServer(app);
const options = {
// opts
};
createTerminus(server, options);
server.listen(PORT || 3000); With koaconst http = require('http');
const Koa = require('koa');
const app = new Koa();
const server = http.createServer(app.callback());
const options = {
// opts
};
createTerminus(server, options);
server.listen(PORT || 3000); How to set Terminus up with Kubernetes?When Kubernetes or a user deletes a Pod, Kubernetes will notify it and wait for During that time window (30 seconds by default), the Pod is in the
During this time, it is possible that load-balancers (like the nginx ingress controller) don't remove the Pods "in time", and when the Pod dies, it kills live connections. To make sure you don't lose any connections, we recommend delaying the shutdown with the number of milliseconds that's defined by the readiness probe in your deployment configuration.
To help with this, terminus exposes an option called Also it makes sense to use the function beforeShutdown () {
// given your readiness probes run every 5 second
// may be worth using a bigger number so you won't
// run into any race conditions
return new Promise(resolve => {
setTimeout(resolve, 5000)
})
}
createTerminus(server, {
beforeShutdown,
useExit0: true
}) Limited Windows supportDue to inherent platform limitations, |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论