| 28 | |
| 29 | |
| 30 | def wait_for_postgres(max_retries=5, retry_interval=5): |
| 31 | retries = 0 |
| 32 | while retries < max_retries: |
| 33 | try: |
| 34 | conn = connect(config.DATABASE_URL) |
| 35 | conn.close() |
| 36 | logger.info("Successfully connected to PostgreSQL") |
| 37 | return |
| 38 | except OperationalError: |
| 39 | logger.warning( |
| 40 | f"PostgreSQL is not ready. Retrying in {retry_interval} seconds... (Attempt {retries + 1}/{max_retries})" |
| 41 | ) |
| 42 | time.sleep(retry_interval) |
| 43 | retries += 1 |
| 44 | |
| 45 | raise Exception("Failed to connect to PostgreSQL after multiple attempts") |
| 46 | |
| 47 | |
| 48 | wait_for_postgres() |