Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): read the built-in mode from the host beside a pinned flag

Yudhi Armyndharisaf6f9fa1f269
diff --git a/dashboard/src/services/api.ts b/dashboard/src/services/api.ts
index 7d0df8765..d05f4c8fb 100644
--- a/dashboard/src/services/api.ts
+++ b/dashboard/src/services/api.ts
@@ -622,10 +622,11 @@ export interface UpdateCheck {
export interface InfraStatus {
// `builtIn` = OpenWA's own bundled container is actually running and backing this service (live),
- // not just the saved intent; falls back to the saved flag (the environment's first) when Docker is
- // unavailable. (#488) `running` = a bundled container OpenWA created is up, whether or not the service uses it,
- // beside this instance's own built-in flag (the environment's first, else the saved one); false for one Docker
- // Compose created and with that flag not true, absent while Docker is unreachable, and from an older gateway.
+ // not just the saved intent; falls back to the saved flag (the environment's first) beside a host that reaches the
+ // bundled container when Docker is unavailable. (#488) `running` = a bundled container OpenWA created is up,
+ // whether or not the service uses it, beside this instance's own built-in flag (the environment's first, else the
+ // saved one); false for one Docker Compose created and with that flag not true, absent while Docker is unreachable,
+ // and from an older gateway.
database: { connected: boolean; type: string; host: string; builtIn: boolean; running?: boolean };
redis: { enabled: boolean; connected: boolean; host: string; port: number; builtIn: boolean; running?: boolean };
queue: {
diff --git a/docs/05-database-design.md b/docs/05-database-design.md
index 0b1587905..3733133cd 100644
--- a/docs/05-database-design.md
+++ b/docs/05-database-design.md
@@ -89,7 +89,7 @@ others.
#### Built-in PostgreSQL Orchestration
-When using PostgreSQL Built-in mode (`DATABASE_TYPE=postgres` with `POSTGRES_BUILTIN=true`), `main.ts` starts the managed `postgres` container (creating it if it does not exist) **before** `NestFactory.create`. It has to run that early: the `data` connection is opened while Nest instantiates providers, before any `onModuleInit` hook, so a stopped `openwa-postgres` would otherwise fail every boot. The step is bounded at 15 seconds and never fails boot on its own. `DockerService.onModuleInit()` later runs the same orchestration for `redis` / `minio` when their own `REDIS_BUILTIN` / `MINIO_BUILTIN` flags are set. A flag set for a datastore the configuration does not use starts nothing: `postgres` needs `DATABASE_TYPE=postgres`, `minio` needs `STORAGE_TYPE=s3`, and `redis` needs one of `REDIS_ENABLED`, `QUEUE_ENABLED` or `CACHE_ENABLED`. Neither does one whose host the environment or the project `.env` pins to anything but the bundled container (`DATABASE_HOST`, `REDIS_HOST`, `S3_ENDPOINT`), the early PostgreSQL start included: the app dials the pinned host.
+When using PostgreSQL Built-in mode (`DATABASE_TYPE=postgres` with `POSTGRES_BUILTIN=true`), `main.ts` starts the managed `postgres` container (creating it if it does not exist) **before** `NestFactory.create`. It has to run that early: the `data` connection is opened while Nest instantiates providers, before any `onModuleInit` hook, so a stopped `openwa-postgres` would otherwise fail every boot. The step is bounded at 15 seconds and never fails boot on its own. `DockerService.onModuleInit()` later runs the same orchestration for `redis` / `minio` when their own `REDIS_BUILTIN` / `MINIO_BUILTIN` flags are set. A flag set for a datastore the configuration does not use starts nothing: `postgres` needs `DATABASE_TYPE=postgres`, `minio` needs `STORAGE_TYPE=s3`, and `redis` needs one of `REDIS_ENABLED`, `QUEUE_ENABLED` or `CACHE_ENABLED`. Neither does one whose host the environment or the project `.env` pins to anything but the bundled container (`DATABASE_HOST`, `REDIS_HOST`, `S3_ENDPOINT`), the early PostgreSQL start included: the app dials the pinned host. A flag decides only that start: whether the app uses the container follows its host. A `*_BUILTIN=true` the environment pins starts the bundled container at every boot whatever host the saved file gives the app, so after the dashboard switched that datastore to an external server the container runs unused until the variable is unset.
The container can still be coming up when the `data` connection first dials it, so that connection is configured with `retryAttempts: 10` and `retryDelay: 3000` (`src/app.module.ts`), giving the database roughly 30 seconds to become reachable.
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index 28caf5763..f49134059 100644
--- a/docs/06-api-specification.md
+++ b/docs/06-api-specification.md
@@ -5541,9 +5541,9 @@ Aggregate infrastructure status (database, Redis, queue, storage, engine).
The `queue.webhooks` counters are live BullMQ job counts (`pending` = waiting + active + delayed; plus `completed`/`failed`), degrading to zeros when the queue is disabled or Redis is unreachable. `redis.connected` is a live probe. `database.host` is the configured `DATABASE_HOST` (default `localhost`) and is reported on SQLite too, so read `type`, not `host`, to tell the database apart.
-`builtIn` (on `database`/`redis`/`storage`) reports whether OpenWA's own bundled container is actually running _and_ backing this service, detected live from the labelled container (on a daemon another OpenWA instance shares, its container counts too while this instance's host is spelled like the bundled one, see [13 - Horizontal Scaling](./13-horizontal-scaling.md)); when Docker is unreachable it falls back to the saved `*_BUILTIN` flag (one pinned by the environment first, else the one in `data/.env.generated`), and a host name alone counts for nothing there, since an external server may be called `postgres`. `running` (on the same three) reports whether a bundled container OpenWA itself created is up, whether or not the service uses it: one Docker Compose created (it carries Compose's project label) reads `false`, so does one beside a configuration whose `POSTGRES_BUILTIN`, `REDIS_BUILTIN` or `MINIO_BUILTIN` (the environment's first, else the saved one) is not `true` (the container names are fixed per Docker daemon, so another OpenWA instance on the same daemon sees the same containers), and the key is absent while Docker is unreachable, since nothing can be seen or stopped then. In S3 mode `storage` additionally carries `bucket` and `endpoint` (each when one is configured; a bundled MinIO that is stopped still reports `http://minio:9000` here, with `builtIn` false) and `s3Available` (re-probed, throttled, while false; once true it stays true until a restart, so a later outage does not clear it); in local mode neither key is present. `engine.webVersion`/`engine.webVersionSource` (`pinned` / `auto` / `native`) appear only on `whatsapp-web.js`; `webVersion` is `null` until the auto-resolve first succeeds. `engine.browserArgs` is the effective launch argument list: `PUPPETEER_ARGS` (or the four defaults shown) with `--lang=en-US` appended unless a `--lang` flag is already present.
+`builtIn` (on `database`/`redis`/`storage`) reports whether OpenWA's own bundled container is actually running _and_ backing this service, detected live from the labelled container (on a daemon another OpenWA instance shares, its container counts too while this instance's host is spelled like the bundled one, see [13 - Horizontal Scaling](./13-horizontal-scaling.md)); when Docker is unreachable it falls back to the saved `*_BUILTIN` flag (one pinned by the environment first, else the one in `data/.env.generated`) beside a host that reaches the bundled container: a host name alone counts for nothing there, since an external server may be called `postgres`, and neither does the flag beside another host, which the process dials. `running` (on the same three) reports whether a bundled container OpenWA itself created is up, whether or not the service uses it: one Docker Compose created (it carries Compose's project label) reads `false`, so does one beside a configuration whose `POSTGRES_BUILTIN`, `REDIS_BUILTIN` or `MINIO_BUILTIN` (the environment's first, else the saved one) is not `true` (the container names are fixed per Docker daemon, so another OpenWA instance on the same daemon sees the same containers), and the key is absent while Docker is unreachable, since nothing can be seen or stopped then. In S3 mode `storage` additionally carries `bucket` and `endpoint` (each when one is configured; a bundled MinIO that is stopped still reports `http://minio:9000` here, with `builtIn` false) and `s3Available` (re-probed, throttled, while false; once true it stays true until a restart, so a later outage does not clear it); in local mode neither key is present. `engine.webVersion`/`engine.webVersionSource` (`pinned` / `auto` / `native`) appear only on `whatsapp-web.js`; `webVersion` is `null` until the auto-resolve first succeeds. `engine.browserArgs` is the effective launch argument list: `PUPPETEER_ARGS` (or the four defaults shown) with `--lang=en-US` appended unless a `--lang` flag is already present.
-`envPinned` lists which of the keys `PUT /api/infra/config` writes the host environment or the project `.env` sets, by name only: `DATABASE_TYPE`, `POSTGRES_BUILTIN`, `DATABASE_HOST`, `DATABASE_PORT`, `DATABASE_USERNAME`, `DATABASE_PASSWORD`, `DATABASE_NAME`, `DATABASE_POOL_SIZE`, `POSTGRES_SCHEMA`, `DATABASE_SSL`, `DATABASE_SSL_REJECT_UNAUTHORIZED`, `REDIS_ENABLED`, `REDIS_BUILTIN`, `REDIS_HOST`, `REDIS_PORT`, `REDIS_PASSWORD`, `QUEUE_ENABLED`, `STORAGE_TYPE`, `MINIO_BUILTIN`, `S3_ENDPOINT`, `S3_BUCKET`, `S3_REGION`, `S3_ACCESS_KEY_ID`, `S3_SECRET_ACCESS_KEY`, `STORAGE_LOCAL_PATH`, `ENGINE_TYPE`, `PUPPETEER_HEADLESS`, `SESSION_DATA_PATH` and `PUPPETEER_ARGS`. Those layers win over `data/.env.generated`, so a value saved for one of these keys through `PUT /api/infra/config` cannot take effect until that layer is changed. A pinned host also decides whether the bundled container backs its service, whatever `builtIn` is saved.
+`envPinned` lists which of the keys `PUT /api/infra/config` writes the host environment or the project `.env` sets, by name only: `DATABASE_TYPE`, `POSTGRES_BUILTIN`, `DATABASE_HOST`, `DATABASE_PORT`, `DATABASE_USERNAME`, `DATABASE_PASSWORD`, `DATABASE_NAME`, `DATABASE_POOL_SIZE`, `POSTGRES_SCHEMA`, `DATABASE_SSL`, `DATABASE_SSL_REJECT_UNAUTHORIZED`, `REDIS_ENABLED`, `REDIS_BUILTIN`, `REDIS_HOST`, `REDIS_PORT`, `REDIS_PASSWORD`, `QUEUE_ENABLED`, `STORAGE_TYPE`, `MINIO_BUILTIN`, `S3_ENDPOINT`, `S3_BUCKET`, `S3_REGION`, `S3_ACCESS_KEY_ID`, `S3_SECRET_ACCESS_KEY`, `STORAGE_LOCAL_PATH`, `ENGINE_TYPE`, `PUPPETEER_HEADLESS`, `SESSION_DATA_PATH` and `PUPPETEER_ARGS`. Those layers win over `data/.env.generated`, so a value saved for one of these keys through `PUT /api/infra/config` cannot take effect until that layer is changed. A pinned host also decides whether the bundled container backs its service, whatever `builtIn` is saved. A pinned `POSTGRES_BUILTIN`, `REDIS_BUILTIN` or `MINIO_BUILTIN` only decides whether the bundled container starts at boot: `true` starts it at every boot whatever host the saved file gives the app, while the service uses the container only when its host reaches it.
**Errors:** `401` missing/invalid key · `403` key role < ADMIN, or key is session-scoped
@@ -5671,7 +5671,7 @@ Read the effective infrastructure config used to hydrate the dashboard form. Eac
}
```
-`builtInSet` says whether the saved configuration states the built-in mode of the database, Redis or storage. It is `false` when a host pinned by the environment (`DATABASE_HOST`, `REDIS_HOST`, `S3_ENDPOINT`) decides the mode, in which case `builtIn` reports the pin (`true` for the bundled host `postgres`, `redis` or `http://minio:9000`, or another spelling that reaches the same container: its container name such as `openwa-postgres`, another letter case, a trailing dot), when the saved file does not select the service (the first boot writes every flag as `false` next to a service that is not in use), when the file holds the bundled host in any of those spellings with the flag not `true` (the next boot dials the container, so `builtIn` is `true`), and when nothing is saved. Read what runs from `GET /api/infra/status` then. Where no host is pinned, a flag pinned by the environment itself counts as stated.
+`builtInSet` says whether the saved configuration states the built-in mode of the database, Redis or storage. It is `false` when a host pinned by the environment (`DATABASE_HOST`, `REDIS_HOST`, `S3_ENDPOINT`) decides the mode, in which case `builtIn` reports the pin (`true` for the bundled host `postgres`, `redis` or `http://minio:9000`, or another spelling that reaches the same container: its container name such as `openwa-postgres`, another letter case, a trailing dot), when the saved file does not select the service (the first boot writes every flag as `false` next to a service that is not in use), when the file holds the bundled host in any of those spellings with the flag not `true` (the next boot dials the container, so `builtIn` is `true`), and when nothing is saved. Read what runs from `GET /api/infra/status` then. Where no host is pinned, the host in the saved file decides `builtIn` (with none the next boot dials the default host, not the bundled container), and a flag pinned by the environment counts as stated: a `*_BUILTIN=true` pinned beside another host reads `builtIn` `false`, since that flag only starts the bundled container at boot.
When nothing supplies a key — no pinned environment value and the file absent or silent about it — empty-string/default values are returned (`schema='public'`, `poolSize=10`, `sslRejectUnauthorized=true`, `engine.type='whatsapp-web.js'`, `headless=true`).
diff --git a/docs/14-migration-guide.md b/docs/14-migration-guide.md
index b5231af2b..8c5979218 100644
--- a/docs/14-migration-guide.md
+++ b/docs/14-migration-guide.md
@@ -330,8 +330,10 @@ REDIS_PASSWORD=optional
REDIS_TLS=false # true for a managed Redis that requires TLS
```
-> Setting `REDIS_BUILTIN` in `.env` **pins** it: the env value wins, so the dashboard's built-in
-> Redis toggle can no longer switch the container back on. That is the right trade for a deployment
+> Setting `REDIS_BUILTIN` in `.env` **pins** it: the env value wins, and it decides only whether
+> OpenWA starts the bundled container at boot. `true` starts it at every boot, even after the
+> dashboard switched Redis to an external host, and `false` never does; which Redis the app uses
+> follows `REDIS_HOST`, as the dashboard writes it. That is the right trade for a deployment
> whose configuration lives in `.env` — but if you manage datastores from the dashboard, leave the
> key unset (as the shipped templates do) and set only the connection details above. The same holds
> for `POSTGRES_BUILTIN` and `MINIO_BUILTIN`.
diff --git a/openapi.json b/openapi.json
index 6657470c8..904ef08de 100644
--- a/openapi.json
+++ b/openapi.json
@@ -21907,7 +21907,7 @@
},
"builtIn": {
"type": "boolean",
- "description": "Whether OpenWA's own bundled container is actually running and backing this service, detected live from the labelled container rather than read from the saved intent. When Docker is unavailable it falls back to the saved flag (the environment's first), and a host name alone proves nothing.",
+ "description": "Whether OpenWA's own bundled container is actually running and backing this service, detected live from the labelled container rather than read from the saved intent. When Docker is unavailable it falls back to the saved flag (the environment's first) beside a host that reaches the bundled container: neither a host name nor the flag alone proves it.",
"example": true
},
"running": {
diff --git a/src/modules/infra/bundled-host-decisions.spec.ts b/src/modules/infra/bundled-host-decisions.spec.ts
index 7fd134aa7..11c80e0c6 100644
--- a/src/modules/infra/bundled-host-decisions.spec.ts
+++ b/src/modules/infra/bundled-host-decisions.spec.ts
@@ -167,12 +167,13 @@ describe.each(cases.map(c => [label(c), c] as const))('%s', (_label, c) => {
it('reads the built-in mode from the host', () => {
arrange(c);
const mode = controller().getConfig()[c.service];
+ // A flag of true beside a file host elsewhere is a saved choice of that host: the flag only starts the container.
const expected =
c.position === 'pinned'
? { builtIn: c.reach, builtInSet: false }
: c.flag !== 'true' && c.reach
? { builtIn: true, builtInSet: false }
- : { builtIn: c.flag === 'true', builtInSet: c.flag !== 'absent' };
+ : { builtIn: c.reach, builtInSet: c.flag !== 'absent' };
expect({ builtIn: mode.builtIn, builtInSet: mode.builtInSet }).toEqual(expected);
});
diff --git a/src/modules/infra/dto/infra-response.dto.ts b/src/modules/infra/dto/infra-response.dto.ts
index f7f057b2a..a43b929ab 100644
--- a/src/modules/infra/dto/infra-response.dto.ts
+++ b/src/modules/infra/dto/infra-response.dto.ts
@@ -25,7 +25,8 @@ export class InfraDatabaseStatusDto {
description:
"Whether OpenWA's own bundled container is actually running and backing this service, " +
'detected live from the labelled container rather than read from the saved intent. When Docker is ' +
- "unavailable it falls back to the saved flag (the environment's first), and a host name alone proves nothing.",
+ "unavailable it falls back to the saved flag (the environment's first) beside a host that reaches the " +
+ 'bundled container: neither a host name nor the flag alone proves it.',
example: true,
})
builtIn!: boolean;
diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.ts
index cd41b266d..1548f16c0 100644
--- a/src/modules/infra/infra-config.controller.spec.ts
+++ b/src/modules/infra/infra-config.controller.spec.ts
@@ -44,6 +44,7 @@ import { resolveStorageRoot } from '../../config/storage-root';
import { validateEnv } from '../../config/env.validation';
import {
EXPECTED_MODE,
+ EXPECTED_MODE_FLAG_PINNED,
FILE_STATES,
HOST_SPELLINGS,
PIN_STATES,
@@ -2025,9 +2026,9 @@ describe('InfraConfigController.getConfig answers the saved built-in mode for ev
const otherPin = (pin: PinState): PinState => (pin === 'external' ? 'bundled' : 'external');
let pinnedKeys: string[] = [];
- const arrange = (service: ServiceName, state: FileState, pin: PinState) => {
+ const arrange = (service: ServiceName, state: FileState, pin: PinState, extraPins: Record<string, string> = {}) => {
const lines: string[] = [];
- const pins: Record<string, string> = {};
+ const pins: Record<string, string> = { ...extraPins };
for (const name of SERVICE_NAMES) {
const own = name === service;
lines.push(...fileLines(name, own ? state : otherState(state)));
@@ -2115,27 +2116,101 @@ describe('InfraConfigController.getConfig answers the saved built-in mode for ev
}
});
- // An operator who pins the flag itself has decided it, whatever the file or the hosts say, unless a host pin does.
- it('a flag pinned by the environment counts as stored, and a pinned host still outranks it', () => {
- recordPinnedEnvKeys({
- POSTGRES_BUILTIN: 'true',
- REDIS_BUILTIN: 'false',
- MINIO_BUILTIN: 'true',
- S3_ENDPOINT: 'https://s3.external.example',
- });
- process.env.POSTGRES_BUILTIN = 'true';
- process.env.REDIS_BUILTIN = 'false';
- process.env.MINIO_BUILTIN = 'true';
- process.env.S3_ENDPOINT = 'https://s3.external.example';
- pinnedKeys = ['S3_ENDPOINT'];
+ // A flag the environment pins only decides whether the bundled container starts at boot. Whether the service is
+ // built-in is the host the next boot dials: the pinned one, else the file's beside the pinned flag, which then
+ // counts as a saved choice.
+ describe.each(SERVICE_NAMES)('%s beside a flag the environment pins', service => {
+ for (const state of FILE_STATES) {
+ for (const flag of ['true', 'false'] as const) {
+ for (const pin of PIN_STATES) {
+ const host = pin === 'none' ? 'no host pin' : `the host pinned ${pin}`;
+ it(`a ${state} file with the flag pinned ${flag} and ${host}`, () => {
+ arrange(service, state, pin, { [SERVICES[service].flag]: flag });
+
+ expect(modeOf(newController().getConfig(), service)).toEqual(
+ pin === 'none' ? EXPECTED_MODE_FLAG_PINNED[state][flag] : EXPECTED_MODE[state][pin],
+ );
+ });
+ }
+ }
+ }
+ });
+
+ // The switch the page writes beside flags the environment pins true (the documented manual flags): the flags false
+ // and external hosts in the file. The restart applies it, so it reads as the saved external choice it is.
+ it('a switch to external servers saved beside flags pinned true reads as external', () => {
+ const pins = { POSTGRES_BUILTIN: 'true', REDIS_BUILTIN: 'true', MINIO_BUILTIN: 'true' };
+ recordPinnedEnvKeys(pins);
+ Object.assign(process.env, pins);
+ pinnedKeys = Object.keys(pins);
(fs.existsSync as jest.Mock).mockReturnValue(true);
- (fs.readFileSync as jest.Mock).mockReturnValue('POSTGRES_BUILTIN=false\nREDIS_BUILTIN=true\nMINIO_BUILTIN=false\n');
+ (fs.readFileSync as jest.Mock).mockReturnValue(
+ 'DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=false\nDATABASE_HOST=db.ext.example\nREDIS_ENABLED=true\n' +
+ 'REDIS_BUILTIN=false\nREDIS_HOST=cache.ext.example\nSTORAGE_TYPE=s3\nMINIO_BUILTIN=false\n' +
+ 'S3_ENDPOINT=https://s3.ext.example\n',
+ );
const cfg = newController().getConfig();
- expect(modeOf(cfg, 'database')).toEqual({ builtIn: true, builtInSet: true });
- expect(modeOf(cfg, 'redis')).toEqual({ builtIn: false, builtInSet: true });
- expect(modeOf(cfg, 'storage')).toEqual({ builtIn: false, builtInSet: false });
+ for (const service of SERVICE_NAMES) expect(modeOf(cfg, service)).toEqual({ builtIn: false, builtInSet: true });
+ });
+});
+
+// The save of what GET /config reports, with an unrelated setting changed, as the Infrastructure page sends it after
+// a switch to external servers beside flags the environment pins true: the next boot keeps dialling those servers
+// with the values saved for them. The pinned flags only start the bundled containers at boot.
+describe('InfraConfigController.saveConfig beside flags the environment pins true', () => {
+ const STORED =
+ 'DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=false\nDATABASE_HOST=db.ext.example\nDATABASE_PORT=5432\n' +
+ 'DATABASE_USERNAME=app\nDATABASE_PASSWORD=ExtPw12345!\nDATABASE_NAME=appdb\nPOSTGRES_SCHEMA=tenant\n' +
+ 'REDIS_ENABLED=true\nREDIS_BUILTIN=false\nREDIS_HOST=cache.ext.example\nREDIS_PORT=6380\n' +
+ 'REDIS_PASSWORD=RedisPw123456\nQUEUE_ENABLED=false\nSTORAGE_TYPE=s3\nMINIO_BUILTIN=false\n' +
+ 'S3_ENDPOINT=https://s3.ext.example\nS3_BUCKET=media\nS3_REGION=eu-west-1\nS3_ACCESS_KEY_ID=AKIAEXTERNAL12345\n' +
+ 'S3_SECRET_ACCESS_KEY=ExternalSecret1234567890\nPUPPETEER_HEADLESS=true\n';
+ const pins = { POSTGRES_BUILTIN: 'true', REDIS_BUILTIN: 'true', MINIO_BUILTIN: 'true' };
+ const dotenv = jest.requireActual<typeof import('dotenv')>('dotenv');
+
+ beforeEach(() => {
+ recordPinnedEnvKeys(pins);
+ Object.assign(process.env, pins);
+ (fs.existsSync as jest.Mock).mockReturnValue(true);
+ (fs.readFileSync as jest.Mock).mockReturnValue(STORED);
+ (fs.writeFileSync as jest.Mock).mockClear();
+ });
+ afterEach(() => {
+ for (const key of Object.keys(pins)) delete process.env[key];
+ recordPinnedEnvKeys({});
+ (fs.existsSync as jest.Mock).mockReturnValue(false);
+ (fs.readFileSync as jest.Mock).mockReturnValue('');
+ });
+
+ it('keeps every external value when only Headless Mode changes', () => {
+ const controller = new InfraConfigController({ getAvailableEngines: () => [] } as never, {} as never, {} as never);
+ const cfg = controller.getConfig();
+ // What the form seeds from GET /config and sends: no host is pinned, so every flag is sent; secrets are blank.
+ controller.saveConfig({
+ database: { ...cfg.database, password: '' },
+ redis: { enabled: cfg.redis.enabled, builtIn: cfg.redis.builtIn, host: cfg.redis.host, port: cfg.redis.port },
+ queue: cfg.queue,
+ storage: {
+ type: cfg.storage.type,
+ builtIn: cfg.storage.builtIn,
+ s3Bucket: cfg.storage.s3Bucket,
+ s3Region: cfg.storage.s3Region,
+ s3Endpoint: cfg.storage.s3Endpoint,
+ s3AccessKey: '',
+ s3SecretKey: '',
+ },
+ engine: { headless: !cfg.engine.headless },
+ });
+
+ const written = dotenv.parse(((fs.writeFileSync as jest.Mock).mock.calls as Array<[string, string]>)[0][1]);
+ expect(written).toEqual({
+ ...dotenv.parse(STORED),
+ DATABASE_POOL_SIZE: '10',
+ DATABASE_SSL: 'false',
+ PUPPETEER_HEADLESS: 'false',
+ });
});
});
diff --git a/src/modules/infra/infra-status.controller.spec.ts b/src/modules/infra/infra-status.controller.spec.ts
index 237fb26f6..474c0d053 100644
--- a/src/modules/infra/infra-status.controller.spec.ts
+++ b/src/modules/infra/infra-status.controller.spec.ts
@@ -29,10 +29,12 @@ import {
PIN_STATES,
SERVICES,
SERVICE_NAMES,
+ fileHost,
fileLines,
pinnedHost,
type FileState,
type PinState,
+ type ServiceName,
} from '../../../test/fixtures/builtin-mode-cases';
describe('InfraStatusController.getStatus DB health (active SELECT 1 probe, not just isInitialized)', () => {
@@ -382,10 +384,17 @@ describe('InfraStatusController.getStatus envPinned', () => {
// With Docker unreachable the status falls back to the saved flag, the environment's first. A host name proves
// nothing without a container to see (an external server may be called postgres), so a pinned bundled host must not
-// read as built-in there: it would replace the stored external credentials with the bundled defaults on a save.
+// read as built-in there: it would replace the stored external credentials with the bundled defaults on a save. Nor
+// does a flag beside a host that does not reach the bundled container: the process dials that host.
describe('InfraStatusController.getStatus built-in fallback reads the flag only when Docker is unreachable', () => {
- const build = () => {
- const config = { get: (k: string, def?: unknown) => (k === 'engine.type' ? 'baileys' : def) };
+ // The hosts the process runs with, as configuration.ts reads them at boot.
+ const build = (hosts: Partial<Record<ServiceName, string>> = {}) => {
+ const key: Record<string, string | undefined> = {
+ 'dataDatabase.host': hosts.database,
+ 'redis.host': hosts.redis,
+ 'storage.s3.endpoint': hosts.storage,
+ };
+ const config = { get: (k: string, def?: unknown) => (k === 'engine.type' ? 'baileys' : (key[k] ?? def)) };
const ds = { isInitialized: true, query: jest.fn().mockResolvedValue([{ '1': 1 }]) };
return new InfraStatusController(
config as never,
@@ -416,11 +425,13 @@ describe('InfraStatusController.getStatus built-in fallback reads the flag only
it(`${service}: a ${state} file with ${pin === 'none' ? 'no host pin' : `the host pinned ${pin}`}`, async () => {
const lines: string[] = [];
const pins: Record<string, string> = {};
+ const hosts: Partial<Record<ServiceName, string>> = {};
for (const name of SERVICE_NAMES) {
const own = name === service;
lines.push(...fileLines(name, own ? state : otherState(state)));
const host = pinnedHost(name, own ? pin : otherPin(pin));
if (host !== undefined) pins[SERVICES[name].host] = host;
+ hosts[name] = host ?? fileHost(name, own ? state : otherState(state));
}
recordPinnedEnvKeys(pins);
for (const [key, value] of Object.entries(pins)) process.env[key] = value;
@@ -428,9 +439,9 @@ describe('InfraStatusController.getStatus built-in fallback reads the flag only
(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.readFileSync as jest.Mock).mockReturnValue(`${lines.join('\n')}\n`);
- const status = await build().getStatus();
+ const status = await build(hosts).getStatus();
- expect(status[service].builtIn).toBe(FALLBACK_BUILT_IN[state]);
+ expect(status[service].builtIn).toBe(FALLBACK_BUILT_IN[state][pin]);
});
}
}
@@ -450,10 +461,33 @@ describe('InfraStatusController.getStatus built-in fallback reads the flag only
(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.readFileSync as jest.Mock).mockReturnValue('POSTGRES_BUILTIN=false\nREDIS_BUILTIN=true\nMINIO_BUILTIN=false\n');
- const status = await build().getStatus();
+ const status = await build({ database: 'postgres', redis: 'redis', storage: 'http://minio:9000' }).getStatus();
expect([status.database.builtIn, status.redis.builtIn, status.storage.builtIn]).toEqual([true, false, false]);
});
+
+ // The page switched the three services to external servers beside flags the environment pins true, and the restart
+ // applied it: the flags still start the containers at boot, but the process dials the external hosts.
+ it('a flag pinned true beside a host that does not reach the bundled container reads as no built-in', async () => {
+ const pins = { POSTGRES_BUILTIN: 'true', REDIS_BUILTIN: 'true', MINIO_BUILTIN: 'true' };
+ recordPinnedEnvKeys(pins);
+ Object.assign(process.env, pins);
+ pinnedKeys = Object.keys(pins);
+ (fs.existsSync as jest.Mock).mockReturnValue(true);
+ (fs.readFileSync as jest.Mock).mockReturnValue(
+ 'DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=false\nDATABASE_HOST=db.ext.example\nREDIS_ENABLED=true\n' +
+ 'REDIS_BUILTIN=false\nREDIS_HOST=cache.ext.example\nSTORAGE_TYPE=s3\nMINIO_BUILTIN=false\n' +
+ 'S3_ENDPOINT=https://s3.ext.example\n',
+ );
+
+ const status = await build({
+ database: 'db.ext.example',
+ redis: 'cache.ext.example',
+ storage: 'https://s3.ext.example',
+ }).getStatus();
+
+ expect([status.database.builtIn, status.redis.builtIn, status.storage.builtIn]).toEqual([false, false, false]);
+ });
});
// `running` reports a bundled container OpenWA created that is up, apart from whether the service uses it: the page
diff --git a/src/modules/infra/infra-status.controller.ts b/src/modules/infra/infra-status.controller.ts
index a11f7ab62..33c88b299 100644
--- a/src/modules/infra/infra-status.controller.ts
+++ b/src/modules/infra/infra-status.controller.ts
@@ -214,8 +214,9 @@ export class InfraStatusController {
// Built-in detection: prefer the actually-running bundled container as truth (so a stopped/missing
// container, or a host-pinned external host, reads as NOT built-in), and require the app to be
- // pointed at the bundled service. Fall back to the saved *_BUILTIN flag (the environment's first) when
- // Docker is unreachable (bare-npm / socket-less) so the toggles don't spuriously flip off. (#488) The labelled
+ // pointed at the bundled service either way. Fall back to the saved *_BUILTIN flag (the environment's first)
+ // when Docker is unreachable (bare-npm / socket-less) so the toggles don't spuriously flip off; a flag only
+ // decides whether the container starts at boot, so it counts beside the bundled host alone. (#488) The labelled
// container may be another instance's on the same daemon: it counts whenever this one's host is spelled like the
// bundled one (docs/13, Scaling Guidelines).
// `running` names a container OpenWA created beside this instance's own built-in flag only: the container names
@@ -225,11 +226,11 @@ export class InfraStatusController {
? await this.dockerService.getRunningBuiltinServices()
: null;
const savedBuiltin = this.readSavedBuiltinFlags();
- const dbBuiltIn = running ? !!running.database && namesBundledContainer('postgres', dbHost) : savedBuiltin.database;
- const redisBuiltIn = running ? !!running.cache && namesBundledContainer('redis', redisHost) : savedBuiltin.cache;
- const storageBuiltIn = running
- ? !!running.storage && namesBundledContainer('minio', s3Endpoint)
- : savedBuiltin.storage;
+ const dbBuiltIn =
+ (running ? !!running.database : savedBuiltin.database) && namesBundledContainer('postgres', dbHost);
+ const redisBuiltIn = (running ? !!running.cache : savedBuiltin.cache) && namesBundledContainer('redis', redisHost);
+ const storageBuiltIn =
+ (running ? !!running.storage : savedBuiltin.storage) && namesBundledContainer('minio', s3Endpoint);
// Re-probe (throttled) so a MinIO/S3 that came up after boot is reflected, not latched unreachable.
const s3Available = storageType === 's3' ? await this.storageService.refreshS3Availability() : undefined;
diff --git a/src/modules/infra/saved-builtin-mode.ts b/src/modules/infra/saved-builtin-mode.ts
index 785d34224..fddc7feb6 100644
--- a/src/modules/infra/saved-builtin-mode.ts
+++ b/src/modules/infra/saved-builtin-mode.ts
@@ -52,22 +52,25 @@ function flagValue(service: BuiltInService, saved: Record<string, string>): stri
}
/**
- * The built-in flag as GET /infra/status falls back to it with Docker unreachable. No host rule here: with no
- * container to see, a host called postgres is as likely an external server (a Kubernetes Service, say) and must
- * not read as the bundled container.
+ * The built-in flag the environment pins, else the saved one: this instance's own record that it chose the bundled
+ * container. GET /infra/status falls back to it with Docker unreachable, where a host called postgres alone is as
+ * likely an external server (a Kubernetes Service, say) and must not read as the bundled container; the flag alone
+ * does not either, since the process dials its host.
*/
export function effectiveBuiltInFlag(service: BuiltInService, saved: Record<string, string>): boolean {
return flagValue(service, saved) === 'true';
}
/**
+ * The host the next boot dials decides whether the service is built-in; the flag only whether the bundled container
+ * starts at boot (a flag the environment pins true starts it at every boot, whatever host the file gives the app).
* Rules, in order:
* 1. A host pinned by the environment (or the project .env) decides, as it does at boot and in
* requestRestart: the bundled host in any spelling that reaches the container is built-in, any other is
* not, and no saved flag can be a pending switch because the pin outranks it.
- * 2. Otherwise the flag counts as saved when the environment pins it, or when the file stores it for a
- * service the file also selects. A flag that is not true beside the bundled host name in the file is no
- * choice of an external service, though: the next boot dials the container, so it reads as built-in and
+ * 2. Otherwise the file's host decides, and the flag counts as saved when the environment pins it, or when the file
+ * stores it for a service the file also selects. A flag that is not true beside the bundled host name in the file
+ * is no choice of an external service, though: the next boot dials the container, so it reads as built-in and
* nothing is saved.
* 3. Otherwise there is nothing saved.
*/
@@ -81,5 +84,5 @@ export function savedBuiltInMode(service: BuiltInService, saved: Record<string,
const selectedInFile = saved[spec.selection] === spec.selected;
const dialsBundled = namesBundledContainer(spec.profile, saved[spec.host]);
if (flag !== 'true' && dialsBundled) return { builtIn: true, builtInSet: false };
- return { builtIn: flag === 'true', builtInSet: flag !== undefined && (flagPinned || selectedInFile) };
+ return { builtIn: dialsBundled, builtInSet: flag !== undefined && (flagPinned || selectedInFile) };
}
diff --git a/test/fixtures/builtin-mode-cases.ts b/test/fixtures/builtin-mode-cases.ts
index 94c05f479..b437c50d6 100644
--- a/test/fixtures/builtin-mode-cases.ts
+++ b/test/fixtures/builtin-mode-cases.ts
@@ -2,13 +2,15 @@
// environment pins for the service's host key. One table for both halves of the Infrastructure page: the
// controller specs assert the answer, the dashboard page tests render the form from it.
//
-// The rule, per service (database, redis, storage):
+// The rule, per service (database, redis, storage): the host the next boot dials decides whether the service is
+// built-in, and a *_BUILTIN flag only whether the bundled container starts at boot.
// 1. A host pinned by the environment decides: pinned to the bundled host is built-in, anything else is not,
// and the saved flag says nothing (builtInSet false).
-// 2. Otherwise the saved flag counts (builtInSet true) only when the file also selects the service. The first
-// boot writes every flag as false next to a service that is not selected, which is a default, not a choice.
-// A flag that is not true beside the bundled host name in the file is no choice of an external service either:
-// the next boot dials the container (builtIn true, builtInSet false).
+// 2. Otherwise the file's host decides (none is the default host, which is not the bundled container). The flag
+// counts as a saved choice (builtInSet true) when the environment pins it or the file also selects the service.
+// The first boot writes every flag as false next to a service that is not selected, which is a default, not a
+// choice. A flag that is not true beside the bundled host name in the file is no choice of an external service
+// either: the next boot dials the container (builtIn true, builtInSet false).
// 3. Otherwise nothing is saved (builtInSet false) and the page follows what runs.
export type FileState =
@@ -96,14 +98,15 @@ export const EXPECTED_MODE: Record<FileState, Record<PinState, SavedMode>> = {
bundled: PINNED_BUNDLED,
external: PINNED_EXTERNAL,
},
+ // The flag true without the bundled host: the next boot dials the default host, so the choice is external.
'stored-true': {
- none: { builtIn: true, builtInSet: true },
+ none: { builtIn: false, builtInSet: true },
bundled: PINNED_BUNDLED,
external: PINNED_EXTERNAL,
},
// A flag left true beside a service the file does not select.
'unselected-true': {
- none: { builtIn: true, builtInSet: false },
+ none: { builtIn: false, builtInSet: false },
bundled: PINNED_BUNDLED,
external: PINNED_EXTERNAL,
},
@@ -122,20 +125,41 @@ export const EXPECTED_MODE: Record<FileState, Record<PinState, SavedMode>> = {
};
/**
- * What GET /infra/status answers with Docker unreachable, where there is no container to see. A host name proves
- * nothing there (an external server may be called postgres), so only the flag counts: the environment's, else the
- * file's, whatever the host pins say.
+ * The built-in mode GET /infra/config answers when the environment pins the service's flag and not its host: the
+ * file's host still decides (rule 2), and the pinned flag counts as a saved choice. A pinned host decides as in
+ * EXPECTED_MODE whatever the flag.
+ */
+export const EXPECTED_MODE_FLAG_PINNED: Record<FileState, Record<'true' | 'false', SavedMode>> = {
+ absent: { true: { builtIn: false, builtInSet: true }, false: { builtIn: false, builtInSet: true } },
+ template: { true: { builtIn: false, builtInSet: true }, false: { builtIn: false, builtInSet: true } },
+ 'stored-false': { true: { builtIn: false, builtInSet: true }, false: { builtIn: false, builtInSet: true } },
+ 'stored-true': { true: { builtIn: false, builtInSet: true }, false: { builtIn: false, builtInSet: true } },
+ 'unselected-true': { true: { builtIn: false, builtInSet: true }, false: { builtIn: false, builtInSet: true } },
+ 'bundled-host': { true: { builtIn: true, builtInSet: true }, false: { builtIn: true, builtInSet: false } },
+ 'saved-bundled': { true: { builtIn: true, builtInSet: true }, false: { builtIn: true, builtInSet: false } },
+};
+
+/**
+ * What GET /infra/status answers with Docker unreachable, where there is no container to see. A host name alone proves
+ * nothing there (an external server may be called postgres), so the flag must be true, the environment's, else the
+ * file's; and a flag beside a host that does not reach the bundled container proves nothing either, since the process
+ * dials that host. The process runs on the pinned host, else the file's.
*/
-export const FALLBACK_BUILT_IN: Record<FileState, boolean> = {
- absent: false,
- template: false,
- 'stored-false': false,
- 'stored-true': true,
- 'unselected-true': true,
- 'bundled-host': false,
- 'saved-bundled': true,
+export const FALLBACK_BUILT_IN: Record<FileState, Record<PinState, boolean>> = {
+ absent: { none: false, bundled: false, external: false },
+ template: { none: false, bundled: false, external: false },
+ 'stored-false': { none: false, bundled: false, external: false },
+ 'stored-true': { none: false, bundled: true, external: false },
+ 'unselected-true': { none: false, bundled: true, external: false },
+ 'bundled-host': { none: false, bundled: false, external: false },
+ 'saved-bundled': { none: true, bundled: true, external: false },
};
+/** The host the file names for a service in a given state, or undefined when it names none. */
+export function fileHost(service: ServiceName, state: FileState): string | undefined {
+ return state === 'bundled-host' || state === 'saved-bundled' ? SERVICES[service].bundledHost : undefined;
+}
+
/** The lines a saved file holds about one service in a given state. */
export function fileLines(service: ServiceName, state: FileState): string[] {
const spec = SERVICES[service];