-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #297 from bcgov/feature/GRAD2-2430
GRAD2-2430: task is complete.
- Loading branch information
Showing
6 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
api/src/main/java/ca/bc/gov/educ/api/trax/scheduler/PurgeOldRecordsScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package ca.bc.gov.educ.api.trax.scheduler; | ||
|
||
import ca.bc.gov.educ.api.trax.repository.EventRepository; | ||
import ca.bc.gov.educ.api.trax.repository.TraxUpdatedPubEventRepository; | ||
import ca.bc.gov.educ.api.trax.util.EducGradTraxApiConstants; | ||
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 TraxUpdatedPubEventRepository traxUpdatedPubEventRepository; | ||
private final EducGradTraxApiConstants constants; | ||
|
||
public PurgeOldRecordsScheduler(final EventRepository eventRepository, | ||
final TraxUpdatedPubEventRepository traxUpdatedPubEventRepository, | ||
final EducGradTraxApiConstants constants) { | ||
this.eventRepository = eventRepository; | ||
this.traxUpdatedPubEventRepository = traxUpdatedPubEventRepository; | ||
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); | ||
this.traxUpdatedPubEventRepository.deleteByCreateDateBefore(createDateToCompare); | ||
} | ||
|
||
private LocalDateTime calculateCreateDateBasedOnStaleEventInDays() { | ||
final LocalDateTime currentTime = LocalDateTime.now(); | ||
return currentTime.minusDays(this.constants.getRecordsStaleInDays()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters