-
Notifications
You must be signed in to change notification settings - Fork 122
FE-1569: Add durable Brunch storage and telemetry #9487
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /** | ||
| * The deployed conversation-store contract. | ||
| * | ||
| * Production accepts only dedicated Postgres fields. Local development and | ||
| * hermetic tests keep the existing SQLite path, but production can never | ||
| * silently select it. | ||
| */ | ||
|
|
||
| export const POSTGRES_ENV = { | ||
| authMode: "BRUNCH_POSTGRES_AUTH_MODE", | ||
| awsRegion: "BRUNCH_POSTGRES_AWS_REGION", | ||
| database: "BRUNCH_POSTGRES_DATABASE", | ||
| host: "BRUNCH_POSTGRES_HOST", | ||
| password: "BRUNCH_POSTGRES_PASSWORD", | ||
| port: "BRUNCH_POSTGRES_PORT", | ||
| tlsCaPath: "BRUNCH_POSTGRES_TLS_CA_PATH", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not be a required environment. It should be possible to connect to Postgres without |
||
| user: "BRUNCH_POSTGRES_USER", | ||
| } as const; | ||
|
|
||
| export interface SqliteDatabaseConfig { | ||
| readonly kind: "sqlite"; | ||
| } | ||
|
|
||
| export interface PostgresDatabaseConfig { | ||
| readonly kind: "postgres"; | ||
| readonly auth: | ||
| | { | ||
| readonly mode: "iam"; | ||
| readonly region: string; | ||
| } | ||
| | { | ||
| readonly mode: "password"; | ||
| readonly password: string; | ||
| }; | ||
| readonly database: string; | ||
| readonly host: string; | ||
| readonly port: number; | ||
| readonly tlsCaPath: string; | ||
| readonly user: string; | ||
| } | ||
|
|
||
| export type DatabaseConfig = SqliteDatabaseConfig | PostgresDatabaseConfig; | ||
|
|
||
| type Environment = Readonly<Record<string, string | undefined>>; | ||
|
|
||
| const valueOf = (environment: Environment, name: string): string => { | ||
| const value = environment[name]?.trim(); | ||
| if (value === undefined || value.length === 0) { | ||
| throw new Error(`Production database configuration requires ${name}.`); | ||
| } | ||
| return value; | ||
| }; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| const absent = (environment: Environment, name: string): void => { | ||
| if (environment[name] !== undefined) { | ||
| throw new Error( | ||
| `Production database configuration does not accept ${name}.`, | ||
| ); | ||
| } | ||
| }; | ||
|
Comment on lines
+54
to
+60
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error on an env-var being set is a weird behavior, it should at most warn. |
||
|
|
||
| const portOf = (environment: Environment): number => { | ||
| const name = POSTGRES_ENV.port; | ||
| const source = valueOf(environment, name); | ||
| if (!/^\d+$/u.test(source)) { | ||
| throw new Error(`${name} must be an integer between 1 and 65535.`); | ||
| } | ||
| const port = Number(source); | ||
| if (!Number.isSafeInteger(port) || port < 1 || port > 65_535) { | ||
| throw new Error(`${name} must be an integer between 1 and 65535.`); | ||
| } | ||
| return port; | ||
| }; | ||
|
|
||
| const rejectLegacyProductionInputs = (environment: Environment): void => { | ||
| absent(environment, "DATABASE_URL"); | ||
| absent(environment, "BRUNCH_DEV_DB_PATH"); | ||
| absent(environment, "BRUNCH_CHAT_DB_PATH"); | ||
| }; | ||
|
|
||
| export function loadDatabaseConfig( | ||
| environment: Environment = process.env, | ||
| ): DatabaseConfig { | ||
| if (environment.NODE_ENV !== "production") { | ||
| return { kind: "sqlite" }; | ||
| } | ||
|
Comment on lines
+84
to
+86
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we cannot use Postgres in non-production builds? |
||
|
|
||
| rejectLegacyProductionInputs(environment); | ||
|
|
||
| const authMode = valueOf(environment, POSTGRES_ENV.authMode); | ||
| const common = { | ||
| kind: "postgres" as const, | ||
| database: valueOf(environment, POSTGRES_ENV.database), | ||
| host: valueOf(environment, POSTGRES_ENV.host), | ||
| port: portOf(environment), | ||
| tlsCaPath: valueOf(environment, POSTGRES_ENV.tlsCaPath), | ||
| user: valueOf(environment, POSTGRES_ENV.user), | ||
| }; | ||
|
|
||
| if (authMode === "iam") { | ||
| absent(environment, POSTGRES_ENV.password); | ||
| return { | ||
| ...common, | ||
| auth: { | ||
| mode: "iam", | ||
| region: valueOf(environment, POSTGRES_ENV.awsRegion), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| if (authMode === "password") { | ||
| absent(environment, POSTGRES_ENV.awsRegion); | ||
| return { | ||
| ...common, | ||
| auth: { | ||
| mode: "password", | ||
| password: valueOf(environment, POSTGRES_ENV.password), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| throw new Error( | ||
| `${POSTGRES_ENV.authMode} must be either "iam" or "password".`, | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,35 @@ | ||
| import { postgres } from "@flue/postgres"; | ||
|
|
||
| import { loadDatabaseConfig } from "./database-config.ts"; | ||
| import { conversationDbPath } from "./db-path.ts"; | ||
| import { createPostgresRunner } from "./postgres.ts"; | ||
| import { shutdownBrunchTelemetry } from "./telemetry-bootstrap.ts"; | ||
| import { recordOperationalFailure } from "./telemetry.ts"; | ||
|
|
||
| import type { DatabaseConfig } from "./database-config.ts"; | ||
|
|
||
| /** | ||
| * The substrate's conversation storage β host-authored because Flue requires | ||
| * it of the consuming app. | ||
| * | ||
| * Without this file conversations are process-memory and a restart loses them. | ||
| * Local development and hermetic tests retain SQLite. Production must provide | ||
| * the dedicated Postgres contract and cannot fall back to a task-local file. | ||
| */ | ||
| let config: DatabaseConfig; | ||
| try { | ||
| config = loadDatabaseConfig(); | ||
| } catch (error) { | ||
| try { | ||
| await recordOperationalFailure("database_configuration", error); | ||
| } catch { | ||
| // The database configuration error remains the authoritative startup cause. | ||
| } | ||
| throw error; | ||
| } | ||
|
Comment on lines
+19
to
+28
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't reading the CA file and pool-functions be wrapped as well? |
||
|
|
||
| import { sqlite } from "@flue/runtime/node"; | ||
|
|
||
| import { conversationDbPath } from "./db-path.ts"; | ||
| const database = | ||
| config.kind === "postgres" | ||
| ? postgres(createPostgresRunner(config, shutdownBrunchTelemetry)) | ||
| : (await import("@flue/runtime/node")).sqlite(conversationDbPath()); | ||
|
|
||
| export default sqlite(conversationDbPath()); | ||
| export default database; | ||
Uh oh!
There was an error while loading. Please reload this page.