From af439dda324e5da0a3bf84c6dc1fcaf155c6deee Mon Sep 17 00:00:00 2001 From: Nico Athanassiadis Date: Sun, 9 Feb 2025 15:45:04 +0100 Subject: [PATCH] Ensure that jobs are enqueued after application restart. Jobs that are in state of PROCESSING or PENDING will be enqueued after application restart so users that have had an uploaded file in PROCESSING or PENDING do not need to manually re-upload the files again. --- .../seshat/services/JobProcessorService.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/se/su/dsv/seshat/services/JobProcessorService.java b/src/main/java/se/su/dsv/seshat/services/JobProcessorService.java index 35a68be..3472315 100644 --- a/src/main/java/se/su/dsv/seshat/services/JobProcessorService.java +++ b/src/main/java/se/su/dsv/seshat/services/JobProcessorService.java @@ -15,6 +15,7 @@ import se.su.dsv.seshat.repositories.FileMetadataRepository; import java.io.File; import java.time.LocalDateTime; +import java.util.Comparator; import java.util.List; import java.util.Objects; import java.util.concurrent.BlockingQueue; @@ -62,12 +63,21 @@ public class JobProcessorService { worker.start(); } + /** + * Add all jobs that have been in a processing status or pending status + * to the queue when the application starts. + * Ensuring that all jobs are processed even if the application is restarted + * wihtout the user having to re-upload the files. + */ @Transactional public void addPendingJobsToQueue() { - List pendingJobs = fileMetadataRepository.findByJobStatus(JobStatus.PENDING); - for (FileMetadata job : pendingJobs) { - jobQueue.offer(job); - } + List processingJobs = fileMetadataRepository.findByJobStatus(JobStatus.PROCESSING); + processingJobs.addAll(fileMetadataRepository.findByJobStatus(JobStatus.PENDING)); + + processingJobs.sort(Comparator.comparing(FileMetadata::getUploadedAt)); + + processingJobs.forEach(this::addJob); + } @Transactional -- 2.39.5