Ensure that jobs are enqueued after application restart. #20

Merged
niat8586 merged 1 commits from ensure-requeuing-jobs-on-restart into develop 2025-02-09 15:50:34 +01:00

View File

@ -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<FileMetadata> pendingJobs = fileMetadataRepository.findByJobStatus(JobStatus.PENDING);
for (FileMetadata job : pendingJobs) {
jobQueue.offer(job);
}
List<FileMetadata> processingJobs = fileMetadataRepository.findByJobStatus(JobStatus.PROCESSING);
processingJobs.addAll(fileMetadataRepository.findByJobStatus(JobStatus.PENDING));
processingJobs.sort(Comparator.comparing(FileMetadata::getUploadedAt));
processingJobs.forEach(this::addJob);
}
@Transactional