Skip to content

Commit

Permalink
[Unit tests] Check that cron job deletes files from cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Marishka17 committed Dec 20, 2024
1 parent 0a6dbdc commit 529b5c6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
46 changes: 46 additions & 0 deletions cvat/apps/dataset_manager/tests/test_rest_api_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,52 @@ def test_cleanup_can_defer_removal_if_file_is_used_recently(self):

self.assertTrue(osp.isfile(export_path))

def test_cleanup_cron_job_can_delete_cached_files(self):
from cvat.apps.dataset_manager.views import cron_export_cache_cleanup

def _get_project_task_job_ids():
project = self._create_project(projects["main"])
project_id = project["id"]

images = self._generate_task_images(3)
task = self._create_task(
data=tasks["task in project #1"],
image_data=images,
)
task_id = task["id"]
job_id = self._get_jobs(task_id)[0]["id"]
return project_id, task_id, job_id

# remove chunks from the cache
self._clear_temp_data()
project_id, task_id, job_id = _get_project_task_job_ids()

for resource, rid in zip(("project", "task", "job"), (project_id, task_id, job_id)):
for save_images in (True, False):
export_path = export(
dst_format="CVAT for images 1.1",
save_images=save_images,
**{resource + "_id": rid},
)
self.assertTrue(osp.isfile(export_path))
self.assertTrue(resource in export_path)

with (
patch(
"cvat.apps.dataset_manager.views.TTL_CONSTS",
new={resource: timedelta(seconds=0)},
),
patch(
"cvat.apps.dataset_manager.views.clear_export_cache",
side_effect=clear_export_cache,
) as mock_clear_export_cache,
):
cron_export_cache_cleanup(f"cvat.apps.engine.models.{resource.title()}")
mock_clear_export_cache.assert_called_once()

self.assertFalse(osp.exists(export_path))


class ProjectDumpUpload(_DbTestBase):
def _get_download_project_dataset_response(self, url, user, dump_format_name, edata):
data = {
Expand Down
10 changes: 7 additions & 3 deletions cvat/apps/engine/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from rq.job import JobStatus as RQJobStatus

import cvat.apps.dataset_manager as dm
from cvat.apps.dataset_manager.util import extend_export_file_lifetime
from cvat.apps.engine import models
from cvat.apps.engine.backup import ProjectExporter, TaskExporter, create_backup
from cvat.apps.engine.cloud_provider import export_resource_to_cloud_storage
Expand All @@ -49,7 +50,6 @@
sendfile,
)
from cvat.apps.events.handlers import handle_dataset_export
from cvat.apps.dataset_manager.util import extend_export_file_lifetime

slogger = ServerLogManager(__name__)

Expand Down Expand Up @@ -610,7 +610,9 @@ def is_result_outdated() -> bool:
)

if action == "download":
with dm.util.get_export_cache_lock(file_path, ttl=LOCK_TTL, acquire_timeout=LOCK_ACQUIRE_TIMEOUT):
with dm.util.get_export_cache_lock(
file_path, ttl=LOCK_TTL, acquire_timeout=LOCK_ACQUIRE_TIMEOUT
):
if not os.path.exists(file_path):
return Response(
"The backup file has been expired, please retry backing up",
Expand All @@ -628,7 +630,9 @@ def is_result_outdated() -> bool:
return sendfile(
self.request, file_path, attachment=True, attachment_filename=filename
)
with dm.util.get_export_cache_lock(file_path, ttl=LOCK_TTL, acquire_timeout=LOCK_ACQUIRE_TIMEOUT):
with dm.util.get_export_cache_lock(
file_path, ttl=LOCK_TTL, acquire_timeout=LOCK_ACQUIRE_TIMEOUT
):
if osp.exists(file_path) and not is_result_outdated():
extend_export_file_lifetime(file_path)
return Response(status=status.HTTP_201_CREATED)
Expand Down
2 changes: 0 additions & 2 deletions cvat/apps/engine/tests/test_rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# SPDX-License-Identifier: MIT

from contextlib import ExitStack
from datetime import timedelta
import io
from itertools import product
import os
Expand All @@ -25,7 +24,6 @@
import json

import av
import django_rq
import numpy as np
from pdf2image import convert_from_bytes
from pyunpack import Archive
Expand Down

0 comments on commit 529b5c6

Please sign in to comment.