diff --git a/README.md b/README.md index 6387185..88e195f 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,8 @@ Run a test with one archive first: curl.exe -X POST -H "X-Service-Token: change-me" "http://localhost:8088/sync/backfill?max_archives=1" ``` +When `SYNC_SUCCESS_ONLY=true`, `max_archives` counts import attempts. Archives that are still `printing` or already failed are skipped and do not consume the limit. + Run full backfill for successful Bambuddy archives: ```powershell diff --git a/src/bambuddy_inventree_sync/sync.py b/src/bambuddy_inventree_sync/sync.py index 06f6309..fdf293d 100644 --- a/src/bambuddy_inventree_sync/sync.py +++ b/src/bambuddy_inventree_sync/sync.py @@ -70,8 +70,7 @@ class ArchiveSyncService: async def backfill(self, *, status: str | None = None, max_archives: int | None = None) -> dict[str, int]: status_filter = status - if status_filter is None and self.settings.sync_success_only: - status_filter = "success" + target_status = status.lower() if status else None counts = {"seen": 0, "synced": 0, "already_synced": 0, "skipped": 0, "failed": 0} offset = 0 @@ -83,7 +82,10 @@ class ArchiveSyncService: break for archive in archives: - if max_archives is not None and counts["seen"] >= max_archives: + if target_status and (archive.status or "").lower() != target_status: + continue + + if max_archives is not None and self._backfill_limit_reached(counts, max_archives): return counts counts["seen"] += 1 @@ -94,6 +96,9 @@ class ArchiveSyncService: logger.exception("Failed to sync Bambuddy archive %s during backfill", archive.id) counts["failed"] += 1 + if max_archives is not None and self._backfill_limit_reached(counts, max_archives): + return counts + offset += len(archives) if total is not None and offset >= total: break @@ -274,3 +279,7 @@ class ArchiveSyncService: if status in {"success", "completed", "complete", "done"}: return True return bool(archive.completed_at and status not in {"failed", "stopped", "printing", "running"}) + + @staticmethod + def _backfill_limit_reached(counts: dict[str, int], max_archives: int) -> bool: + return counts["synced"] + counts["already_synced"] + counts["failed"] >= max_archives