Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GRAD2-2334: task is complete #577

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions api/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions api/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ cron:
run: 0 0/5 * * * *
lockAtLeastFor: 800ms
lockAtMostFor: 900ms
threshold: 100

#Endpoint properties
endpoint:
Expand Down
Loading