Skip to content

Commit

Permalink
Merge pull request #605 from bcgov/feature/GRAD2-2430
Browse files Browse the repository at this point in the history
GRAD2-2430: task is completed.
  • Loading branch information
kamal-mohammed authored Dec 19, 2023
2 parents eab22fb + 8fe4fe3 commit 5ea93b6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import ca.bc.gov.educ.api.gradstudent.model.entity.GradStatusEvent;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -35,4 +39,9 @@ public interface GradStatusEventRepository extends JpaRepository<GradStatusEvent
* @return the list
*/
List<GradStatusEvent> findByEventStatusOrderByCreateDate(String eventStatus);

@Transactional
@Modifying
@Query("delete from GradStatusEvent where createDate <= :createDate")
void deleteByCreateDateBefore(LocalDateTime createDate);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ca.bc.gov.educ.api.gradstudent.scheduler;

import ca.bc.gov.educ.api.gradstudent.repository.GradStatusEventRepository;
import ca.bc.gov.educ.api.gradstudent.util.EducGradStudentApiConstants;
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 GradStatusEventRepository gradStatusEventRepository;
private final EducGradStudentApiConstants constants;

public PurgeOldRecordsScheduler(final GradStatusEventRepository gradStatusEventRepository,
final EducGradStudentApiConstants constants) {
this.gradStatusEventRepository = gradStatusEventRepository;
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.gradStatusEventRepository.deleteByCreateDateBefore(createDateToCompare);
}

private LocalDateTime calculateCreateDateBasedOnStaleEventInDays() {
final LocalDateTime currentTime = LocalDateTime.now();
return currentTime.minusDays(this.constants.getRecordsStaleInDays());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,7 @@ public class EducGradStudentApiConstants {

@Value("${cron.scheduled.process.events.stan.threshold}")
private int gradToTraxProcessingThreshold;

@Value("${cron.scheduled.process.purge-old-records.staleInDays}")
private int recordsStaleInDays;
}
3 changes: 3 additions & 0 deletions api/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,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}

#Resilience
resilience4j.retry:
Expand Down
3 changes: 3 additions & 0 deletions api/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ cron:
lockAtLeastFor: 800ms
lockAtMostFor: 900ms
threshold: 100
purge-old-records:
run: 0 30 0 * * *
staleInDays: 90

#Endpoint properties
endpoint:
Expand Down

0 comments on commit 5ea93b6

Please sign in to comment.