Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): never stop a service the next boot still dials

Yudhi Armyndharis80a8cf3939c4
diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.ts
index b705b34da..22ea41384 100644
--- a/src/modules/infra/infra-config.controller.spec.ts
+++ b/src/modules/infra/infra-config.controller.spec.ts
@@ -1609,6 +1609,80 @@ describe('InfraConfigController.requestRestart constrains teardown to managed pr
});
});
+ // The restart never stops a service the NEXT boot still dials, whatever the page asked for: the host the environment
+ // pins when it pins one, else the host the saved file holds (the save that precedes the restart is already on disk).
+ // The profile is the container to stop; PROFILE_OF maps the three managed services to it.
+ describe('a service whose next-boot host is the bundled one is never stopped', () => {
+ const PROFILE_OF: Record<ServiceName, string> = { database: 'postgres', redis: 'redis', storage: 'minio' };
+ const HOST_STATES = ['absent', 'bundled', 'external'] as const;
+ let pinnedKeys: string[] = [];
+
+ const stoppedFor = async (service: ServiceName, filePin: string | undefined, envPin: string | undefined) => {
+ const spec = SERVICES[service];
+ const lines = [`${spec.selection}=${spec.selected}`, `${spec.flag}=false`];
+ if (filePin !== undefined) lines.push(`${spec.host}=${filePin}`);
+ (fs.existsSync as jest.Mock).mockReturnValue(true);
+ (fs.readFileSync as jest.Mock).mockReturnValue(`${lines.join('\n')}\n`);
+ if (envPin !== undefined) {
+ process.env[spec.host] = envPin;
+ pinnedKeys.push(spec.host);
+ recordPinnedEnvKeys({ [spec.host]: envPin });
+ } else {
+ recordPinnedEnvKeys({});
+ }
+ const stopManagedService = jest.fn().mockResolvedValue(true);
+ const controller = buildController({
+ isDockerAvailable: () => true,
+ stopManagedService,
+ orchestrateProfiles: jest.fn().mockResolvedValue({}),
+ });
+
+ const result = await controller.requestRestart({ profilesToRemove: [PROFILE_OF[service]] });
+
+ return { stopped: stopManagedService.mock.calls.length > 0, kept: result.profilesToRemove.length === 0 };
+ };
+
+ afterEach(() => {
+ for (const key of pinnedKeys) delete process.env[key];
+ pinnedKeys = [];
+ recordPinnedEnvKeys({});
+ (fs.existsSync as jest.Mock).mockReturnValue(false);
+ (fs.readFileSync as jest.Mock).mockReturnValue('');
+ });
+
+ describe.each(SERVICE_NAMES)('%s', service => {
+ const hostFor = (state: (typeof HOST_STATES)[number]) =>
+ state === 'absent'
+ ? undefined
+ : state === 'bundled'
+ ? SERVICES[service].bundledHost
+ : SERVICES[service].externalHost;
+ for (const fileHost of HOST_STATES) {
+ for (const envHost of HOST_STATES) {
+ const file = hostFor(fileHost);
+ const env = hostFor(envHost);
+ // The next boot dials the environment's host when it pins one, else the file's.
+ const dialled = envHost !== 'absent' ? env : file;
+ const keeps = dialled === SERVICES[service].bundledHost;
+ it(`file host ${fileHost}, environment host ${envHost}: ${keeps ? 'kept running' : 'stopped'}`, async () => {
+ const { stopped, kept } = await stoppedFor(service, file, env);
+
+ expect(stopped).toBe(!keeps);
+ expect(kept).toBe(keeps);
+ });
+ }
+ }
+ });
+
+ it('keeps the container when the flag is pinned off over a stored bundled host', async () => {
+ process.env.REDIS_BUILTIN = 'false';
+ pinnedKeys.push('REDIS_BUILTIN');
+ const { stopped } = await stoppedFor('redis', 'redis', undefined);
+
+ expect(stopped).toBe(false);
+ });
+ });
+
it('starts only allowlisted profiles, never an unknown entry (symmetry with teardown)', async () => {
const orchestrateProfiles = jest.fn().mockResolvedValue({});
const controller = buildController({
diff --git a/src/modules/infra/infra-config.controller.ts b/src/modules/infra/infra-config.controller.ts
index 0db13f326..e52aec7c8 100644
--- a/src/modules/infra/infra-config.controller.ts
+++ b/src/modules/infra/infra-config.controller.ts
@@ -452,16 +452,24 @@ export class InfraConfigController {
removal?: object;
}> {
const profiles = body?.profiles || [];
- // Never stop a bundled service the environment pins the app to (e.g. the documented manual
- // built-in Postgres with DATABASE_HOST=postgres in .env): that pin outranks the saved config, so
- // the restarted app would still point at the container and fail to boot.
- const pinnedToBundled = Object.entries(BUNDLED_SERVICE_ENV)
- .filter(([, [key, value]]) => isEnvPinned(key) && process.env[key] === value)
- .map(([profile]) => profile);
+ // Never stop a bundled service the NEXT boot still dials, whatever the page asked for: the restarted
+ // app would point at a stopped container and fail to boot, with the dashboard that could undo it. The
+ // host it dials is the one the environment pins (e.g. the documented manual built-in Postgres with
+ // DATABASE_HOST=postgres in .env, which outranks the saved config), else the one in the saved file,
+ // which already holds the save that precedes this restart. A host moved away from the bundled name
+ // still stops its container.
+ let saved: Record<string, string> | undefined;
+ const dialsBundled = (profile: string): boolean => {
+ const bundled = BUNDLED_SERVICE_ENV[profile];
+ if (!bundled) return false;
+ const [key, value] = bundled;
+ return (isEnvPinned(key) ? process.env[key] : (saved ??= readGeneratedEnv())[key]) === value;
+ };
const requestedRemoval = body?.profilesToRemove || [];
- const profilesToRemove = requestedRemoval.filter(p => !pinnedToBundled.includes(p));
- if (profilesToRemove.length < requestedRemoval.length) {
- this.logger.warn('Keeping profiles the environment pins the app to', { pinnedToBundled });
+ const keptRunning = requestedRemoval.filter(dialsBundled);
+ const profilesToRemove = requestedRemoval.filter(p => !keptRunning.includes(p));
+ if (keptRunning.length > 0) {
+ this.logger.warn('Keeping profiles the next boot still dials', { keptRunning });
}
let orchestrationResult: object | undefined;
// Teardown is stop-only (see DockerService.stopManagedService): containers are stopped and