improve healthcheck
This commit is contained in:
@@ -31,8 +31,15 @@ declare module 'fastify' {
|
|||||||
|
|
||||||
async function withTimings<T>(promise: Promise<T>) {
|
async function withTimings<T>(promise: Promise<T>) {
|
||||||
const time = performance.now();
|
const time = performance.now();
|
||||||
const res = await promise;
|
try {
|
||||||
return [round(performance.now() - time, 2), res] as const;
|
const data = await promise;
|
||||||
|
return {
|
||||||
|
time: round(performance.now() - time, 2),
|
||||||
|
data,
|
||||||
|
} as const;
|
||||||
|
} catch (e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const port = parseInt(process.env.API_PORT || '3000', 10);
|
const port = parseInt(process.env.API_PORT || '3000', 10);
|
||||||
@@ -91,44 +98,37 @@ const startServer = async () => {
|
|||||||
reply.send({ name: 'openpanel sdk api' });
|
reply.send({ name: 'openpanel sdk api' });
|
||||||
});
|
});
|
||||||
fastify.get('/healthcheck', async (request, reply) => {
|
fastify.get('/healthcheck', async (request, reply) => {
|
||||||
const redisRes = await withTimings(redis.keys('*')).catch(
|
const redisRes = await withTimings(redis.keys('*'));
|
||||||
(e: Error) => e
|
const dbRes = await withTimings(db.project.findFirst());
|
||||||
);
|
const queueRes = await withTimings(eventsQueue.getCompleted());
|
||||||
const dbRes = await withTimings(db.project.findFirst()).catch(
|
const chRes = await withTimings(chQuery('SELECT * FROM events LIMIT 1'));
|
||||||
(e: Error) => e
|
const status = redisRes && dbRes && queueRes && chRes ? 200 : 500;
|
||||||
);
|
|
||||||
const queueRes = await withTimings(eventsQueue.getCompleted()).catch(
|
|
||||||
(e: Error) => e
|
|
||||||
);
|
|
||||||
const chRes = await withTimings(
|
|
||||||
chQuery('SELECT * FROM events LIMIT 1')
|
|
||||||
).catch((e: Error) => e);
|
|
||||||
|
|
||||||
reply.send({
|
reply.status(status).send({
|
||||||
redis: Array.isArray(redisRes)
|
redis: redisRes
|
||||||
? {
|
? {
|
||||||
ok: redisRes[1].length ? true : false,
|
ok: !!redisRes.data.length,
|
||||||
time: `${redisRes[0]}ms`,
|
time: `${redisRes.time}ms`,
|
||||||
}
|
}
|
||||||
: redisRes,
|
: null,
|
||||||
db: Array.isArray(dbRes)
|
db: dbRes
|
||||||
? {
|
? {
|
||||||
ok: dbRes[1] ? true : false,
|
ok: !!dbRes.data,
|
||||||
time: `${dbRes[0]}ms`,
|
time: `${dbRes.time}ms`,
|
||||||
}
|
}
|
||||||
: dbRes,
|
: null,
|
||||||
queue: Array.isArray(queueRes)
|
queue: queueRes
|
||||||
? {
|
? {
|
||||||
ok: queueRes[1].length ? true : false,
|
ok: !!queueRes.data,
|
||||||
time: `${queueRes[0]}ms`,
|
time: `${queueRes.time}ms`,
|
||||||
}
|
}
|
||||||
: queueRes,
|
: null,
|
||||||
ch: Array.isArray(chRes)
|
ch: chRes
|
||||||
? {
|
? {
|
||||||
ok: chRes[1].length ? true : false,
|
ok: !!chRes.data,
|
||||||
time: `${chRes[0]}ms`,
|
time: `${chRes.time}ms`,
|
||||||
}
|
}
|
||||||
: chRes,
|
: null,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
if (process.env.NODE_ENV === 'production') {
|
if (process.env.NODE_ENV === 'production') {
|
||||||
|
|||||||
Reference in New Issue
Block a user