Skip to content

Commit

Permalink
fix: retry api now resets scraped_at (#816)
Browse files Browse the repository at this point in the history
* fix: retry api now resets scraped_at

* fix: set scraped_times to 1

---------

Co-authored-by: Gaisberg <None>
Co-authored-by: Spoked <[email protected]>
  • Loading branch information
Gaisberg and dreulavelle authored Oct 26, 2024
1 parent 0de32fd commit 2676fe8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/program/db/db_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def get_item(session, media_item_id, item_type):
continue
item = get_item(session, media_item_id, item_type)
if item:
session.expunge(item)
yield item

def get_parent_ids(media_item_ids: list[int]):
Expand Down
26 changes: 14 additions & 12 deletions src/routers/secure/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Literal, Optional

import Levenshtein
from fastapi import APIRouter, HTTPException, Request
from fastapi import APIRouter, HTTPException, Request, status
from program.content import Overseerr
from program.db.db import db
from program.db.db_functions import (
Expand Down Expand Up @@ -39,7 +39,7 @@
def handle_ids(ids: str) -> list[int]:
ids = [int(id) for id in ids.split(",")] if "," in ids else [int(ids)]
if not ids:
raise HTTPException(status_code=400, detail="No item ID provided")
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="No item ID provided")
return ids


Expand Down Expand Up @@ -308,15 +308,18 @@ class RetryResponse(BaseModel):
async def retry_items(request: Request, ids: str) -> RetryResponse:
"""Re-add items to the queue"""
ids = handle_ids(ids)
try:
media_items_generator = get_media_items_by_ids(ids)
for media_item in media_items_generator:
request.app.program.em.cancel_job(media_item._id)
await asyncio.sleep(0.1) # Ensure cancellation is processed
# request.app.program.em.add_item(media_item)
request.app.program.em.add_event(Event("RetryItem", media_item._id))
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
for id in ids:
try:
item = next(get_media_items_by_ids([id]), None)
if item:
with db.Session() as session:
item.scraped_at = None
item.scraped_times = 1
session.merge(item)
session.commit()
request.app.program.em.add_event(Event("RetryItem", id))
except ValueError as e:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))

return {"message": f"Retried items with ids {ids}", "ids": ids}

Expand All @@ -338,7 +341,6 @@ async def remove_item(request: Request, ids: str) -> RemoveResponse:
media_items: list[int] = get_parent_ids(ids)
if not media_items:
return HTTPException(status_code=404, detail="Item(s) not found")

for item_id in media_items:
logger.debug(f"Removing item with ID {item_id}")
request.app.program.em.cancel_job(item_id)
Expand Down

0 comments on commit 2676fe8

Please sign in to comment.