@@ -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 } ` ) ;
0 commit comments