Skip to content

Commit

Permalink
GRAD2-2430: task is complete.
Browse files Browse the repository at this point in the history
GRAD2-2430: task is complete.
  • Loading branch information
infstar committed Dec 19, 2023
1 parent 6c83e7c commit 6d52aff
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,4 +17,9 @@ public interface EventRepository extends JpaRepository<Event, UUID> {
Optional<Event> findByEventId(UUID eventId);

List<Event> findAllByEventStatusOrderByCreateDate(String eventStatus);

@Transactional
@Modifying
@Query("delete from Event where createDate <= :createDate")
void deleteByCreateDateBefore(LocalDateTime createDate);
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 4 additions & 1 deletion api/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion api/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -90,6 +90,9 @@ cron:
lockAtLeastFor: 800ms
lockAtMostFor: 900ms
threshold: 100
purge-old-records:
run: 0 30 0 * * *
staleInDays: 90

#Endpoints
endpoint:
Expand Down

0 comments on commit 6d52aff

Please sign in to comment.