From 8034e5285db2fa474e9a30fd596b2f82f75e1e59 Mon Sep 17 00:00:00 2001 From: Jinil Sung Date: Tue, 19 Sep 2023 11:55:15 -0700 Subject: [PATCH] GRAD2-2334: task is complete GRAD2-2334: task is complete --- .../scheduler/JetStreamEventScheduler.java | 36 ++++++++++++------- .../util/EducGradStudentApiConstants.java | 15 ++++++++ api/src/main/resources/application.yaml | 1 + api/src/test/resources/application.yaml | 1 + 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/api/src/main/java/ca/bc/gov/educ/api/gradstudent/scheduler/JetStreamEventScheduler.java b/api/src/main/java/ca/bc/gov/educ/api/gradstudent/scheduler/JetStreamEventScheduler.java index 6124732b..349b3cda 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/gradstudent/scheduler/JetStreamEventScheduler.java +++ b/api/src/main/java/ca/bc/gov/educ/api/gradstudent/scheduler/JetStreamEventScheduler.java @@ -1,15 +1,15 @@ package ca.bc.gov.educ.api.gradstudent.scheduler; import ca.bc.gov.educ.api.gradstudent.messaging.jetstream.Publisher; +import ca.bc.gov.educ.api.gradstudent.model.entity.GradStatusEvent; import ca.bc.gov.educ.api.gradstudent.repository.GradStatusEventRepository; +import ca.bc.gov.educ.api.gradstudent.util.EducGradStudentApiConstants; 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; - import static ca.bc.gov.educ.api.gradstudent.constant.EventStatus.DB_COMMITTED; /** @@ -23,35 +23,45 @@ public class JetStreamEventScheduler { private final GradStatusEventRepository gradStatusEventRepository; private final Publisher publisher; + private final EducGradStudentApiConstants constants; + /** * Instantiates a new Stan event scheduler. * * @param gradStatusEventRepository the grad status event repository * @param publisher the publisher */ - public JetStreamEventScheduler(GradStatusEventRepository gradStatusEventRepository, Publisher publisher) { + public JetStreamEventScheduler(final GradStatusEventRepository gradStatusEventRepository, + final Publisher publisher, + final EducGradStudentApiConstants constants) { this.gradStatusEventRepository = gradStatusEventRepository; this.publisher = publisher; + this.constants = constants; } /** * Find and publish grad status events to stan. */ - @Scheduled(cron = "${cron.scheduled.process.events.stan.run}") // minimum = every 5 minutes + @Scheduled(cron = "${cron.scheduled.process.events.stan.run}") @SchedulerLock(name = "PUBLISH_GRAD_STATUS_EVENTS_TO_JET_STREAM", lockAtLeastFor = "${cron.scheduled.process.events.stan.lockAtLeastFor}", lockAtMostFor = "${cron.scheduled.process.events.stan.lockAtMostFor}") public void findAndPublishGradStatusEventsToJetStream() { + log.debug("PUBLISH_GRAD_STATUS_EVENTS_TO_JET_STREAM: started - cron {}, lockAtMostFor {}", constants.getGradToTraxCronRun(), constants.getGradToTraxLockAtMostFor()); LockAssert.assertLocked(); var results = gradStatusEventRepository.findByEventStatusOrderByCreateDate(DB_COMMITTED.toString()); if (!results.isEmpty()) { - results.stream() - .filter(el -> el.getUpdateDate().isBefore(LocalDateTime.now().minusMinutes(5))) - .forEach(el -> { - try { - publisher.dispatchChoreographyEvent(el); - } catch (final Exception ex) { - log.error("Exception while trying to publish message", ex); - } - }); + int cnt = 0; + for (GradStatusEvent el : results) { + if (cnt++ >= constants.getGradToTraxProcessingThreshold()) { + log.info(" ==> Reached the processing threshold of {}", constants.getGradToTraxProcessingThreshold()); + break; + } + try { + publisher.dispatchChoreographyEvent(el); + } catch (final Exception ex) { + log.error("Exception while trying to publish message", ex); + } + } + log.debug("PUBLISH_GRAD_STATUS_EVENTS_TO_JET_STREAM: processing is completed"); } } } diff --git a/api/src/main/java/ca/bc/gov/educ/api/gradstudent/util/EducGradStudentApiConstants.java b/api/src/main/java/ca/bc/gov/educ/api/gradstudent/util/EducGradStudentApiConstants.java index a6a8ac41..40247e9e 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/gradstudent/util/EducGradStudentApiConstants.java +++ b/api/src/main/java/ca/bc/gov/educ/api/gradstudent/util/EducGradStudentApiConstants.java @@ -103,6 +103,8 @@ public class EducGradStudentApiConstants { public static final String CONV_STUDENT_OPTIONAL_PROGRAM_BY_STUDENT_ID = "/conv/studentoptionalprogram/{optionalProgramID}/{studentID}"; public static final String CONV_STUDENT_CAREER_PROGRAM_BY_STUDENT_ID = "/conv/studentcareerprogram/{careerProgramCode}/{studentID}"; + public static final String EDW_GRADUATION_STATUS_SNAPSHOT = "/edw/snapshot"; + //Default Date format constants public static final String DEFAULT_CREATED_BY = "API_GRAD_STUDENT"; public static final String DEFAULT_UPDATED_BY = "API_GRAD_STUDENT"; @@ -192,4 +194,17 @@ public class EducGradStudentApiConstants { // Data Conversion option @Value("${data-conversion.student-guid-pen-xref.enabled}") private boolean studentGuidPenXrefEnabled; + + // Scheduler: ongoing updates from GRAD to TRAX + @Value("${cron.scheduled.process.events.stan.run}") + private String gradToTraxCronRun; + + @Value("${cron.scheduled.process.events.stan.lockAtLeastFor}") + private String gradToTraxLockAtLeastFor; + + @Value("${cron.scheduled.process.events.stan.lockAtMostFor}") + private String gradToTraxLockAtMostFor; + + @Value("${cron.scheduled.process.events.stan.threshold}") + private int gradToTraxProcessingThreshold; } diff --git a/api/src/main/resources/application.yaml b/api/src/main/resources/application.yaml index b152f4c7..6f1d051c 100644 --- a/api/src/main/resources/application.yaml +++ b/api/src/main/resources/application.yaml @@ -107,6 +107,7 @@ cron: run: ${CRON_SCHEDULED_PROCESS_EVENTS_STAN} 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} #Resilience resilience4j.retry: diff --git a/api/src/test/resources/application.yaml b/api/src/test/resources/application.yaml index 7d12c246..6a050df6 100644 --- a/api/src/test/resources/application.yaml +++ b/api/src/test/resources/application.yaml @@ -94,6 +94,7 @@ cron: run: 0 0/5 * * * * lockAtLeastFor: 800ms lockAtMostFor: 900ms + threshold: 100 #Endpoint properties endpoint: