Skip to content

Commit 26ca5f6

Browse files
committed
chore: wip
1 parent 09d81ac commit 26ca5f6

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

packages/launchpad/src/services/database.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-console */
22
import { spawn } from 'node:child_process'
33
import fs from 'node:fs'
4+
import net from 'node:net'
45
import path from 'node:path'
56
import process from 'node:process'
67
import { findBinaryInPath } from '../utils'
@@ -54,21 +55,39 @@ async function createPostgreSQLDatabase(dbName: string, options: DatabaseOptions
5455

5556
try {
5657
// Ensure server is accepting TCP connections before attempting to create DB
57-
const pgIsReady = findBinaryInPath('pg_isready') || 'pg_isready'
58+
const maxAttempts = 20
5859
let ready = false
59-
for (let i = 0; i < 12; i++) {
60-
try {
61-
await executeCommand([pgIsReady, '-h', host, '-p', String(port)])
60+
for (let i = 0; i < maxAttempts; i++) {
61+
const attempt = await new Promise<boolean>((resolve) => {
62+
const socket = net.connect({ host, port, timeout: 1000 }, () => {
63+
socket.end()
64+
resolve(true)
65+
})
66+
socket.on('error', () => resolve(false))
67+
socket.on('timeout', () => {
68+
socket.destroy()
69+
resolve(false)
70+
})
71+
})
72+
if (attempt) {
6273
ready = true
6374
break
6475
}
65-
catch {
66-
await new Promise(r => setTimeout(r, 500 + i * 250))
67-
}
76+
await new Promise(r => setTimeout(r, 250 + i * 150))
6877
}
6978
if (!ready) {
70-
throw new Error('PostgreSQL not accepting connections yet')
79+
// As a fallback, try pg_isready if available
80+
const pgIsReady = findBinaryInPath('pg_isready') || 'pg_isready'
81+
try {
82+
await executeCommand([pgIsReady, '-h', host, '-p', String(port)])
83+
ready = true
84+
}
85+
catch {}
7186
}
87+
if (!ready)
88+
throw new Error('PostgreSQL not accepting connections yet')
89+
// Small grace period after accept
90+
await new Promise(r => setTimeout(r, 250))
7291

7392
// Check if database exists
7493
const checkCommand = ['psql', '-h', host, '-p', String(port), '-U', user, '-lqt']

0 commit comments

Comments
 (0)