From 3ec052b442173f475ed25cb25e4b18dd69932083 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Fri, 3 Feb 2023 22:31:47 +0100 Subject: [PATCH] Added scheduled task to remove old tasks. --- .../model/VideoProcessingTaskRepository.java | 5 +++-- .../service/VideoProcessingService.java | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/gymboard-cdn/src/main/java/nl/andrewlalis/gymboardcdn/model/VideoProcessingTaskRepository.java b/gymboard-cdn/src/main/java/nl/andrewlalis/gymboardcdn/model/VideoProcessingTaskRepository.java index f0361e2..6c997d6 100644 --- a/gymboard-cdn/src/main/java/nl/andrewlalis/gymboardcdn/model/VideoProcessingTaskRepository.java +++ b/gymboard-cdn/src/main/java/nl/andrewlalis/gymboardcdn/model/VideoProcessingTaskRepository.java @@ -3,6 +3,7 @@ package nl.andrewlalis.gymboardcdn.model; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; @@ -10,7 +11,7 @@ import java.util.Optional; public interface VideoProcessingTaskRepository extends JpaRepository { Optional findByVideoIdentifier(String identifier); - boolean existsByVideoIdentifier(String identifier); - List findAllByStatusOrderByCreatedAtDesc(VideoProcessingTask.Status status); + + List findAllByCreatedAtBefore(LocalDateTime cutoff); } diff --git a/gymboard-cdn/src/main/java/nl/andrewlalis/gymboardcdn/service/VideoProcessingService.java b/gymboard-cdn/src/main/java/nl/andrewlalis/gymboardcdn/service/VideoProcessingService.java index 66bddce..52b0553 100644 --- a/gymboard-cdn/src/main/java/nl/andrewlalis/gymboardcdn/service/VideoProcessingService.java +++ b/gymboard-cdn/src/main/java/nl/andrewlalis/gymboardcdn/service/VideoProcessingService.java @@ -14,6 +14,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; import java.time.Instant; +import java.time.LocalDateTime; import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; @@ -44,6 +45,27 @@ public class VideoProcessingService { } } + @Scheduled(fixedRate = 1, timeUnit = TimeUnit.HOURS) + public void removeOldTasks() { + LocalDateTime cutoff = LocalDateTime.now().minusHours(12); + List oldTasks = taskRepo.findAllByCreatedAtBefore(cutoff); + for (var task : oldTasks) { + if (task.getStatus() == VideoProcessingTask.Status.COMPLETED) { + log.info("Deleting completed task for video {}.", task.getVideoIdentifier()); + taskRepo.delete(task); + } else if (task.getStatus() == VideoProcessingTask.Status.FAILED) { + log.info("Deleting failed task for video {}.", task.getVideoIdentifier()); + taskRepo.delete(task); + } else if (task.getStatus() == VideoProcessingTask.Status.IN_PROGRESS) { + log.info("Task for video {} was in progress for too long; deleting.", task.getVideoIdentifier()); + taskRepo.delete(task); + } else if (task.getStatus() == VideoProcessingTask.Status.WAITING) { + log.info("Task for video {} was waiting for too long; deleting.", task.getVideoIdentifier()); + taskRepo.delete(task); + } + } + } + private void processVideo(VideoProcessingTask task) { log.info("Started processing video {}.", task.getVideoIdentifier());