package uk.ac.ebi.subs.api.sheetloader;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@Component
/**
 * The batch loading process leaves processed batches in the db
 * We need them in the short term, so the UI can look up progress, but we don't need them in the long term.
 *
 * Every few hours, we can remove old completed batches from the db.
 *
 * At the moment, it's every 4 hours, remove completed records over a week old
 */
public class SheetCleanupTask {

    private static final long FOUR_HOUR_IN_MILLIS = 1000l * 60 * 60 * 4;

    private SheetCleanupRepository sheetCleanupRepository;


    @Scheduled(fixedDelay = FOUR_HOUR_IN_MILLIS, initialDelay = FOUR_HOUR_IN_MILLIS)
    public void cleanUpOldSubmittableBatches(){

        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.DAY_OF_MONTH, -7);
        Date sevenDaysAgo = cal.getTime();

        sheetCleanupRepository.removeByLastModifiedDateBeforeAndStatus(
                sevenDaysAgo,
                "Completed"
        );

    }
}

@@ -34,16 +34,78 @@
 oader;%0A%0A
+import lombok.NonNull;%0Aimport lombok.RequiredArgsConstructor;%0A
 import o
@@ -156,16 +156,16 @@
 eduled;%0A
-
 import o
@@ -297,19 +297,9 @@
 r;%0A%0A
-@Component
 %0A
+
 /**%0A
@@ -628,16 +628,52 @@
 old%0A */%0A
+@Component%0A@RequiredArgsConstructor%0A
 public c
@@ -771,16 +771,29 @@
 0 * 4;%0A%0A
+    @NonNull%0A
     priv

add dependency for cleanup task