| import { resolve } from "node:path"; |
|
|
| function buildAdminConnectionString(port: number) { |
| return `postgres://paperclip:paperclip@127.0.0.1:${port}/postgres`; |
| } |
|
|
| function normalizePath(value: string) { |
| return resolve(value); |
| } |
|
|
| export async function resolveReusableEmbeddedPostgresPort(input: { |
| dataDir: string; |
| configuredPort: number; |
| runningPid: number | null; |
| runningPort: number | null; |
| getPostgresDataDirectory: (connectionString: string) => Promise<string | null>; |
| ensurePostgresDatabase: (connectionString: string, databaseName: string) => Promise<unknown>; |
| removePidFile: () => void; |
| logWarn?: (message: string) => void; |
| }): Promise<number | null> { |
| if (!input.runningPid) return null; |
|
|
| const candidatePort = input.runningPort ?? input.configuredPort; |
| const connectionString = buildAdminConnectionString(candidatePort); |
|
|
| try { |
| const actualDataDir = await input.getPostgresDataDirectory(connectionString); |
| if (!actualDataDir || normalizePath(actualDataDir) !== normalizePath(input.dataDir)) { |
| input.logWarn?.( |
| `Embedded PostgreSQL pid file pointed at a reachable postgres on port ${candidatePort}, but its data dir did not match ${input.dataDir}; ignoring stale pid file.`, |
| ); |
| input.removePidFile(); |
| return null; |
| } |
|
|
| await input.ensurePostgresDatabase(connectionString, "paperclip"); |
| return candidatePort; |
| } catch { |
| input.logWarn?.( |
| `Embedded PostgreSQL pid file pointed at pid ${input.runningPid}, but port ${candidatePort} was not reusable; ignoring stale pid file.`, |
| ); |
| input.removePidFile(); |
| return null; |
| } |
| } |
|
|