Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): stop a bundled container left behind on a failed read

Yudhi Armyndharisd9d49aa8d311
diff --git a/dashboard/src/pages/Infrastructure.test.ts b/dashboard/src/pages/Infrastructure.test.ts
index 8913b858e..084599d46 100644
--- a/dashboard/src/pages/Infrastructure.test.ts
+++ b/dashboard/src/pages/Infrastructure.test.ts
@@ -2030,6 +2030,123 @@ test('bundled containers the pinned hosts reach are neither stopped nor named wh
assert.deepEqual(body, { profiles: [], profilesToRemove: [] });
});
+// The save writes the host a built-in service is switched to as sent (trimmed, or dropped with another type), so
+// without the read-back the page takes it from the payload: a host that reaches the bundled container keeps it, any
+// other stops it, and the dialog names it.
+const READBACK_SAVED: SavedConfig = {
+ ...SAVED_CONFIG,
+ database: { ...SAVED_CONFIG.database, builtIn: true, host: 'postgres' },
+ redis: { enabled: true, builtIn: true, builtInSet: true, host: 'redis', port: '6379', passwordSet: false },
+ storage: {
+ ...SAVED_CONFIG.storage,
+ type: 's3',
+ builtIn: true,
+ s3Bucket: 'openwa',
+ s3Endpoint: 'http://minio:9000',
+ },
+};
+const BUILT_IN_SWITCHES = [
+ ['database', 'postgres', 'Use Built-in PostgreSQL Container', 'Database Configuration', 'Host'],
+ ['redis', 'redis', 'Use Built-in Redis Container', 'Redis', 'Host'],
+ ['storage', 'minio', 'Use Built-in MinIO Container', 'Storage Configuration', 'Custom Endpoint (optional)'],
+] as const;
+const CONTAINER_NAMES: Record<string, string> = {
+ postgres: 'openwa-postgres',
+ redis: 'openwa-redis',
+ minio: 'openwa-minio',
+};
+
+// `kept` is what the save answers with: the profiles of the built-in services left on.
+async function switchWithoutReadBack(
+ kept: string[],
+ edit: (container: HTMLElement) => void,
+): Promise<{ names: string[]; body: unknown }> {
+ const { screen, waitFor, fireEvent, within } = rtl;
+ resetFetchCalls();
+ overrides = { status: RUNNING_BUNDLED, saved: READBACK_SAVED, savedAfterSaveFails: true, saveProfiles: kept };
+ const { container } = renderInfrastructure();
+ await screen.findByText('Database Configuration');
+ await awaitSeeded(container);
+ edit(container);
+ fireEvent.click(screen.getByRole('button', { name: 'Save Configuration' }));
+ const dialog = await screen.findByRole('dialog');
+ const names = stopNames(dialog);
+ fireEvent.click(within(dialog).getByRole('button', { name: 'Restart Now' }));
+ await waitFor(() => assert.ok(findFetchCall('POST', '/api/infra/restart'), 'expected the restart POST'));
+ return { names, body: findFetchCall('POST', '/api/infra/restart')!.body };
+}
+
+for (const [service, profile, toggle, heading, field] of BUILT_IN_SWITCHES) {
+ const hosts = [
+ ...HOST_SPELLINGS[service].reach.map(host => [host, true] as const),
+ ...HOST_SPELLINGS[service].elsewhere.map(host => [host, false] as const),
+ ];
+ for (const [host, reach] of hosts) {
+ test(`a built-in ${service} switched to ${JSON.stringify(host)} is ${reach ? 'kept' : 'stopped'} when the read-back fails`, async () => {
+ const { fireEvent } = rtl;
+ const kept = ['postgres', 'redis', 'minio'].filter(p => p !== profile);
+ const { names, body } = await switchWithoutReadBack(kept, container => {
+ fireEvent.click(toggleInput(container, toggle));
+ fireEvent.change(fieldInput(card(container, heading), field), { target: { value: host } });
+ });
+
+ assert.deepEqual(names, reach ? [] : [CONTAINER_NAMES[profile]]);
+ assert.deepEqual(body, { profiles: kept, profilesToRemove: reach ? [] : [profile] });
+ });
+ }
+}
+
+test('a built-in database and storage switched to SQLite and local are stopped when the read-back fails', async () => {
+ const { fireEvent } = rtl;
+ const { names, body } = await switchWithoutReadBack(['redis'], container => {
+ fireEvent.click(container.querySelectorAll<HTMLInputElement>('input[name="dbType"]')[0]);
+ fireEvent.click(container.querySelectorAll<HTMLInputElement>('input[name="storageType"]')[0]);
+ });
+
+ assert.deepEqual(names, ['openwa-postgres', 'openwa-minio']);
+ assert.deepEqual(body, { profiles: ['redis'], profilesToRemove: ['postgres', 'minio'] });
+});
+
+// A built-in Redis nothing uses any more is saved without its profile, and the save drops its host.
+test('a built-in Redis switched off is stopped when the read-back fails', async () => {
+ const { fireEvent } = rtl;
+ const { names, body } = await switchWithoutReadBack(['postgres', 'minio'], container => {
+ fireEvent.click(toggleInput(container, 'Enable Redis'));
+ });
+
+ assert.deepEqual(names, ['openwa-redis']);
+ assert.deepEqual(body, { profiles: ['postgres', 'minio'], profilesToRemove: ['redis'] });
+});
+
+// The cache may still use Redis, and the page cannot see it: a host on the bundled name, switched off from a built-in
+// Redis together with Redis itself, is the server's to drop or keep, so its container is not asked to stop.
+test('a Redis switched off beside a host on the bundled name is not stopped when the read-back fails', async () => {
+ const { fireEvent } = rtl;
+ const { names, body } = await switchWithoutReadBack(['postgres', 'minio'], container => {
+ fireEvent.click(toggleInput(container, 'Use Built-in Redis Container'));
+ fireEvent.click(toggleInput(container, 'Enable Redis'));
+ });
+
+ assert.deepEqual(names, []);
+ assert.deepEqual(body, { profiles: ['postgres', 'minio'], profilesToRemove: [] });
+});
+
+// With REDIS_ENABLED pinned, an untouched switch is left out of the save, and the stored value the file keeps decides.
+test('a built-in Redis whose switch the environment pins is not stopped when the read-back fails', async () => {
+ resetFetchCalls();
+ overrides = {
+ status: { ...RUNNING_BUNDLED, envPinned: ['REDIS_ENABLED'] },
+ saved: { ...READBACK_SAVED, redis: { ...READBACK_SAVED.redis, enabled: false } },
+ savedAfterSaveFails: true,
+ saveProfiles: ['postgres', 'minio'],
+ };
+
+ const { names, body } = await saveAndRestart();
+
+ assert.deepEqual(names, []);
+ assert.deepEqual(body, { profiles: ['postgres', 'minio'], profilesToRemove: [] });
+});
+
test('bundled containers the environment pins the hosts to are neither stopped nor named', async () => {
const { screen, waitFor, fireEvent, within } = rtl;
resetFetchCalls();
diff --git a/dashboard/src/pages/Infrastructure.tsx b/dashboard/src/pages/Infrastructure.tsx
index 45530eeb3..9d07ef53e 100644
--- a/dashboard/src/pages/Infrastructure.tsx
+++ b/dashboard/src/pages/Infrastructure.tsx
@@ -14,7 +14,7 @@ import {
Download,
Upload,
} from 'lucide-react';
-import { API_BASE_URL, infraApi, type SavedConfig } from '../services/api';
+import { API_BASE_URL, infraApi, type SavedConfig, type SaveConfigPayload } from '../services/api';
import { copyToClipboard } from '../utils/clipboard';
import { CONTAINER_OF, namesBundledContainer, sameHost } from '../utils/bundledHost';
import { useDocumentTitle } from '../hooks/useDocumentTitle';
@@ -46,6 +46,23 @@ const HOST_KEY: Record<string, string> = { postgres: 'DATABASE_HOST', redis: 'RE
const nextBootHost = (profile: string, config: SavedConfig): string =>
profile === 'postgres' ? config.database.host : profile === 'redis' ? config.redis.host : config.storage.s3Endpoint;
+/**
+ * The host a save wrote for a profile whose host the environment does not pin, taken from what it sent the way the
+ * section writers (src/modules/infra/config-sections.ts) write it, for when the config cannot be read back: the host
+ * sent, or '' where the save drops it. A built-in choice the new configuration uses is in the save's profiles, which
+ * the restart starts rather than stops, so the host sent beside it does not matter.
+ */
+function writtenHost(profile: string, sent: SaveConfigPayload): string | undefined {
+ if (profile === 'postgres') return sent.database?.type === 'sqlite' ? '' : sent.database?.host;
+ if (profile === 'minio') return sent.storage?.type === 'local' ? '' : sent.storage?.s3Endpoint;
+ // A built-in Redis switched off is dropped with its host unless the queue or the cache still uses it, which keeps its
+ // profile. The one case the page cannot see is CACHE_ENABLED true in the saved file beside an environment value that
+ // turns it off: the host stays. An external host is kept as sent; one on the bundled name is dropped only once
+ // nothing uses Redis, which the cache may, so the page does not ask to stop that container.
+ const redis = sent.redis;
+ return redis?.builtIn && redis.enabled === false ? '' : redis?.host;
+}
+
export function Infrastructure() {
const { t } = useTranslation();
useDocumentTitle(t('infrastructure.title'));
@@ -129,16 +146,17 @@ export function Infrastructure() {
.filter(([profile, up, host]) => up && host !== undefined && !namesBundledContainer(profile, host))
.map(([profile]): string => profile);
// The server never stops a container the next boot still reaches: the host the environment pins, else the one in
- // the file this save wrote. A pinned host is the one the page loaded; the saved one the page reads back. It asks
- // for (and names) only the containers neither reaches, and none whose host it could not read, so the dialog lists
- // what the server stops. The form is seeded once, so the refetch keeps any edit in progress.
+ // the file this save wrote. A pinned host is the one the page loaded; the saved one the page reads back, or takes
+ // from what the save sent when that read fails. It asks for (and names) only the containers neither reaches, and
+ // none whose host it cannot tell, so the dialog lists what the server stops. The form is seeded once, so the
+ // refetch keeps any edit in progress.
const saved = await queryClient
.fetchQuery({ queryKey: ['infra', 'config'], queryFn: infraApi.getConfig, staleTime: 0 })
.catch(() => undefined);
- const next = (profile: string) => pinned(HOST_KEY[profile]) ?? saved;
const toStop = [...running, ...pinnedAway].filter(profile => {
- const config = next(profile);
- return !!config && !namesBundledContainer(profile, nextBootHost(profile, config));
+ const config = pinned(HOST_KEY[profile]) ?? saved;
+ const host = config ? nextBootHost(profile, config) : writtenHost(profile, sent);
+ return host !== undefined && !namesBundledContainer(profile, host);
});
restartFlow.open({
profiles: profiles.filter(p => !pinnedAway.includes(p)),
diff --git a/src/modules/infra/infra-config.controller.ts b/src/modules/infra/infra-config.controller.ts
index 27c3f21e1..beca1b825 100644
--- a/src/modules/infra/infra-config.controller.ts
+++ b/src/modules/infra/infra-config.controller.ts
@@ -481,9 +481,9 @@ export class InfraConfigController {
// otherwise tear down the very backend the app is running on. The dashboard fills this list from the
// live /infra/status at save time: the builtIn services minus the new profiles, plus a running container
// OpenWA created whose host the environment pins elsewhere, less any whose next-boot host (the pinned one,
- // else the saved config read back) still reaches it, and less any whose host it could not read. A
- // container is left running when that status read had failed, or when the next boot still dials it
- // (dropped above).
+ // else the saved config read back, else the one the save sent when that read fails) still reaches it, and
+ // less any whose host it cannot tell. A container is left running when that status read had failed, or when
+ // the next boot still dials it (dropped above).
// Only ever tear down OpenWA-managed services. An arbitrary profile name (or the empty string)
// would otherwise reach stopManagedService and, via container-name matching, could stop an unrelated
// container — so constrain teardown to the managed allowlist and drop anything else.