Skip to content

Commit 86b7809

Browse files
committed
Add a premigration hook to clean stale Status records
1 parent 1a37427 commit 86b7809

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

pulpcore/app/apps.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from django.conf import settings
99
from django.core.exceptions import ImproperlyConfigured
1010
from django.db import connection, transaction
11-
from django.db.models.signals import post_migrate
11+
from django.db.models.signals import post_migrate, pre_migrate
1212
from django.utils.module_loading import module_has_submodule
1313

1414
from pulpcore.exceptions.plugin import MissingPlugin
@@ -250,6 +250,7 @@ def ready(self):
250250
super().ready()
251251
from . import checks # noqa
252252

253+
pre_migrate.connect(_clean_app_status, sender=self, dispatch_uid="clean_app_status")
253254
post_migrate.connect(
254255
_ensure_default_domain, sender=self, dispatch_uid="ensure_default_domain"
255256
)
@@ -263,6 +264,19 @@ def ready(self):
263264
)
264265

265266

267+
def _clean_app_status(sender, apps, verbosity, **kwargs):
268+
from django.contrib.postgres.functions import TransactionNow
269+
from django.db.models import F
270+
271+
try:
272+
AppStatus = apps.get_model("core", "AppStatus")
273+
except LookupError:
274+
if verbosity >= 1:
275+
print(_("AppStatus model does not exist. Skipping pre migrate cleanup."))
276+
else:
277+
AppStatus.objects.filter(last_heartbeat__lt=TransactionNow() - F("ttl")).delete()
278+
279+
266280
def _populate_access_policies(sender, apps, verbosity, **kwargs):
267281
from pulpcore.app.util import get_view_urlpattern
268282
from pulpcore.app.viewsets import LoginViewSet

0 commit comments

Comments
 (0)