Compare commits
2
Commits
c8eb9bc862
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
138ea78ec7 | ||
|
|
474b54e6ec |
No files matched your search
@@ -55,8 +55,9 @@ Monitor these production checks:
|
||||
- Verifies Nginx is serving the frontend container.
|
||||
- Backend liveness: `GET https://your-host/api/health/live`
|
||||
- Verifies the API process is running.
|
||||
- Backend readiness: `GET https://your-host/api/health/ready`
|
||||
- Verifies the API can reach Postgres and Redis. Returns `503` if either dependency is unavailable.
|
||||
- Application health: `GET https://your-host/api/health` (alias: `/api/health/ready`)
|
||||
- Use this endpoint for application availability monitoring. Verifies Postgres and Redis, returning HTTP `503` if a check fails or exceeds four seconds, and HTTP `200` only when all checks pass. Responses are not cacheable.
|
||||
- Connection failures and timeouts also mean unavailable: during a startup database failure, the API may not be listening at all.
|
||||
- Backend metrics: `GET https://your-host/api/metrics`
|
||||
- Admin-authenticated process, request, and queue metrics.
|
||||
- Postgres and Redis:
|
||||
|
||||
+9
-17
@@ -1,3 +1,4 @@
|
||||
import { createReadinessHandler } from './health/readiness.js';
|
||||
import { photoCacheControl } from './storage/photoCache.js';
|
||||
import { canUpdateWorkspaceSettings, subscriptionAllowsWrite } from './workspaceAccess.js';
|
||||
import { buildWorkspaceInviteEmail } from './emails/workspaceInviteEmail.js';
|
||||
@@ -2210,28 +2211,19 @@ const writeBirdTimelineEvent = async ({
|
||||
}
|
||||
};
|
||||
|
||||
app.get('/api/health', (_req: Request, res: Response) => {
|
||||
res.json({ ok: true });
|
||||
});
|
||||
const readinessHandler = createReadinessHandler([
|
||||
() => db.query('SELECT 1'),
|
||||
() => getBirdMilestoneReminderQueueCounts(),
|
||||
() => getMedicationReminderQueueCounts(),
|
||||
]);
|
||||
|
||||
app.get(['/api/health', '/api/health/ready'], readinessHandler);
|
||||
|
||||
app.get('/api/health/live', (_req: Request, res: Response) => {
|
||||
res.set('Cache-Control', 'no-store');
|
||||
res.json({ ok: true, status: 'live' });
|
||||
});
|
||||
|
||||
app.get('/api/health/ready', async (_req: Request, res: Response) => {
|
||||
try {
|
||||
await Promise.all([
|
||||
db.query('SELECT 1'),
|
||||
getBirdMilestoneReminderQueueCounts(),
|
||||
getMedicationReminderQueueCounts(),
|
||||
]);
|
||||
res.json({ ok: true, status: 'ready' });
|
||||
} catch (error) {
|
||||
console.error('Readiness check failed', error);
|
||||
res.status(503).json({ ok: false, status: 'not_ready' });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/metrics', requireAuth, requireAdmin, async (_req: Request, res: Response, next: NextFunction) => {
|
||||
const memoryUsage = process.memoryUsage();
|
||||
const averageDurationMs = requestMetrics.totalRequests > 0 ? requestMetrics.totalDurationMs / requestMetrics.totalRequests : 0;
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { test } from 'node:test';
|
||||
import type { Request, Response } from 'express';
|
||||
import { createReadinessHandler } from './readiness.js';
|
||||
|
||||
async function probe(checks: Array<() => Promise<unknown>>) {
|
||||
const result = { code: 0, cache: '', body: {} };
|
||||
const response = {
|
||||
set(_name: string, value: string) { result.cache = value; return this; },
|
||||
status(code: number) { result.code = code; return this; },
|
||||
json(body: unknown) { result.body = body as object; return this; },
|
||||
} as unknown as Response;
|
||||
await createReadinessHandler(checks, 20, () => {} )({} as Request, response);
|
||||
return result;
|
||||
}
|
||||
|
||||
test('healthy dependencies return uncached HTTP 200', async () => {
|
||||
const result = await probe([async () => 1, async () => ({})]);
|
||||
assert.equal(result.code, 200);
|
||||
assert.equal(result.cache, 'no-store');
|
||||
assert.deepEqual(result.body, { ok: true, status: 'ready' });
|
||||
});
|
||||
|
||||
for (const dependency of ['postgres', 'redis']) {
|
||||
test(`${dependency} failure returns HTTP 503`, async () => {
|
||||
const checks = [async () => 1, async () => 1];
|
||||
checks[dependency === 'postgres' ? 0 : 1] = async () => { throw new Error('unavailable'); };
|
||||
const result = await probe(checks);
|
||||
assert.equal(result.code, 503);
|
||||
assert.equal(result.cache, 'no-store');
|
||||
assert.deepEqual(result.body, { ok: false, status: 'not_ready' });
|
||||
});
|
||||
}
|
||||
|
||||
test('unresponsive dependencies return HTTP 503 within the deadline', async () => {
|
||||
const result = await probe([() => new Promise(() => {})]);
|
||||
assert.equal(result.code, 503);
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import type { Request, Response } from 'express';
|
||||
|
||||
/** Dependency readiness shared by the public health and readiness endpoints. */
|
||||
export const createReadinessHandler = (
|
||||
checks: Array<() => Promise<unknown>>,
|
||||
timeoutMs = 4000,
|
||||
reportError: (error: unknown) => void = (error) => console.error('Readiness check failed', error),
|
||||
) => async (_req: Request, res: Response) => {
|
||||
res.set('Cache-Control', 'no-store');
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
try {
|
||||
await Promise.race([
|
||||
Promise.all(checks.map((check) => Promise.resolve().then(check))),
|
||||
new Promise<never>((_resolve, reject) => {
|
||||
timer = setTimeout(() => reject(new Error('Readiness dependencies timed out')), timeoutMs);
|
||||
}),
|
||||
]);
|
||||
res.status(200).json({ ok: true, status: 'ready' });
|
||||
} catch (error) {
|
||||
reportError(error);
|
||||
res.status(503).json({ ok: false, status: 'not_ready' });
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
};
|
||||
+19
-2
@@ -8,10 +8,27 @@
|
||||
type="image/svg+xml"
|
||||
href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Cdefs%3E%3ClinearGradient id='featherFill' x1='0%25' y1='0%25' x2='100%25' y2='100%25'%3E%3Cstop offset='0%25' stop-color='%23cb3a35'/%3E%3Cstop offset='30%25' stop-color='%23f0b63f'/%3E%3Cstop offset='58%25' stop-color='%23238a5a'/%3E%3Cstop offset='100%25' stop-color='%232769b3'/%3E%3C/linearGradient%3E%3C/defs%3E%3Cpath d='M50.8 10.4C37.9 10.3 27 18.5 22.7 31.1c-3.1 9.1-2.1 18.5-8.6 24.8c-1.5 1.5-0.2 4 1.9 3.6c8.4-1.5 14.6-6.7 18.6-13.7c1 0.5 2.2 0.8 3.4 0.8c3.5 0 6.5-2.3 7.5-5.4c1.9-0.4 3.7-1.3 5.1-2.7c2-2 3-4.6 3.1-7.2c3.3-5.8 4.9-12.9 1.4-20.2c-0.7-1.3-2-0.7-4.3-0.7Z' fill='url(%23featherFill)'/%3E%3Cpath d='M18 56c8.5-3.4 14.2-9.8 18.1-17.8M26.9 48.9c6.9-7.2 13.5-14.8 20.3-22.1M31.8 41.2c6.4-1.3 12.1-4.6 16.5-9.4M36.8 33.8c4.9-0.9 9.2-3.4 12.6-7.1' fill='none' stroke='%23fff8ef' stroke-linecap='round' stroke-width='2.6'/%3E%3Cpath d='M18 56c8.5-3.4 14.2-9.8 18.1-17.8' fill='none' stroke='%2363562d' stroke-linecap='round' stroke-width='2.2'/%3E%3C/svg%3E"
|
||||
/>
|
||||
<title>FlockPal</title>
|
||||
<title>FlockPal | Bird Care, Health Records & Flock Management</title>
|
||||
<meta
|
||||
name="description"
|
||||
content="Manage your flock with FlockPal. Track bird weights, health records, medications, and care routines in one shared workspace for bird households and rescues."
|
||||
/>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<div id="root">
|
||||
<!-- React replaces this initial content when the app loads. -->
|
||||
<main class="auth-shell">
|
||||
<section class="panel auth-panel">
|
||||
<div class="auth-copy">
|
||||
<h1>Organized care for companion birds and rescue flocks</h1>
|
||||
<p class="muted">
|
||||
Keep every bird's care story in one place; your flock's health, history, and routines together and easier to visualize.
|
||||
</p>
|
||||
<noscript>Enable JavaScript to sign in and manage your flock.</noscript>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
</div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user