Skip to content

Commit 771ca84

Browse files
authored
fix(versions): convert version removal celery task to migration DEV-1227 (#6442)
### 💭 Notes Backport of #6434 Converts the remove_all_versions celery task (which accidentally had a time limit of 30 seconds) to a long-running migration to be in keeping with the rest of the long-running works. ### 👀 Preview steps 1. Run migrations 2. If empty, populate the Version table with ``` for i in range(100): Revision.objects.create(date_created=timezone.now()) Version.objects.create(revision_id=i, content_type_id=21) ``` 3. Update `VERSION_DELETION_BATCH_SIZE` to 5 4. Restart the kpi_worker 5. Wait for the long-running migrations job to run 6. 🟢 The number of objects in the version table is decreased 7. 🟢 There are logs in the kpi_worker showing how many versions are deleted with each run
1 parent 45612ef commit 771ca84

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import time
2+
3+
from django.conf import settings
4+
from django.db.models import Min
5+
from reversion.models import Version
6+
7+
from kpi.utils.log import logging
8+
9+
10+
def run():
11+
while min_id := Version.objects.aggregate(Min('pk'))['pk__min']:
12+
queryset = Version.objects.filter(
13+
pk__lt=min_id + settings.VERSION_DELETION_BATCH_SIZE
14+
).only('pk')
15+
deleted = queryset.delete()
16+
# log at debug level so we don't flood the logs
17+
logging.debug(f'Deleted {deleted[0]} version objects with pk < {min_id}')
18+
time.sleep(10)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from django.db import migrations
2+
3+
4+
def add_long_running_migration(apps, schema_editor):
5+
LongRunningMigration = apps.get_model('long_running_migrations', 'LongRunningMigration') # noqa
6+
LongRunningMigration.objects.create(
7+
name='0012_remove_old_versions'
8+
)
9+
10+
11+
def noop(*args, **kwargs):
12+
pass
13+
14+
15+
class Migration(migrations.Migration):
16+
17+
dependencies = [
18+
('long_running_migrations', '0011_backfill_exceeded_limit_counters'),
19+
]
20+
21+
operations = [
22+
migrations.RunPython(add_long_running_migration, noop),
23+
]

kpi/tasks.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from django.core import mail
77
from django.core.exceptions import ObjectDoesNotExist
88
from django.core.management import call_command
9-
from django.db.models import Min
10-
from reversion.models import Version
119

1210
from kobo.apps.kobo_auth.shortcuts import User
1311
from kobo.apps.markdownx_uploader.tasks import remove_unused_markdown_files
@@ -16,7 +14,6 @@
1614
from kpi.maintenance_tasks import remove_old_asset_snapshots, remove_old_import_tasks
1715
from kpi.models.asset import Asset
1816
from kpi.models.import_export_task import ImportTask, SubmissionExportTask
19-
from kpi.utils.log import logging
2017

2118

2219
@celery_app.task(
@@ -128,15 +125,3 @@ def perform_maintenance():
128125
remove_unused_markdown_files()
129126
remove_old_import_tasks()
130127
remove_old_asset_snapshots()
131-
132-
133-
@celery_app.task(time_limit=30, soft_time_limit=30)
134-
def remove_old_versions():
135-
while min_id := Version.objects.aggregate(Min('pk'))['pk__min']:
136-
queryset = Version.objects.filter(
137-
pk__lt=min_id + settings.VERSION_DELETION_BATCH_SIZE
138-
).only('pk')
139-
deleted = queryset.delete()
140-
# log at debug level so we don't flood the logs
141-
logging.debug(f'Deleted {deleted[0]} version objects with pk < {min_id}')
142-
time.sleep(10)

0 commit comments

Comments
 (0)