-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add /readyz readiness probe with database check #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { json } from '@sveltejs/kit'; | ||
| import { sql } from 'drizzle-orm'; | ||
| import { db } from '$lib/server/db'; | ||
| import type { RequestHandler } from './$types'; | ||
|
|
||
| // Readiness probe: reports 200 only when the app can actually serve traffic | ||
| // (i.e. the database is reachable). This is distinct from /health, which is a | ||
| // pure liveness check. Orchestrator readiness gates should point here; the | ||
| // container liveness HEALTHCHECK stays on /health. | ||
| // A readiness verdict must never be cached: the endpoint flips between | ||
| // 200/ready and 503/unavailable, and a stale cached response could mis-gate | ||
| // traffic. Mark every response no-store so no intermediary reuses it. | ||
| const NO_STORE = { 'Cache-Control': 'no-store' }; | ||
|
|
||
| export const GET: RequestHandler = async () => { | ||
| try { | ||
| await db.execute(sql`select 1`); | ||
| } catch (err) { | ||
| console.error('[readyz] database check failed', err); | ||
| return json( | ||
| { status: 'unavailable', checks: { database: 'down' } }, | ||
| { status: 503, headers: NO_STORE } | ||
| ); | ||
| } | ||
|
|
||
| return json({ status: 'ready', checks: { database: 'ok' } }, { headers: NO_STORE }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { test, expect } from '@playwright/test'; | ||
|
|
||
| test('readiness endpoint reports ready when the database is reachable', async ({ request }) => { | ||
| const response = await request.get('/readyz'); | ||
| expect(response.status()).toBe(200); | ||
| const body = await response.json(); | ||
| expect(body.status).toBe('ready'); | ||
| expect(body.checks.database).toBe('ok'); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
|
|
||
| const { executeMock } = vi.hoisted(() => ({ | ||
| executeMock: vi.fn() | ||
| })); | ||
|
|
||
| vi.mock('$lib/server/db', () => ({ db: { execute: executeMock } })); | ||
|
|
||
| vi.mock('drizzle-orm', () => ({ | ||
| sql: (strings: TemplateStringsArray) => strings.join('') | ||
| })); | ||
|
|
||
| // SvelteKit's generated $types is not available under vitest; the endpoint only | ||
| // uses the type import, so stub it out. | ||
| vi.mock('./$types', () => ({})); | ||
|
|
||
| import { GET } from '../../src/routes/readyz/+server'; | ||
|
|
||
| // The route handler only reads request-independent state, so an empty event is fine. | ||
| const callGet = () => GET({} as never); | ||
|
|
||
| describe('/readyz readiness probe', () => { | ||
| beforeEach(() => { | ||
| executeMock.mockReset(); | ||
| }); | ||
|
|
||
| it('returns 200/ready with no-store when the database is reachable', async () => { | ||
| executeMock.mockResolvedValueOnce(undefined); | ||
|
|
||
| const response = await callGet(); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(response.headers.get('cache-control')).toBe('no-store'); | ||
| expect(await response.json()).toEqual({ status: 'ready', checks: { database: 'ok' } }); | ||
| }); | ||
|
|
||
| it('returns 503/unavailable with no-store when the database check throws', async () => { | ||
| const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
| executeMock.mockRejectedValueOnce(new Error('connection refused')); | ||
|
|
||
| const response = await callGet(); | ||
|
|
||
| expect(response.status).toBe(503); | ||
| expect(response.headers.get('cache-control')).toBe('no-store'); | ||
| expect(await response.json()).toEqual({ | ||
| status: 'unavailable', | ||
| checks: { database: 'down' } | ||
| }); | ||
| expect(consoleError).toHaveBeenCalled(); | ||
|
|
||
| consoleError.mockRestore(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.