Skip to content

Commit 56348ef

Browse files
committed
Merge branch 'cap/fix-wi201-targets-add' into 'main'
fix(cli): apply targets config after add Closes #201 See merge request postgres-ai/postgresai!279
2 parents af80b7e + 7ed9fdb commit 56348ef

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

cli/bin/postgres-ai.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,6 +2265,18 @@ function updatePgwatchConfig(configPath: string, updates: Record<string, string>
22652265
fs.writeFileSync(configPath, lines.join("\n") + "\n", { encoding: "utf8", mode: 0o600 });
22662266
}
22672267

2268+
/**
2269+
* Regenerate pgwatch sources and recreate collectors so target changes take effect without `mon restart`.
2270+
*/
2271+
async function applyMonitoringTargetsConfig(): Promise<number> {
2272+
console.log("Applying monitoring target configuration...");
2273+
const generateCode = await runCompose(["run", "--rm", "sources-generator"]);
2274+
if (generateCode !== 0) return generateCode;
2275+
2276+
console.log("Restarting pgwatch collectors to pick up target changes...");
2277+
return runCompose(["up", "-d", "--force-recreate", "pgwatch-prometheus", "pgwatch-postgres"]);
2278+
}
2279+
22682280
/**
22692281
* Run docker compose command
22702282
*/
@@ -3341,6 +3353,14 @@ targets
33413353
try {
33423354
addInstanceToFile(file, buildInstance(instanceName, connStr));
33433355
console.log(`Monitoring target '${instanceName}' added`);
3356+
3357+
const applyCode = await applyMonitoringTargetsConfig();
3358+
if (applyCode !== 0) {
3359+
console.error("Monitoring target was saved, but applying the generated pgwatch sources failed. Run 'postgresai mon restart' to apply manually.");
3360+
process.exitCode = 1;
3361+
return;
3362+
}
3363+
console.log("✓ Monitoring target configuration applied");
33443364
} catch (err) {
33453365
// Surface InstancesParseError as-is so we don't silently overwrite a
33463366
// corrupted file (which could discard several targets, including the
@@ -3369,6 +3389,14 @@ targets
33693389
return;
33703390
}
33713391
console.log(`Monitoring target '${name}' removed`);
3392+
3393+
const applyCode = await applyMonitoringTargetsConfig();
3394+
if (applyCode !== 0) {
3395+
console.error("Monitoring target was removed, but applying the generated pgwatch sources failed. Run 'postgresai mon restart' to apply manually.");
3396+
process.exitCode = 1;
3397+
return;
3398+
}
3399+
console.log("✓ Monitoring target configuration applied");
33723400
} catch (err) {
33733401
const message = err instanceof Error ? err.message : String(err);
33743402
console.error(`Error processing instances.yml: ${message}`);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { readFileSync } from "fs";
3+
import { resolve } from "path";
4+
5+
const cliSource = readFileSync(resolve(import.meta.dir, "../bin/postgres-ai.ts"), "utf8");
6+
7+
describe("mon targets configuration apply", () => {
8+
test("targets add regenerates sources and restarts pgwatch collectors after saving", () => {
9+
expect(cliSource).toContain("async function applyMonitoringTargetsConfig()");
10+
expect(cliSource).toContain('runCompose(["run", "--rm", "sources-generator"])');
11+
expect(cliSource).toContain(
12+
'runCompose(["up", "-d", "--force-recreate", "pgwatch-prometheus", "pgwatch-postgres"])'
13+
);
14+
15+
const saveIndex = cliSource.indexOf("addInstanceToFile(file, buildInstance(instanceName, connStr))");
16+
const applyIndex = cliSource.indexOf("await applyMonitoringTargetsConfig()", saveIndex);
17+
expect(saveIndex).toBeGreaterThan(-1);
18+
expect(applyIndex).toBeGreaterThan(saveIndex);
19+
});
20+
21+
test("targets remove regenerates sources and restarts pgwatch collectors after saving", () => {
22+
const removeIndex = cliSource.indexOf("removeInstanceFromFile(file, name)");
23+
const applyIndex = cliSource.indexOf("await applyMonitoringTargetsConfig()", removeIndex);
24+
expect(removeIndex).toBeGreaterThan(-1);
25+
expect(applyIndex).toBeGreaterThan(-1);
26+
expect(applyIndex).toBeGreaterThan(removeIndex);
27+
});
28+
});

0 commit comments

Comments
 (0)