Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(worker): set synchronous_commit in code instead of PGOPTIONS

Hampusf4316aa1534f
diff --git a/deploy/helm/fluxer/templates/apps.yaml b/deploy/helm/fluxer/templates/apps.yaml
index 235ac6100..0662b758e 100644
--- a/deploy/helm/fluxer/templates/apps.yaml
+++ b/deploy/helm/fluxer/templates/apps.yaml
@@ -16,7 +16,7 @@
{{- $heartbeat := dict "exec" (dict "command" (list "node" "-e" "const age=Date.now()-require('node:fs').statSync('/tmp/fluxer-worker-heartbeat').mtimeMs;if(age>30000){console.error('worker heartbeat is '+Math.round(age)+'ms old');process.exit(1)}")) "initialDelaySeconds" 90 "periodSeconds" 10 "timeoutSeconds" 5 "failureThreshold" 3 }}
{{- include "fluxer.app" (dict "root" . "name" "worker" "component" "worker" "w" $v.worker "image" "fluxer-api" "port" 8080 "noService" true
"command" (list "node" "dist/WorkerEntrypoint.js")
- "env" (concat (list (dict "name" "FLUXER_API_WORKER_MODE" "value" "all_lanes") (dict "name" "FLUXER_API_WORKER_ENABLE_CRON_SCHEDULER" "value" "true") (dict "name" "PGOPTIONS" "value" "-c synchronous_commit=off")) $nodeEnv)
+ "env" (concat (list (dict "name" "FLUXER_API_WORKER_MODE" "value" "all_lanes") (dict "name" "FLUXER_API_WORKER_ENABLE_CRON_SCHEDULER" "value" "true")) $nodeEnv)
"probes" (dict "liveness" $heartbeat)
"strategy" (dict "type" "Recreate")
"waitFor" (include "fluxer.appWaitFor" (dict "root" . "usesNats" true "usesPostgres" true "usesS3" true "usesKv" true "usesSvc" true) | fromJsonArray)) }}
diff --git a/fluxer_api/pkgs/postgres/src/Client.ts b/fluxer_api/pkgs/postgres/src/Client.ts
index ed7d3bcd2..a9274e8c3 100644
--- a/fluxer_api/pkgs/postgres/src/Client.ts
+++ b/fluxer_api/pkgs/postgres/src/Client.ts
@@ -7,6 +7,7 @@ import pg from 'pg';
const MAX_DIAGNOSTIC_FIELD_LENGTH = 128;
const INVALID_PASSWORD_CODE = '28P01';
const PASSWORD_MISMATCH_DOCS = 'https://docs.fluxer.app/operator/rexctl/#rexctl-doctor';
+const SYNCHRONOUS_COMMIT_LEVELS = new Set(['on', 'off', 'local', 'remote_write', 'remote_apply']);
interface PostgresConnectionDiagnostic {
phase: 'idle' | 'checked_out';
@@ -35,6 +36,7 @@ interface PostgresConfig {
maxConnections?: number;
kvTable?: string;
preparedStatements?: boolean;
+ synchronousCommit?: string;
}
export interface PostgresQueryable {
@@ -126,6 +128,11 @@ class PostgresClient implements IPostgresClient {
config: PostgresConfig,
private readonly onConnectionError: PostgresConnectionErrorReporter,
) {
+ if (config.synchronousCommit !== undefined && !SYNCHRONOUS_COMMIT_LEVELS.has(config.synchronousCommit)) {
+ throw new Error(
+ `Postgres synchronous_commit must be one of ${[...SYNCHRONOUS_COMMIT_LEVELS].join(', ')}, got ${JSON.stringify(config.synchronousCommit)}`,
+ );
+ }
this.config = {...config};
this.pool = null;
}
@@ -142,6 +149,7 @@ class PostgresClient implements IPostgresClient {
}
private async openPool(): Promise<void> {
+ const synchronousCommit = this.config.synchronousCommit;
const poolConfig: PoolConfig & {scramMaxIterations: number} = {
connectionString: this.config.url || undefined,
host: this.config.url ? undefined : (this.config.host ?? '127.0.0.1'),
@@ -152,6 +160,11 @@ class PostgresClient implements IPostgresClient {
ssl: this.config.ssl ? {rejectUnauthorized: true, ca: normalizePem(this.config.sslCa)} : undefined,
max: this.config.maxConnections ?? 20,
scramMaxIterations: 0,
+ onConnect: synchronousCommit
+ ? async (client) => {
+ await client.query(`SET synchronous_commit = ${synchronousCommit}`).catch(() => {});
+ }
+ : undefined,
};
const pool = new pg.Pool(poolConfig);
this.observePoolConnections(pool);
diff --git a/fluxer_api/src/api/worker/WorkerMain.ts b/fluxer_api/src/api/worker/WorkerMain.ts
index df2dbc0e1..1261c0f06 100644
--- a/fluxer_api/src/api/worker/WorkerMain.ts
+++ b/fluxer_api/src/api/worker/WorkerMain.ts
@@ -36,6 +36,7 @@ import {
import {initializeSearch, shutdownSearch} from '@app/api/SearchFactory';
import {accountStateDepsFromContext} from '@app/api/user/services/AccountStateApplier';
import {awaitAll} from '@app/api/utils/ConcurrencyUtils';
+import {readOptionalEnv} from '@app/api/utils/IntegerOptions';
import {startAccountActionConsumer, stopAccountActionConsumer} from '@app/api/worker/AccountActionConsumer';
import {queueBlocklistFeedStartupJobs} from '@app/api/worker/BlocklistFeedStartup';
import {CronScheduler} from '@app/api/worker/CronScheduler';
@@ -235,7 +236,13 @@ export async function startWorkerMain(): Promise<void> {
try {
if (Config.database.backend === 'postgres') {
- await initPostgres(Config.postgres, (diagnostic) => Logger.error(diagnostic, 'Postgres connection error'));
+ await initPostgres(
+ {
+ ...Config.postgres,
+ synchronousCommit: readOptionalEnv('FLUXER_WORKER_POSTGRES_SYNCHRONOUS_COMMIT')?.trim() ?? 'off',
+ },
+ (diagnostic) => Logger.error(diagnostic, 'Postgres connection error'),
+ );
postgresInitialized = true;
const postgres = getDefaultPostgresClient();
await ensurePostgresKvSchema(postgres);
diff --git a/fluxer_rexctl/internal/stack/files/docker-compose.yml b/fluxer_rexctl/internal/stack/files/docker-compose.yml
index 916c772e9..4327037be 100644
--- a/fluxer_rexctl/internal/stack/files/docker-compose.yml
+++ b/fluxer_rexctl/internal/stack/files/docker-compose.yml
@@ -534,7 +534,7 @@ services:
FLUXER_API_WORKER_MODE: all_lanes
FLUXER_API_WORKER_ENABLE_CRON_SCHEDULER: "true"
FLUXER_POSTGRES_MAX_CONNECTIONS: "${FLUXER_WORKER_POSTGRES_MAX_CONNECTIONS:-25}"
- PGOPTIONS: -c synchronous_commit=off
+ FLUXER_WORKER_POSTGRES_SYNCHRONOUS_COMMIT: "${FLUXER_WORKER_POSTGRES_SYNCHRONOUS_COMMIT:-}"
healthcheck:
test: ["CMD-SHELL", "node -e \"const age=Date.now()-require('node:fs').statSync('/tmp/fluxer-worker-heartbeat').mtimeMs;if(age>30000){console.error('worker heartbeat is '+Math.round(age)+'ms old');process.exit(1)}\""]
<<: *fluxer-app-healthcheck