Skip to content

Commit

Permalink
fixup! first implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kernicPanel committed Oct 17, 2024
1 parent 8c2e002 commit ca863f7
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Management command to delete transcoding temp files."""

import logging

from django.core.management import BaseCommand

from marsha.core.utils.transcode import delete_transcoding_temp_files


logger = logging.getLogger(__name__)


class Command(BaseCommand):
"""Delete transcoding temp files."""

help = "Delete transcoding temp files"

def handle(self, *args, **options):
"""Delete all transcoding temp files."""
delete_transcoding_temp_files()
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Test transcript_video command."""

from unittest.mock import patch

from django.core.management import call_command
from django.test import TestCase

from marsha.core.utils import transcode


@patch.object(transcode, "delete_transcoding_temp_files")
class TranscriptVideosTestCase(TestCase):
"""
Test case for the transcript_videos command.
"""

maxDiff = None

def test_delete_transcoding_temp_files(self, mock_delete_temp_files):
"""Should call delete_transcoding_temp_files function."""
call_command("delete_transcoding_temp_files")

mock_delete_temp_files.assert_called_once()
62 changes: 61 additions & 1 deletion src/backend/marsha/core/tests/utils/test_transcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

from django.test import TestCase

from django_peertube_runner_connector.factories import (
VideoFactory as TranscodedVideoFactory,
VideoJobInfoFactory,
)
from django_peertube_runner_connector.models import (
Video as TranscodedVideo,
VideoFile,
Expand All @@ -14,7 +18,10 @@
from marsha.core import defaults
from marsha.core.factories import VideoFactory
from marsha.core.utils.time_utils import to_datetime
from marsha.core.utils.transcode import transcoding_ended_callback
from marsha.core.utils.transcode import (
delete_transcoding_temp_files,
transcoding_ended_callback,
)


class TranscodeTestCase(TestCase):
Expand Down Expand Up @@ -82,3 +89,56 @@ def test_transcoding_ended_callback_with_error(self, mock_delete_temp_file):
mock_delete_temp_file.assert_called_once_with(
self.transcoded_video, f"tmp/{self.video.pk}/video/1698941501"
)

@patch("marsha.core.utils.transcode.delete_temp_file")
def test_transcoding_delete_transcoding_temp_files(self, mock_delete_temp_file):
"""The temp files should be deleted."""
VideoJobInfoFactory(pendingTranscode=0, video=self.transcoded_video)
temp_video_file = TranscodedVideoFactory(
directory=self.transcoded_video.directory.replace(
defaults.VOD_VIDEOS_STORAGE_BASE_DIRECTORY,
defaults.TMP_VIDEOS_STORAGE_BASE_DIRECTORY,
)
)

delete_transcoding_temp_files()

mock_delete_temp_file.assert_called_once_with(
self.transcoded_video, temp_video_file.directory
)

@patch("marsha.core.utils.transcode.delete_temp_file")
def test_transcoding_delete_transcoding_temp_files_pending_transcode(
self, mock_delete_temp_file
):
"""The temp files should not be deleted if there is a pending transcode."""
VideoJobInfoFactory(pendingTranscode=1, video=self.transcoded_video)
TranscodedVideoFactory(
directory=self.transcoded_video.directory.replace(
defaults.VOD_VIDEOS_STORAGE_BASE_DIRECTORY,
defaults.TMP_VIDEOS_STORAGE_BASE_DIRECTORY,
)
)

delete_transcoding_temp_files()

mock_delete_temp_file.assert_not_called()

@patch("marsha.core.utils.transcode.delete_temp_file")
def test_transcoding_delete_transcoding_temp_files_not_published(
self, mock_delete_temp_file
):
"""The temp files should be deleted."""
self.transcoded_video.state = VideoState.TO_TRANSCODE
self.transcoded_video.save()
VideoJobInfoFactory(pendingTranscode=0, video=self.transcoded_video)
TranscodedVideoFactory(
directory=self.transcoded_video.directory.replace(
defaults.VOD_VIDEOS_STORAGE_BASE_DIRECTORY,
defaults.TMP_VIDEOS_STORAGE_BASE_DIRECTORY,
)
)

delete_transcoding_temp_files()

mock_delete_temp_file.assert_not_called()
12 changes: 12 additions & 0 deletions src/backend/marsha/core/utils/transcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ def transcoding_ended_callback(transcoded_video: TranscodedVideo):
to_datetime(uploaded_on),
**{"resolutions": resolutions},
)


def delete_transcoding_temp_files():
"""Delete all transcoding temp files."""
transcoded_videos = TranscodedVideo.objects.filter(
state=VideoState.PUBLISHED, jobInfo__pendingTranscode=0
)
for transcoded_video in transcoded_videos:
tmp_filename = transcoded_video.directory.replace(
VOD_VIDEOS_STORAGE_BASE_DIRECTORY, TMP_VIDEOS_STORAGE_BASE_DIRECTORY
)
delete_temp_file(transcoded_video, tmp_filename)

0 comments on commit ca863f7

Please sign in to comment.