From a02fb1f2bdb6ef9de391c47bb74e5bbda7d4151b Mon Sep 17 00:00:00 2001 From: "claude[bot]" <209825114+claude[bot]@users.noreply.github.com> Date: Sat, 28 Jun 2025 12:25:39 +0000 Subject: [PATCH] fix: pgforge service autostart functionality - Fix service file generation in ServiceManager: * Change Type=forking to Type=notify for better PostgreSQL integration * Add missing PostgreSQL options (-k socket directory, -p port) * Add proper ExecStop command for graceful shutdown * Increase timeouts from 0 to 120 seconds for proper startup/shutdown - Fix CLI commands to use service-aware methods: * start command now uses startInstanceWithService() * stop command now uses stopInstanceWithService() * restart command updated to use service-aware methods - Update restartInstance() to be service-aware * Now uses service-aware stop/start methods for consistency These changes ensure PostgreSQL instances with enabled services: - Properly autostart after system restart via systemd - Use consistent service management for manual operations - Have proper systemd service configuration Fixes #52 Co-authored-by: Samuel --- index.ts | 4 ++-- src/instance/manager.ts | 4 ++-- src/service/manager.ts | 11 ++++++++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/index.ts b/index.ts index 1411949..11f4c33 100644 --- a/index.ts +++ b/index.ts @@ -127,7 +127,7 @@ program const spinner = ora(`Starting instance '${name}'...`).start(); try { - await instanceManager.startInstance(name); + await instanceManager.startInstanceWithService(name); spinner.succeed(`Instance '${name}' started successfully`); const config = await instanceManager.getInstanceStatus(name); @@ -154,7 +154,7 @@ program const spinner = ora(`Stopping instance '${name}'...`).start(); try { - await instanceManager.stopInstance(name); + await instanceManager.stopInstanceWithService(name); spinner.succeed(`Instance '${name}' stopped successfully`); } catch (error) { diff --git a/src/instance/manager.ts b/src/instance/manager.ts index b11ba8a..9312ab9 100644 --- a/src/instance/manager.ts +++ b/src/instance/manager.ts @@ -150,12 +150,12 @@ export class InstanceManager { } if (config.status?.state === 'running') { - await this.stopInstance(name); + await this.stopInstanceWithService(name); // Wait a moment for cleanup await new Promise(resolve => setTimeout(resolve, 2000)); } - await this.startInstance(name); + await this.startInstanceWithService(name); } async getInstanceStatus(name: string): Promise { diff --git a/src/service/manager.ts b/src/service/manager.ts index 23d308d..763f0d5 100644 --- a/src/service/manager.ts +++ b/src/service/manager.ts @@ -205,6 +205,9 @@ export class ServiceManager { const restartSec = config.spec.service?.restartSec || 5; const user = useUserService ? process.env.USER || 'postgres' : 'postgres'; + // Generate socket directory path from data directory + const socketDir = join(config.spec.storage.dataDirectory, 'sockets'); + return `[Unit] Description=PostgreSQL database server for ${config.metadata.name} Documentation=man:postgres(1) @@ -212,13 +215,15 @@ After=network.target Wants=network.target [Service] -Type=forking +Type=notify User=${user} -ExecStart=${postgresPath} -D ${config.spec.storage.dataDirectory} +ExecStart=${postgresPath} -D ${config.spec.storage.dataDirectory} -k ${socketDir} -p ${config.spec.network.port} ExecReload=/bin/kill -HUP $MAINPID +ExecStop=/bin/kill -TERM $MAINPID KillMode=mixed KillSignal=SIGINT -TimeoutSec=0 +TimeoutSec=120 +TimeoutStopSec=120 # Restart policy Restart=${restartPolicy}