altre fix
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#!.venv/bin/python
|
||||
import logging
|
||||
import asyncio
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -50,29 +51,52 @@ async def load_data(cfg: object, matrice_valori: list, pool) -> bool :
|
||||
|
||||
async with pool.acquire() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
try:
|
||||
await cur.executemany(sql_insert_RAWDATA, matrice_valori)
|
||||
await conn.commit()
|
||||
logging.info("Data loaded.")
|
||||
rc = True
|
||||
except Exception as e:
|
||||
await conn.rollback()
|
||||
logging.error(f"Error: {e}.")
|
||||
rc = False
|
||||
finally:
|
||||
return rc
|
||||
rc = False
|
||||
for attempt in range(cfg.max_retries):
|
||||
try:
|
||||
await cur.executemany(sql_insert_RAWDATA, matrice_valori)
|
||||
await conn.commit()
|
||||
logging.info("Data loaded.")
|
||||
rc = True
|
||||
except Exception as e:
|
||||
await conn.rollback()
|
||||
logging.error(f"Error: {e}.")
|
||||
|
||||
if e.args[0] == 1213: # Deadlock detected
|
||||
logging.warning(f"Deadlock detected, attempt {attempt + 1}/{cfg.max_retries}")
|
||||
|
||||
if attempt < cfg.max_retries - 1:
|
||||
delay = (2 * attempt)
|
||||
await asyncio.sleep(delay)
|
||||
continue
|
||||
else:
|
||||
logging.error("Max retry attempts reached for deadlock")
|
||||
raise
|
||||
finally:
|
||||
return rc
|
||||
|
||||
async def update_status(cfg: object, id: int, status: int, pool) -> None:
|
||||
async with pool.acquire() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
try:
|
||||
await cur.execute(f'update {cfg.dbrectable} set locked = 0, status = {status}, {timestamp_cols[status]} = now() where id = {id}')
|
||||
await cur.execute(f'update {cfg.dbrectable} set status = {status}, {timestamp_cols[status]} = now() where id = {id}')
|
||||
await conn.commit()
|
||||
logging.info("Status updated.")
|
||||
except Exception as e:
|
||||
await conn.rollback()
|
||||
logging.error(f'Error: {e}')
|
||||
|
||||
async def unlock(cfg: object, id: int, pool) -> None:
|
||||
async with pool.acquire() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
try:
|
||||
await cur.execute(f'update {cfg.dbrectable} set locked = 0 where id = {id}')
|
||||
await conn.commit()
|
||||
logging.info(f"id {id} unlocked.")
|
||||
except Exception as e:
|
||||
await conn.rollback()
|
||||
logging.error(f'Error: {e}')
|
||||
|
||||
async def get_matlab_cmd(cfg: object, unit: str, tool: str, pool) -> tuple:
|
||||
async with pool.acquire() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
|
||||
Reference in New Issue
Block a user