Skip to content

Commit

Permalink
Update after merging #8721
Browse files Browse the repository at this point in the history
  • Loading branch information
Marishka17 committed Dec 20, 2024
1 parent 8fec24a commit 81ec1fc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 46 deletions.
11 changes: 5 additions & 6 deletions cvat/apps/engine/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
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 @@ -330,7 +331,7 @@ def handle_local_download() -> Response:
acquire_timeout=LOCK_ACQUIRE_TIMEOUT,
):
if osp.exists(file_path) and not is_result_outdated():
dm.util.extend_export_file_lifetime(file_path)
extend_export_file_lifetime(file_path)

return Response(status=status.HTTP_201_CREATED)

Expand Down Expand Up @@ -611,8 +612,7 @@ def is_result_outdated() -> bool:
)

if action == "download":
# TODO: update after 8721
with dm.util.get_export_cache_lock(file_path, ttl=55, acquire_timeout=50):
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 @@ -630,10 +630,9 @@ def is_result_outdated() -> bool:
return sendfile(
self.request, file_path, attachment=True, attachment_filename=filename
)
# TODO: update after 8721
with dm.util.get_export_cache_lock(file_path, ttl=55, acquire_timeout=50):
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)
extend_export_file_lifetime(file_path)
return Response(status=status.HTTP_201_CREATED)

cancel_and_delete(rq_job)
Expand Down
28 changes: 7 additions & 21 deletions cvat/apps/engine/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
from cvat.apps.engine.permissions import get_cloud_storage_for_import_or_export
from cvat.apps.dataset_manager.views import log_exception
from cvat.apps.dataset_manager.bindings import CvatImportError
from cvat.apps.dataset_manager.views import EXPORT_CACHE_LOCK_TTL, EXPORT_CACHE_LOCK_ACQUISITION_TIMEOUT
from cvat.apps.dataset_manager.util import extend_export_file_lifetime

slogger = ServerLogManager(__name__)

Expand Down Expand Up @@ -1042,14 +1044,12 @@ def create_backup(
with get_export_cache_lock(
output_path,
block=True,
# TODO: update after merging #8721 (DATASET_CACHE_LOCK_ACQUISITION_TIMEOUT, DATASET_EXPORT_LOCK_TTL)
acquire_timeout=60,
ttl=30,
acquire_timeout=EXPORT_CACHE_LOCK_ACQUISITION_TIMEOUT,
ttl=EXPORT_CACHE_LOCK_TTL,
):
# output_path includes timestamp of the last update
if os.path.exists(output_path):
# TODO: update after merging #8721
# extend_export_file_lifetime(output_path)
extend_export_file_lifetime(output_path)
return output_path

with tempfile.TemporaryDirectory(dir=cache_dir) as temp_dir:
Expand All @@ -1060,9 +1060,8 @@ def create_backup(
with get_export_cache_lock(
output_path,
block=True,
# TODO: update after merging #8721 (DATASET_CACHE_LOCK_ACQUISITION_TIMEOUT, DATASET_EXPORT_LOCK_TTL)
acquire_timeout=60,
ttl=30,
acquire_timeout=EXPORT_CACHE_LOCK_ACQUISITION_TIMEOUT,
ttl=EXPORT_CACHE_LOCK_TTL,
):
os.replace(temp_file, output_path)

Expand Down Expand Up @@ -1228,16 +1227,3 @@ def import_task(request, queue_name, filename=None):
location_conf=location_conf,
filename=filename
)

# TODO: delete function
def _clear_export_cache(file_path: str, file_ctime: float, logger: Logger) -> None:
try:
if os.path.exists(file_path) and os.path.getctime(file_path) == file_ctime:
os.remove(file_path)

logger.info(
"Export cache file '{}' successfully removed" \
.format(file_path))
except Exception:
log_exception(logger)
raise
39 changes: 20 additions & 19 deletions cvat/apps/engine/tests/test_rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3088,31 +3088,32 @@ def test_api_v2_tasks_id_export_somebody(self):
def test_api_v2_tasks_id_export_no_auth(self):
self._run_api_v2_tasks_id_export_import(None)

def test_can_remove_export_cache_automatically_after_successful_export(self):
self._create_tasks()
task_id = self.tasks[0]["id"]
user = self.admin
# TODO: add another test that checks running cron job
# def test_can_remove_export_cache_automatically_after_successful_export(self):
# self._create_tasks()
# task_id = self.tasks[0]["id"]
# user = self.admin

with mock.patch('cvat.apps.dataset_manager.views.TASK_CACHE_TTL', new=timedelta(hours=10)):
response = self._run_api_v2_tasks_id_export(task_id, user)
self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
# with mock.patch('cvat.apps.dataset_manager.views.TASK_CACHE_TTL', new=timedelta(hours=10)):
# response = self._run_api_v2_tasks_id_export(task_id, user)
# self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)

response = self._run_api_v2_tasks_id_export(task_id, user)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
# response = self._run_api_v2_tasks_id_export(task_id, user)
# self.assertEqual(response.status_code, status.HTTP_201_CREATED)

scheduler = django_rq.get_scheduler(settings.CVAT_QUEUES.IMPORT_DATA.value)
scheduled_jobs = list(scheduler.get_jobs())
cleanup_job = next(
j for j in scheduled_jobs if j.func_name.endswith('.engine.backup._clear_export_cache')
)
# scheduler = django_rq.get_scheduler(settings.CVAT_QUEUES.IMPORT_DATA.value)
# scheduled_jobs = list(scheduler.get_jobs())
# cleanup_job = next(
# j for j in scheduled_jobs if j.func_name.endswith('.engine.backup._clear_export_cache')
# )

export_path = cleanup_job.kwargs['file_path']
self.assertTrue(os.path.isfile(export_path))
# export_path = cleanup_job.kwargs['file_path']
# self.assertTrue(os.path.isfile(export_path))

from cvat.apps.engine.backup import _clear_export_cache
_clear_export_cache(**cleanup_job.kwargs)
# from cvat.apps.engine.backup import _clear_export_cache
# _clear_export_cache(**cleanup_job.kwargs)

self.assertFalse(os.path.isfile(export_path))
# self.assertFalse(os.path.isfile(export_path))


def generate_random_image_file(filename):
Expand Down

0 comments on commit 81ec1fc

Please sign in to comment.