export_import: clean old jobs (#87614)
gitea/combo/pipeline/head This commit looks good Details

This commit is contained in:
Lauréline Guérin 2024-03-07 10:03:32 +01:00
parent d128ae63be
commit 9480ed89dc
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
3 changed files with 42 additions and 1 deletions

View File

@ -26,3 +26,8 @@ class AppConfig(django.apps.AppConfig):
from . import urls
return urls.urlpatterns
def hourly(self):
from combo.apps.export_import.models import ApplicationAsyncJob
ApplicationAsyncJob.clean_jobs()

View File

@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import collections
import datetime
import io
import json
import sys
@ -281,3 +282,10 @@ class ApplicationAsyncJob(models.Model):
'total_count': self.total_count,
'percent': int(current_count * 100 / self.total_count),
}
@classmethod
def clean_jobs(cls):
# remove jobs after 7 days
for job in cls.objects.filter(last_update_timestamp__lte=now() - datetime.timedelta(days=7)):
job.bundle.delete(save=False)
job.delete()

View File

@ -1,15 +1,20 @@
import datetime
import io
import json
import os
import re
import tarfile
import uuid
from unittest import mock
import pytest
from django.apps import apps
from django.contrib.auth.models import Group
from django.contrib.contenttypes.models import ContentType
from django.core.files import File
from django.core.files.storage import default_storage
from combo.apps.export_import.models import Application, ApplicationElement
from combo.apps.export_import.models import Application, ApplicationAsyncJob, ApplicationElement
from combo.apps.wcs.models import WcsCardCell, WcsCategoryCell, WcsFormCell
from combo.data.models import LinkCell, LinkListCell, Page, PageSnapshot, TextCell
@ -681,3 +686,26 @@ def test_page_dependencies_category_cell(mock_send):
'redirect': 'http://127.0.0.1:8999/api/export-import/forms-xategories/test-3/redirect/',
},
} not in page.get_dependencies()
def test_hourly(freezer):
job = ApplicationAsyncJob.objects.create(action='foo', bundle=File(io.BytesIO(b'test'), 'test.tar'))
ApplicationAsyncJob.objects.create(
action='foo',
)
bundle_path = os.path.join(default_storage.path(''), job.bundle.path)
assert os.path.exists(bundle_path)
appconfig = apps.get_app_config('export_import')
appconfig.hourly()
assert ApplicationAsyncJob.objects.count() == 2
assert os.path.exists(bundle_path) is True
freezer.move_to(datetime.timedelta(days=7))
appconfig = apps.get_app_config('export_import')
appconfig.hourly()
assert ApplicationAsyncJob.objects.count() == 0
assert os.path.exists(bundle_path) is False