Auto-update: Tue Jul 30 12:12:04 PDT 2024
This commit is contained in:
parent
2f1384bc23
commit
c4be5cbd7c
1 changed files with 85 additions and 69 deletions
|
@ -332,27 +332,34 @@ class APIConfig(BaseModel):
|
||||||
if pool_entry is None:
|
if pool_entry is None:
|
||||||
pool_entry = self.local_db
|
pool_entry = self.local_db
|
||||||
|
|
||||||
info(f"Attempting to connect to database: {pool_entry}")
|
for attempt in range(3): # Retry up to 3 times
|
||||||
try:
|
try:
|
||||||
conn = await self.db_pool.get_connection(pool_entry)
|
conn = await asyncpg.connect(
|
||||||
|
host=pool_entry['ts_ip'],
|
||||||
|
port=pool_entry['db_port'],
|
||||||
|
user=pool_entry['db_user'],
|
||||||
|
password=pool_entry['db_pass'],
|
||||||
|
database=pool_entry['db_name']
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
yield conn
|
yield conn
|
||||||
finally:
|
finally:
|
||||||
await self.db_pool.release_connection(pool_entry, conn)
|
await conn.close()
|
||||||
except asyncpg.exceptions.ConnectionDoesNotExistError:
|
return
|
||||||
err(f"Connection to database {pool_entry['ts_ip']}:{pool_entry['db_port']} does not exist or has been closed")
|
except asyncpg.exceptions.CannotConnectNowError:
|
||||||
raise
|
if attempt < 2: # Don't sleep on the last attempt
|
||||||
except asyncpg.exceptions.ConnectionFailureError as e:
|
await asyncio.sleep(1) # Wait before retrying
|
||||||
err(f"Failed to connect to database: {pool_entry['ts_ip']}:{pool_entry['db_port']}")
|
|
||||||
err(f"Connection error: {str(e)}")
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
err(f"Unexpected error connecting to database: {pool_entry['ts_ip']}:{pool_entry['db_port']}")
|
err(f"Failed to connect to database: {pool_entry['ts_ip']}:{pool_entry['db_port']}")
|
||||||
err(f"Error: {str(e)}")
|
err(f"Error: {str(e)}")
|
||||||
|
if attempt == 2: # Raise the exception on the last attempt
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
raise Exception(f"Failed to connect to database after 3 attempts: {pool_entry['ts_ip']}:{pool_entry['db_port']}")
|
||||||
|
|
||||||
async def initialize_sync(self):
|
async def initialize_sync(self):
|
||||||
for pool_entry in self.POOL:
|
for pool_entry in self.POOL:
|
||||||
|
for attempt in range(3): # Retry up to 3 times
|
||||||
try:
|
try:
|
||||||
async with self.get_connection(pool_entry) as conn:
|
async with self.get_connection(pool_entry) as conn:
|
||||||
tables = await conn.fetch("""
|
tables = await conn.fetch("""
|
||||||
|
@ -366,11 +373,15 @@ class APIConfig(BaseModel):
|
||||||
await self.create_sync_trigger(conn, table_name)
|
await self.create_sync_trigger(conn, table_name)
|
||||||
|
|
||||||
info(f"Sync initialization complete for {pool_entry['ts_ip']}. All tables now have version and server_id columns with appropriate triggers.")
|
info(f"Sync initialization complete for {pool_entry['ts_ip']}. All tables now have version and server_id columns with appropriate triggers.")
|
||||||
|
break # If successful, break the retry loop
|
||||||
except asyncpg.exceptions.ConnectionFailureError:
|
except asyncpg.exceptions.ConnectionFailureError:
|
||||||
err(f"Failed to connect to database during initialization: {pool_entry['ts_ip']}:{pool_entry['db_port']}")
|
err(f"Failed to connect to database during initialization: {pool_entry['ts_ip']}:{pool_entry['db_port']}")
|
||||||
|
if attempt < 2: # Don't sleep on the last attempt
|
||||||
|
await asyncio.sleep(1) # Wait before retrying
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
err(f"Error initializing sync for {pool_entry['ts_ip']}: {str(e)}")
|
err(f"Error initializing sync for {pool_entry['ts_ip']}: {str(e)}")
|
||||||
err(f"Traceback: {traceback.format_exc()}")
|
err(f"Traceback: {traceback.format_exc()}")
|
||||||
|
break # Don't retry for unexpected errors
|
||||||
|
|
||||||
async def ensure_sync_columns(self, conn, table_name):
|
async def ensure_sync_columns(self, conn, table_name):
|
||||||
try:
|
try:
|
||||||
|
@ -383,7 +394,7 @@ class APIConfig(BaseModel):
|
||||||
ADD COLUMN IF NOT EXISTS server_id TEXT DEFAULT '{os.environ.get('TS_ID')}';
|
ADD COLUMN IF NOT EXISTS server_id TEXT DEFAULT '{os.environ.get('TS_ID')}';
|
||||||
EXCEPTION
|
EXCEPTION
|
||||||
WHEN duplicate_column THEN
|
WHEN duplicate_column THEN
|
||||||
RAISE NOTICE 'column version or server_id already exists in {table_name}.';
|
NULL; -- Silently handle duplicate column
|
||||||
END;
|
END;
|
||||||
END $$;
|
END $$;
|
||||||
""")
|
""")
|
||||||
|
@ -392,6 +403,7 @@ class APIConfig(BaseModel):
|
||||||
err(f"Error ensuring sync columns for table {table_name}: {str(e)}")
|
err(f"Error ensuring sync columns for table {table_name}: {str(e)}")
|
||||||
err(f"Traceback: {traceback.format_exc()}")
|
err(f"Traceback: {traceback.format_exc()}")
|
||||||
|
|
||||||
|
|
||||||
async def create_sync_trigger(self, conn, table_name):
|
async def create_sync_trigger(self, conn, table_name):
|
||||||
await conn.execute(f"""
|
await conn.execute(f"""
|
||||||
CREATE OR REPLACE FUNCTION update_version_and_server_id()
|
CREATE OR REPLACE FUNCTION update_version_and_server_id()
|
||||||
|
@ -421,6 +433,7 @@ class APIConfig(BaseModel):
|
||||||
if pool_entry['ts_id'] == os.environ.get('TS_ID'):
|
if pool_entry['ts_id'] == os.environ.get('TS_ID'):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
for _ in range(3): # Retry up to 3 times
|
||||||
try:
|
try:
|
||||||
async with self.get_connection(pool_entry) as conn:
|
async with self.get_connection(pool_entry) as conn:
|
||||||
# Check if the version column exists in any table
|
# Check if the version column exists in any table
|
||||||
|
@ -435,7 +448,7 @@ class APIConfig(BaseModel):
|
||||||
|
|
||||||
if not version_exists:
|
if not version_exists:
|
||||||
info(f"Version column does not exist in any table for {pool_entry['ts_id']}")
|
info(f"Version column does not exist in any table for {pool_entry['ts_id']}")
|
||||||
continue
|
break # Move to the next pool entry
|
||||||
|
|
||||||
version = await conn.fetchval("""
|
version = await conn.fetchval("""
|
||||||
SELECT COALESCE(MAX(version), -1)
|
SELECT COALESCE(MAX(version), -1)
|
||||||
|
@ -451,10 +464,13 @@ class APIConfig(BaseModel):
|
||||||
if version > max_version:
|
if version > max_version:
|
||||||
max_version = version
|
max_version = version
|
||||||
most_recent_source = pool_entry
|
most_recent_source = pool_entry
|
||||||
except asyncpg.exceptions.ConnectionFailureError:
|
break # If successful, break the retry loop
|
||||||
err(f"Failed to connect to database: {pool_entry['ts_ip']}:{pool_entry['db_port']}")
|
except asyncpg.exceptions.PostgresError as e:
|
||||||
except Exception as e:
|
|
||||||
err(f"Error checking version for {pool_entry['ts_id']}: {str(e)}")
|
err(f"Error checking version for {pool_entry['ts_id']}: {str(e)}")
|
||||||
|
await asyncio.sleep(1) # Wait before retrying
|
||||||
|
except Exception as e:
|
||||||
|
err(f"Unexpected error for {pool_entry['ts_id']}: {str(e)}")
|
||||||
|
break # Don't retry for unexpected errors
|
||||||
|
|
||||||
if most_recent_source:
|
if most_recent_source:
|
||||||
info(f"Most recent source: {most_recent_source['ts_id']} with version {max_version}")
|
info(f"Most recent source: {most_recent_source['ts_id']} with version {max_version}")
|
||||||
|
|
Loading…
Reference in a new issue