from datetime import timedelta from django.conf import settings from django.db import migrations def backfill_next_fetch_at(apps, schema_editor): """One-time backfill for feeds that existed before next_fetch_at (C1) — everything created after this migration gets it set by fetch_feed itself. Mirrors the exact formula apps.feeds.services.fetcher. effective_interval_minutes used to compute on every poll tick, just run once here instead of forever after. """ Feed = apps.get_model("feeds", "Feed") SiteSettings = apps.get_model("core", "SiteSettings") configured_interval = 60 settings_row = SiteSettings.objects.filter(pk=1).first() if settings_row is not None: configured_interval = settings_row.fetch_interval_minutes floor = getattr(settings, "EVECTION_MIN_FETCH_INTERVAL_MINUTES", 15) base = max(configured_interval, floor) max_backoff = 24 * 60 for feed in Feed.objects.filter(last_fetched_at__isnull=False).only( "id", "last_fetched_at", "fetch_error_count" ): if feed.fetch_error_count <= 0: interval = base else: interval = min(base * (2**feed.fetch_error_count), max_backoff) feed.next_fetch_at = feed.last_fetched_at + timedelta(minutes=interval) feed.save(update_fields=["next_fetch_at"]) def noop_reverse(apps, schema_editor): pass class Migration(migrations.Migration): dependencies = [ ("feeds", "0005_feed_next_fetch_at_and_more"), ("core", "0001_initial"), ] operations = [ migrations.RunPython(backfill_next_fetch_at, noop_reverse), ]