Skip to content

Commit c87f525

Browse files
committed
fixup: refactors
1 parent 48a8868 commit c87f525

3 files changed

Lines changed: 34 additions & 31 deletions

File tree

docs/admin/reference/settings.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,8 @@ Defaults to `False`.
295295

296296
### DISTRIBUTED\_PUBLICATION\_RETENTION\_PERIOD
297297

298-
When a distribution switches to a newer publication, the previously served (superseded) publication
299-
continues to be available for this many seconds.
300-
This grace period prevents 404 errors for clients that began downloading from the old publication
301-
before the switch occurred.
298+
When a distribution switches to a newer publication, the previously served (superseded) publication continues to be available for this many seconds.
299+
This grace period prevents 404 errors for clients that began downloading from the old publication before the switch occurred.
302300

303301
Set to `0` to disable the grace period and serve only the latest publication immediately.
304302

pulpcore/app/models/publication.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -756,9 +756,8 @@ def get_fallback_ca(self, path):
756756
if not retain_distributed_pub_enabled():
757757
return None
758758
recent_dp = (
759-
self.core_distributedpublications.filter(
760-
models.Q(expires_at__gte=timezone.now()) | models.Q(expires_at__isnull=True)
761-
)
759+
DistributedPublication.get_non_expired()
760+
.filter(distribution=self)
762761
.order_by("-pulp_created")
763762
.select_related("publication__repository_version")
764763
)
@@ -881,11 +880,8 @@ class DistributedPublication(BaseModel):
881880
"""
882881
Records the history of publications served by each Distribution.
883882
884-
Keeps superseded publications alive and serveable for a configurable grace period
885-
so clients mid-download don't receive 404 errors when a distribution switches publications.
886-
887-
- ``expires_at=null`` — currently active
888-
- ``expires_at=<datetime>`` — superseded; still served until that time
883+
Keeps superseded publications alive and serveable for a configurable grace period.
884+
Null `expired_at` means the publication was not superseded yet.
889885
"""
890886

891887
distribution = models.ForeignKey(
@@ -897,21 +893,21 @@ class DistributedPublication(BaseModel):
897893
expires_at = models.DateTimeField(null=True)
898894

899895
@classmethod
900-
def get_active(cls, distribution):
901-
"""Return records that are still being served (current or within grace period)."""
902-
return cls.objects.filter(distribution=distribution).filter(
903-
models.Q(expires_at__isnull=True) | models.Q(expires_at__gte=timezone.now())
904-
)
896+
def get_non_expired(cls, include_current=True):
897+
if include_current:
898+
return cls.objects.filter(
899+
models.Q(expires_at__isnull=True) | models.Q(expires_at__gte=timezone.now())
900+
)
901+
return cls.objects.filter(expires_at__gte=timezone.now())
905902

906903
@classmethod
907-
def get_expired(cls, distribution):
908-
"""Return records whose grace period has elapsed for a distribution."""
909-
return cls.objects.filter(distribution=distribution, expires_at__lt=timezone.now())
904+
def get_expired(cls):
905+
return cls.objects.filter(expires_at__lt=timezone.now())
910906

911907
@hook(AFTER_CREATE)
912908
def cleanup(self):
913909
"""Expire older active DistributedPublications; delete already-expired ones."""
914-
DistributedPublication.objects.filter(expires_at__lt=timezone.now()).delete()
910+
DistributedPublication.get_expired().delete()
915911
superseded = DistributedPublication.objects.exclude(pk=self.pk).filter(
916912
distribution=self.distribution, expires_at__isnull=True
917913
)

pulpcore/tests/unit/models/test_publication_retention.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,22 @@ class TestDistributedPublication:
9090
def test_created_when_publication_added_to_distribution(self, db):
9191
pub = pub_factory()
9292
dist = dist_factory(pub=pub)
93-
active = DistributedPublication.get_active(dist)
94-
assert active.count() == 1
95-
assert active.first().publication_id == pub.pk
96-
assert active.first().expires_at is None
93+
non_expired = DistributedPublication.get_non_expired(include_current=True).filter(
94+
distribution=dist
95+
)
96+
assert non_expired.count() == 1
97+
assert non_expired.first().publication_id == pub.pk
98+
assert non_expired.first().expires_at is None
9799

98100
def test_first_distributed_publication_is_active(self, db):
99101
dist = dist_factory(pub=pub_factory())
100-
assert DistributedPublication.get_active(dist).count() == 1
101-
assert DistributedPublication.get_expired(dist).count() == 0
102+
assert (
103+
DistributedPublication.get_non_expired(include_current=True)
104+
.filter(distribution=dist)
105+
.count()
106+
== 1
107+
)
108+
assert DistributedPublication.get_expired().filter(distribution=dist).count() == 0
102109

103110
def test_switching_publication_expires_old_and_activates_new(self, db):
104111
pub1 = pub_factory()
@@ -107,10 +114,12 @@ def test_switching_publication_expires_old_and_activates_new(self, db):
107114
pub2 = pub_factory()
108115
update_dist(dist, pub=pub2)
109116

110-
active = DistributedPublication.get_active(dist)
111-
assert active.count() == 2
112-
assert active.filter(expires_at__isnull=True).first().publication_id == pub2.pk
113-
assert active.filter(expires_at__isnull=False).first().publication_id == pub1.pk
117+
non_expired = DistributedPublication.get_non_expired(include_current=True).filter(
118+
distribution=dist
119+
)
120+
assert non_expired.count() == 2
121+
assert non_expired.filter(expires_at__isnull=True).first().publication_id == pub2.pk
122+
assert non_expired.filter(expires_at__isnull=False).first().publication_id == pub1.pk
114123

115124

116125
@pytest.mark.django_db

0 commit comments

Comments
 (0)