diff --git a/api/src/main/java/ca/bc/gov/educ/api/dataconversion/config/BatchJobConfig.java b/api/src/main/java/ca/bc/gov/educ/api/dataconversion/config/BatchJobConfig.java index 626f21c4..f061ad96 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/dataconversion/config/BatchJobConfig.java +++ b/api/src/main/java/ca/bc/gov/educ/api/dataconversion/config/BatchJobConfig.java @@ -16,7 +16,6 @@ import org.springframework.batch.core.job.builder.JobBuilder; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.launch.support.RunIdIncrementer; -import org.springframework.batch.core.launch.support.SimpleJobLauncher; import org.springframework.batch.core.launch.support.TaskExecutorJobLauncher; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.step.builder.StepBuilder; diff --git a/api/src/main/java/ca/bc/gov/educ/api/dataconversion/repository/EventRepository.java b/api/src/main/java/ca/bc/gov/educ/api/dataconversion/repository/EventRepository.java index fe10a968..3ae6a28d 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/dataconversion/repository/EventRepository.java +++ b/api/src/main/java/ca/bc/gov/educ/api/dataconversion/repository/EventRepository.java @@ -2,8 +2,12 @@ import ca.bc.gov.educ.api.dataconversion.entity.Event; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; import java.util.UUID; @@ -13,4 +17,9 @@ public interface EventRepository extends JpaRepository { Optional findByEventId(UUID eventId); List findAllByEventStatusOrderByCreateDate(String eventStatus); + + @Transactional + @Modifying + @Query("delete from Event where createDate <= :createDate") + void deleteByCreateDateBefore(LocalDateTime createDate); } diff --git a/api/src/main/java/ca/bc/gov/educ/api/dataconversion/scheduler/PurgeOldRecordsScheduler.java b/api/src/main/java/ca/bc/gov/educ/api/dataconversion/scheduler/PurgeOldRecordsScheduler.java new file mode 100644 index 00000000..c1928df3 --- /dev/null +++ b/api/src/main/java/ca/bc/gov/educ/api/dataconversion/scheduler/PurgeOldRecordsScheduler.java @@ -0,0 +1,41 @@ +package ca.bc.gov.educ.api.dataconversion.scheduler; + +import ca.bc.gov.educ.api.dataconversion.repository.EventRepository; +import ca.bc.gov.educ.api.dataconversion.util.EducGradDataConversionApiConstants; +import jakarta.transaction.Transactional; +import lombok.extern.slf4j.Slf4j; +import net.javacrumbs.shedlock.core.LockAssert; +import net.javacrumbs.shedlock.spring.annotation.SchedulerLock; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; + +@Component +@Slf4j +public class PurgeOldRecordsScheduler { + private final EventRepository eventRepository; + private final EducGradDataConversionApiConstants constants; + + public PurgeOldRecordsScheduler(final EventRepository eventRepository, + final EducGradDataConversionApiConstants constants) { + this.eventRepository = eventRepository; + this.constants = constants; + } + + @Scheduled(cron = "${cron.scheduled.process.purge-old-records.run}") + @SchedulerLock(name = "PurgeOldRecordsLock", + lockAtLeastFor = "PT1H", lockAtMostFor = "PT1H") //midnight job so lock for an hour + @Transactional + public void purgeOldRecords() { + LockAssert.assertLocked(); + final LocalDateTime createDateToCompare = this.calculateCreateDateBasedOnStaleEventInDays(); + this.eventRepository.deleteByCreateDateBefore(createDateToCompare); + } + + + private LocalDateTime calculateCreateDateBasedOnStaleEventInDays() { + final LocalDateTime currentTime = LocalDateTime.now(); + return currentTime.minusDays(this.constants.getRecordsStaleInDays()); + } +} diff --git a/api/src/main/java/ca/bc/gov/educ/api/dataconversion/util/EducGradDataConversionApiConstants.java b/api/src/main/java/ca/bc/gov/educ/api/dataconversion/util/EducGradDataConversionApiConstants.java index 4bf19040..97d3a817 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/dataconversion/util/EducGradDataConversionApiConstants.java +++ b/api/src/main/java/ca/bc/gov/educ/api/dataconversion/util/EducGradDataConversionApiConstants.java @@ -222,4 +222,7 @@ public class EducGradDataConversionApiConstants { @Value("${cron.scheduled.process.events.stan.threshold}") private int traxToGradProcessingThreshold; + + @Value("${cron.scheduled.process.purge-old-records.staleInDays}") + private int recordsStaleInDays; } diff --git a/api/src/main/resources/application.yaml b/api/src/main/resources/application.yaml index 7eb3f960..56869e1a 100644 --- a/api/src/main/resources/application.yaml +++ b/api/src/main/resources/application.yaml @@ -13,7 +13,7 @@ spring: username: ${CONV_USERNAME} password: ${CONV_PASSWORD} url: ${CONNECTION_STRING} - driver-class: oracle.jdbc.driver.OracleDriver + driver-class-name: oracle.jdbc.driver.OracleDriver jpa: show-sql: ${SHOW_SQL_LOGS} database-platform: org.hibernate.dialect.Oracle12cDialect @@ -177,6 +177,9 @@ cron: lockAtLeastFor: ${CRON_SCHEDULED_PROCESS_EVENTS_STAN_LOCK_AT_LEAST_FOR} lockAtMostFor: ${CRON_SCHEDULED_PROCESS_EVENTS_STAN_LOCK_AT_MOST_FOR} threshold: ${CRON_SCHEDULED_PROCESS_EVENTS_STAN_THRESHOLD} + purge-old-records: + run: ${CRON_SCHEDULED_PURGE_OLD_RECORDS} + staleInDays: ${RECORDS_STALE_IN_DAYS} #Endpoints endpoint: diff --git a/api/src/test/resources/application.yaml b/api/src/test/resources/application.yaml index 6f7080aa..1d5869fa 100644 --- a/api/src/test/resources/application.yaml +++ b/api/src/test/resources/application.yaml @@ -10,7 +10,7 @@ spring: minimum-idle: 1 idle-timeout: 300000 max-lifetime: 600000 - driver-class: org.h2.Driver + driver-class-name: org.h2.Driver url: jdbc:h2:mem:test jpa: show-sql: true @@ -90,6 +90,9 @@ cron: lockAtLeastFor: 800ms lockAtMostFor: 900ms threshold: 100 + purge-old-records: + run: 0 30 0 * * * + staleInDays: 90 #Endpoints endpoint: