feat: harden containers and ci
This commit is contained in:
		
							
								
								
									
										65
									
								
								backend/scripts/wait-for-db.js
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										65
									
								
								backend/scripts/wait-for-db.js
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,65 @@ | ||||
| #!/usr/bin/env node | ||||
| const { Client } = require('pg'); | ||||
|  | ||||
| const SECOND = 1000; | ||||
| const DEFAULT_TIMEOUT = Number.parseInt(process.env.DB_WAIT_TIMEOUT_MS || `${60 * SECOND}`, 10); | ||||
| const RETRY_INTERVAL = Number.parseInt(process.env.DB_WAIT_RETRY_MS || '2000', 10); | ||||
|  | ||||
| function deriveConnectionString() { | ||||
|   if (process.env.DATABASE_URL) { | ||||
|     return process.env.DATABASE_URL; | ||||
|   } | ||||
|  | ||||
|   const { | ||||
|     POSTGRES_USER = 'merchantsofhope_user', | ||||
|     POSTGRES_PASSWORD, | ||||
|     POSTGRES_DB = 'merchantsofhope_supplyanddemandportal', | ||||
|     POSTGRES_HOST = 'merchantsofhope-supplyanddemandportal-database', | ||||
|     POSTGRES_PORT = '5432' | ||||
|   } = process.env; | ||||
|  | ||||
|   if (!POSTGRES_PASSWORD) { | ||||
|     return null; | ||||
|   } | ||||
|  | ||||
|   return `postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}`; | ||||
| } | ||||
|  | ||||
| const connectionString = deriveConnectionString(); | ||||
|  | ||||
| if (!connectionString) { | ||||
|   console.error('[wait-for-db] DATABASE_URL or POSTGRES_PASSWORD must be provided.'); | ||||
|   process.exit(1); | ||||
| } | ||||
|  | ||||
| const startTime = Date.now(); | ||||
|  | ||||
| async function waitForDatabase() { | ||||
|   const client = new Client({ | ||||
|     connectionString, | ||||
|     connectionTimeoutMillis: Math.min(RETRY_INTERVAL, DEFAULT_TIMEOUT) | ||||
|   }); | ||||
|  | ||||
|   try { | ||||
|     await client.connect(); | ||||
|   } catch (error) { | ||||
|     const elapsed = Date.now() - startTime; | ||||
|     if (elapsed >= DEFAULT_TIMEOUT) { | ||||
|       console.error('[wait-for-db] Timed out waiting for database connection.'); | ||||
|       console.error(error); | ||||
|       process.exit(1); | ||||
|     } | ||||
|  | ||||
|     const remaining = DEFAULT_TIMEOUT - elapsed; | ||||
|     const delay = Math.min(RETRY_INTERVAL, remaining); | ||||
|     console.info(`[wait-for-db] Database not ready yet. Retrying in ${delay}ms...`); | ||||
|     await new Promise((resolve) => setTimeout(resolve, delay)); | ||||
|     return waitForDatabase(); | ||||
|   } finally { | ||||
|     await client.end().catch(() => {}); | ||||
|   } | ||||
|  | ||||
|   console.info('[wait-for-db] Database connection established.'); | ||||
| } | ||||
|  | ||||
| waitForDatabase().then(() => process.exit(0)); | ||||
		Reference in New Issue
	
	Block a user