diff --git a/importarr/main.py b/importarr/main.py index 10e3ef2..66c7c75 100644 --- a/importarr/main.py +++ b/importarr/main.py @@ -275,6 +275,7 @@ async def sync_queue() -> None: state.upsert_queue_item(source_type="system", source_id="sab-sync", name="SABnzbd", state="failed", reason=exc.__class__.__name__) return slots = data.get("history", {}).get("slots", []) + state.delete_queue_items_by_state("sab", "ignored", "SAB category is not owned by Importarr") for item in slots: readiness = classify_history_item(item, active, settings.sab_category, settings.download_root) job_id = str(item.get("nzo_id") or item.get("nzoid") or item.get("name") or "") diff --git a/importarr/state.py b/importarr/state.py index f6f598f..fa152d4 100644 --- a/importarr/state.py +++ b/importarr/state.py @@ -146,6 +146,14 @@ class State: self.conn.commit() return cursor.rowcount > 0 + def delete_queue_items_by_state(self, source_type: str, state: str, reason: str | None = None) -> int: + if reason is None: + cursor = self.conn.execute("delete from import_queue_items where source_type = ? and state = ?", (source_type, state)) + else: + cursor = self.conn.execute("delete from import_queue_items where source_type = ? and state = ? and reason = ?", (source_type, state, reason)) + self.conn.commit() + return cursor.rowcount + def get_queue_item(self, item_id: int) -> dict[str, Any] | None: row = self.conn.execute("select * from import_queue_items where id = ?", (item_id,)).fetchone() return dict(row) if row else None diff --git a/tests/test_state.py b/tests/test_state.py new file mode 100644 index 0000000..3e13e62 --- /dev/null +++ b/tests/test_state.py @@ -0,0 +1,13 @@ +from importarr.state import State + + +def test_delete_queue_items_by_state_can_target_reason(tmp_path): + state = State(tmp_path / "state.db") + state.upsert_queue_item(source_type="sab", source_id="stale", name="Stale", state="ignored", reason="SAB category is not owned by Importarr") + state.upsert_queue_item(source_type="sab", source_id="other", name="Other", state="ignored", reason="other reason") + + assert state.delete_queue_items_by_state("sab", "ignored", "SAB category is not owned by Importarr") == 1 + + rows = state.list_queue_items() + assert len(rows) == 1 + assert rows[0]["source_id"] == "other"