fix: retry and try catch exception when update entity

This commit is contained in:
arkohut 2024-07-29 16:09:06 +08:00
parent 873034c76b
commit 6d18a3aa05

View File

@ -300,7 +300,9 @@ def scan(
limit = 100 limit = 100
offset = 0 offset = 0
total_entities = 0 # We'll update this after the first request total_entities = 0 # We'll update this after the first request
with tqdm(total=total_entities, desc="Checking for deleted files", leave=True) as pbar2: with tqdm(
total=total_entities, desc="Checking for deleted files", leave=True
) as pbar2:
while True: while True:
existing_files_response = httpx.get( existing_files_response = httpx.get(
f"{BASE_URL}/libraries/{library_id}/folders/{folder['id']}/entities", f"{BASE_URL}/libraries/{library_id}/folders/{folder['id']}/entities",
@ -378,7 +380,11 @@ async def update_entity(
new_entity, new_entity,
existing_entity, existing_entity,
) -> Tuple[FileStatus, bool, httpx.Response]: ) -> Tuple[FileStatus, bool, httpx.Response]:
MAX_RETRIES = 3
RETRY_DELAY = 2.0
async with semaphore: async with semaphore:
for attempt in range(MAX_RETRIES):
try:
update_response = await client.put( update_response = await client.put(
f"{BASE_URL}/entities/{existing_entity['id']}", f"{BASE_URL}/entities/{existing_entity['id']}",
json=new_entity, json=new_entity,
@ -389,9 +395,27 @@ async def update_entity(
timeout=60, timeout=60,
) )
if 200 <= update_response.status_code < 300: if 200 <= update_response.status_code < 300:
return new_entity["filepath"], FileStatus.UPDATED, True, update_response return (
new_entity["filepath"],
FileStatus.UPDATED,
True,
update_response,
)
else: else:
return new_entity["filepath"], FileStatus.UPDATED, False, update_response return (
new_entity["filepath"],
FileStatus.UPDATED,
False,
update_response,
)
except Exception as e:
logging.error(
f"Error while updating entity {existing_entity['id']} (attempt {attempt + 1}/{MAX_RETRIES}): {e}"
)
if attempt < MAX_RETRIES - 1:
await asyncio.sleep(RETRY_DELAY)
else:
return new_entity["filepath"], FileStatus.UPDATED, False, None
@lib_app.command("index") @lib_app.command("index")