Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(cloudinary): end the usage sync cleanly when today is refused (#126956)

Tom Owers307da79e1de9
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudinary/cloudinary.py b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudinary/cloudinary.py
index 028d1ea4672f..66ca3fe99d41 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudinary/cloudinary.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudinary/cloudinary.py
@@ -5,6 +5,8 @@ from datetime import UTC, date, datetime, timedelta
from typing import Any, Optional
from urllib.parse import quote
+from requests.exceptions import HTTPError
+
from products.warehouse_sources.backend.temporal.data_imports.sources.cloudinary.settings import (
CLOUDINARY_ENDPOINTS,
LAST_ACCESS_REPORTS_PATH,
@@ -111,15 +113,22 @@ def iter_usage(
resumed_day = _as_date(resume.cursor) if resume else None
day = max(resumed_day, start) if resumed_day else start
while day <= end:
- for page in client.paginate(
- path=CLOUDINARY_ENDPOINTS["usage"].path.format(date=day.isoformat()),
- paginator=SinglePagePaginator(),
- data_selector="$",
- hooks=_RETRY_HOOKS,
- ):
- rows = [{**row, "date": day.isoformat()} for row in page]
- manager.save_state(CloudinaryResumeConfig(cursor=(day + timedelta(days=1)).isoformat()))
- yield rows
+ try:
+ for page in client.paginate(
+ path=CLOUDINARY_ENDPOINTS["usage"].path.format(date=day.isoformat()),
+ paginator=SinglePagePaginator(),
+ data_selector="$",
+ hooks=_RETRY_HOOKS,
+ ):
+ rows = [{**row, "date": day.isoformat()} for row in page]
+ manager.save_state(CloudinaryResumeConfig(cursor=(day + timedelta(days=1)).isoformat()))
+ yield rows
+ except HTTPError as e:
+ # Cloudinary can refuse the current UTC date with a 400 before its own usage day has
+ # started. The day is not lost, because the next run re-reads the restatement window.
+ if day == end and e.response is not None and e.response.status_code == 400:
+ return
+ raise
day += timedelta(days=1)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudinary/tests/test_cloudinary.py b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudinary/tests/test_cloudinary.py
index fffa171b6fbd..5491d9e9a156 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudinary/tests/test_cloudinary.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudinary/tests/test_cloudinary.py
@@ -207,6 +207,29 @@ class TestCloudinaryUsage:
CloudinaryResumeConfig(cursor="2026-10-19"),
]
+ @pytest.mark.parametrize(
+ "statuses,expected_days",
+ [
+ ([200, 400], ["2026-10-17"]),
+ ([400, 200], None),
+ ],
+ )
+ @time_machine.travel("2026-10-18T02:00:00Z", tick=False)
+ def test_only_a_refused_current_day_ends_the_sync_cleanly(
+ self, statuses: list[int], expected_days: list[str] | None
+ ) -> None:
+ result = _driver().run(
+ "usage",
+ [ScriptedResponse(status=status, json={"plan": "Plus"}) for status in statuses],
+ resume_state=CloudinaryResumeConfig(cursor="2026-10-17"),
+ )
+
+ if expected_days is None:
+ assert result.raised is not None
+ else:
+ assert result.raised is None
+ assert [row["date"] for row in result.rows] == expected_days
+
class TestCloudinaryCredentials:
@pytest.mark.parametrize(