Skip to content

Commit

Permalink
GRAD2-3012: task is complete.
Browse files Browse the repository at this point in the history
GRAD2-3012: task is complete.
  • Loading branch information
infstar committed Nov 22, 2024
1 parent 98d80be commit 99a144d
Show file tree
Hide file tree
Showing 19 changed files with 60 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package ca.bc.gov.educ.api.batchgraduation.listener;

import ca.bc.gov.educ.api.batchgraduation.model.StudentSearchRequest;
import ca.bc.gov.educ.api.batchgraduation.rest.RestUtils;
import ca.bc.gov.educ.api.batchgraduation.util.JsonTransformer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.ZoneId;
import java.util.Date;
import java.util.List;

import static ca.bc.gov.educ.api.batchgraduation.util.EducGradBatchGraduationApiConstants.SEARCH_REQUEST;

@Component
public class SpecialRunCompletionNotificationListener extends BaseRunCompletionNotificationListener {
Expand All @@ -29,31 +23,14 @@ public class SpecialRunCompletionNotificationListener extends BaseRunCompletionN
@Autowired
JsonTransformer jsonTransformer;

private static final String RUN_BY = "runBy";

@Override
public void afterJob(JobExecution jobExecution) {
if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
long elapsedTimeMillis = new Date().getTime() - jobExecution.getStartTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
LOGGER.info("=======================================================================================");
LOGGER.info("Special Job completed in {} s with jobExecution status {}", elapsedTimeMillis/1000, jobExecution.getStatus());
handleSummary(jobExecution, "spcRunAlgSummaryDTO", true);
processGradStudentRecordJobHistory(jobExecution);
LOGGER.info("=======================================================================================");
}
}

private void processGradStudentRecordJobHistory(JobExecution jobExecution) {

JobParameters jobParameters = jobExecution.getJobParameters();
Long batchId = jobExecution.getId();
String userName = jobParameters.getString(RUN_BY);

String searchRequest = jobParameters.getString(SEARCH_REQUEST, "{}");
StudentSearchRequest req = (StudentSearchRequest)jsonTransformer.unmarshall(searchRequest, StudentSearchRequest.class);

restUtils.updateStudentGradRecordHistory(List.of(), batchId, userName, StringUtils.upperCase(req.getActivityCode()));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public abstract class BasePartitioner extends SimplePartitioner {

private static final Logger LOGGER = LoggerFactory.getLogger(BasePartitioner.class);
private static final String RERUN_TYPE = "reRunType";
private static final String RUN_BY = "runBy";
protected static final String RUN_BY = "runBy";
private static final String PREV_BATCH_ID = "previousBatchId";
private static final String RERUN_ALL = "RERUN_ALL";
private static final String RERUN_FAILED = "RERUN_FAILED";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import ca.bc.gov.educ.api.batchgraduation.model.AlgorithmSummaryDTO;
import ca.bc.gov.educ.api.batchgraduation.model.ResponseObj;
import ca.bc.gov.educ.api.batchgraduation.rest.RestUtils;
import ca.bc.gov.educ.api.batchgraduation.util.ThreadLocalStateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -20,6 +22,9 @@ public abstract class BaseStudentReader implements ItemReader<UUID> {
@Value("#{stepExecutionContext['index']}")
protected Integer nxtStudentForProcessing;

@Value("#{stepExecutionContext['runBy']}")
protected String runBy;

@Value("#{stepExecutionContext['data']}")
protected List<UUID> studentList;

Expand All @@ -35,4 +40,10 @@ protected void fetchAccessToken() {
summaryDTO.setAccessToken(res.getAccess_token());
}
}

protected void setUserName() {
if (StringUtils.isNotBlank(runBy) && StringUtils.isBlank(ThreadLocalStateUtil.getCurrentUser())) {
ThreadLocalStateUtil.setCurrentUser(runBy);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class RecalculateProjectedGradRunErrorReader extends BaseStudentReader {
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
UUID nextStudent = null;

if (nxtStudentForProcessing < studentList.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class RecalculateProjectedGradRunErrorRetryReader extends BaseStudentRead
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
UUID nextStudent = null;

if (nxtStudentForProcessing < studentList.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class RecalculateProjectedGradRunReader extends BaseStudentReader {
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
UUID nextStudent = null;

if (nxtStudentForProcessing < studentList.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class RecalculateStudentErrorReader extends BaseStudentReader {
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
UUID nextStudent = null;

if (nxtStudentForProcessing < studentList.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class RecalculateStudentErrorRetryReader extends BaseStudentReader {
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
UUID nextStudent = null;

if (nxtStudentForProcessing < studentList.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class RecalculateStudentReader extends BaseStudentReader {
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
summaryDTO.setReadCount(studentList.size());

UUID nextStudent = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.beans.factory.annotation.Value;

Expand All @@ -31,6 +32,8 @@ public JobExecution getJobExecution() {
public Map<String, ExecutionContext> partition(int gridSize) {
initializeRunType();
BatchGradAlgorithmJobHistoryEntity jobHistory = createBatchJobHistory();
JobParameters jobParameters = jobExecution.getJobParameters();
String username = jobParameters.getString(RUN_BY);
List<UUID> studentList;
if (runType == RunTypeEnum.NORMAL_JOB_PROCESS) {
studentList = restUtils.getStudentsForAlgorithm();
Expand All @@ -57,6 +60,7 @@ public Map<String, ExecutionContext> partition(int gridSize) {
summaryDTO.setReadCount(data.size());
executionContext.put("summary", summaryDTO);
executionContext.put("index",0);
executionContext.put("runBy", username);
String key = "partition" + i;
map.put(key, executionContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.beans.factory.annotation.Value;

Expand All @@ -30,6 +31,8 @@ public JobExecution getJobExecution() {
@Override
public Map<String, ExecutionContext> partition(int gridSize) {
Long batchId = jobExecution.getId();
JobParameters jobParameters = jobExecution.getJobParameters();
String username = jobParameters.getString(RUN_BY);
List<UUID> studentList = getInputDataForErroredStudents(batchId);
createTotalSummaryDTO("regGradAlgSummaryDTO");

Expand All @@ -48,6 +51,7 @@ public Map<String, ExecutionContext> partition(int gridSize) {
summaryDTO.setReadCount(data.size());
executionContext.put("summary", summaryDTO);
executionContext.put("index",0);
executionContext.put("runBy", username);
String key = "partition" + i;
map.put(key, executionContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ca.bc.gov.educ.api.batchgraduation.model.AlgorithmSummaryDTO;
import ca.bc.gov.educ.api.batchgraduation.model.RunTypeEnum;
import ca.bc.gov.educ.api.batchgraduation.model.StudentSearchRequest;
import ca.bc.gov.educ.api.batchgraduation.util.ThreadLocalStateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
Expand Down Expand Up @@ -37,6 +38,7 @@ public Map<String, ExecutionContext> partition(int gridSize) {
BatchGradAlgorithmJobHistoryEntity jobHistory = createBatchJobHistory();
List<UUID> studentList;
JobParameters jobParameters = jobExecution.getJobParameters();
String username = jobParameters.getString(RUN_BY);
String searchRequest = jobParameters.getString(SEARCH_REQUEST, "{}");
StudentSearchRequest req = (StudentSearchRequest)jsonTransformer.unmarshall(searchRequest, StudentSearchRequest.class);
if (runType == RunTypeEnum.NORMAL_JOB_PROCESS) {
Expand Down Expand Up @@ -64,6 +66,7 @@ public Map<String, ExecutionContext> partition(int gridSize) {
summaryDTO.setReadCount(data.size());
executionContext.put("summary", summaryDTO);
executionContext.put("index",0);
executionContext.put("runBy", username);
String key = "partition" + i;
map.put(key, executionContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ca.bc.gov.educ.api.batchgraduation.model.AlgorithmSummaryDTO;
import ca.bc.gov.educ.api.batchgraduation.model.RunTypeEnum;
import ca.bc.gov.educ.api.batchgraduation.model.StudentSearchRequest;
import ca.bc.gov.educ.api.batchgraduation.util.ThreadLocalStateUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
Expand Down Expand Up @@ -34,6 +35,8 @@ public JobExecution getJobExecution() {
@Override
public Map<String, ExecutionContext> partition(int gridSize) {
Long batchId = jobExecution.getId();
JobParameters jobParameters = jobExecution.getJobParameters();
String username = jobParameters.getString(RUN_BY);
List<UUID> studentList = getInputDataForErroredStudents(batchId);
createTotalSummaryDTO("spcRunAlgSummaryDTO");

Expand All @@ -52,6 +55,7 @@ public Map<String, ExecutionContext> partition(int gridSize) {
summaryDTO.setReadCount(data.size());
executionContext.put("summary", summaryDTO);
executionContext.put("index",0);
executionContext.put("runBy", username);
String key = "partition" + i;
map.put(key, executionContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class SpecialGradRunStudentReader extends BaseStudentReader {
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
UUID nextStudent = null;

if (nxtStudentForProcessing < studentList.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class SpecialProjectedGradRunReader extends BaseStudentReader {
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
UUID nextStudent = null;

if (nxtStudentForProcessing < studentList.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.beans.factory.annotation.Value;

Expand All @@ -31,6 +32,8 @@ public JobExecution getJobExecution() {
public Map<String, ExecutionContext> partition(int gridSize) {
initializeRunType();
BatchGradAlgorithmJobHistoryEntity jobHistory = createBatchJobHistory();
JobParameters jobParameters = jobExecution.getJobParameters();
String username = jobParameters.getString(RUN_BY);
List<UUID> studentList;
if (runType == RunTypeEnum.NORMAL_JOB_PROCESS) {
studentList = restUtils.getStudentsForProjectedAlgorithm();
Expand All @@ -57,6 +60,7 @@ public Map<String, ExecutionContext> partition(int gridSize) {
summaryDTO.setReadCount(data.size());
executionContext.put("summary", summaryDTO);
executionContext.put("index",0);
executionContext.put("runBy", username);
String key = "partition" + i;
map.put(key, executionContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.beans.factory.annotation.Value;

Expand All @@ -30,6 +31,8 @@ public JobExecution getJobExecution() {
@Override
public Map<String, ExecutionContext> partition(int gridSize) {
Long batchId = jobExecution.getId();
JobParameters jobParameters = jobExecution.getJobParameters();
String username = jobParameters.getString(RUN_BY);
List<UUID> studentList = getInputDataForErroredStudents(batchId);
createTotalSummaryDTO("tvrRunSummaryDTO");

Expand All @@ -48,6 +51,7 @@ public Map<String, ExecutionContext> partition(int gridSize) {
summaryDTO.setReadCount(data.size());
executionContext.put("summary", summaryDTO);
executionContext.put("index",0);
executionContext.put("runBy", username);
String key = "partition" + i;
map.put(key, executionContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ public <T> T get(String url, Class<T> clazz) {
obj = this.batchWebClient
.get()
.uri(url)
.headers(h -> h.set(EducGradBatchGraduationApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID()))
.headers(h -> {
h.set(EducGradBatchGraduationApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID());
h.set(EducGradBatchGraduationApiConstants.USERNAME, ThreadLocalStateUtil.getCurrentUser());
})
.retrieve()
// if 5xx errors, throw Service error
.onStatus(HttpStatusCode::is5xxServerError,
Expand Down Expand Up @@ -154,7 +157,10 @@ public <T> T post(String url, Object body, Class<T> clazz) {
try {
obj = this.batchWebClient.post()
.uri(url)
.headers(h -> h.set(EducGradBatchGraduationApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID()))
.headers(h -> {
h.set(EducGradBatchGraduationApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID());
h.set(EducGradBatchGraduationApiConstants.USERNAME, ThreadLocalStateUtil.getCurrentUser());
})
.body(BodyInserters.fromValue(body))
.retrieve()
.onStatus(HttpStatusCode::is5xxServerError,
Expand Down Expand Up @@ -192,7 +198,10 @@ public <T> T put(String url, Object body, Class<T> clazz) {
try {
obj = this.batchWebClient.put()
.uri(url)
.headers(h -> h.set(EducGradBatchGraduationApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID()))
.headers(h -> {
h.set(EducGradBatchGraduationApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID());
h.set(EducGradBatchGraduationApiConstants.USERNAME, ThreadLocalStateUtil.getCurrentUser());
})
.body(BodyInserters.fromValue(body))
.retrieve()
.onStatus(HttpStatusCode::is5xxServerError,
Expand Down Expand Up @@ -227,7 +236,10 @@ public <T> T delete(String url, Class<T> boundClass) {
T obj;
try {
obj = this.batchWebClient.delete().uri(url)
.headers(h -> h.set(EducGradBatchGraduationApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID()))
.headers(h -> {
h.set(EducGradBatchGraduationApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID());
h.set(EducGradBatchGraduationApiConstants.USERNAME, ThreadLocalStateUtil.getCurrentUser());
})
.retrieve().bodyToMono(boundClass).block();
} catch(Exception e) {
// catches IOExceptions and the like
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class EducGradBatchGraduationApiConstants {
public static final String API_ROOT_MAPPING = "";
public static final String API_VERSION = "v1";
public static final String CORRELATION_ID = "correlationID";
public static final String USERNAME = "username";
public static final String GRAD_BATCH_API_ROOT_MAPPING = "/api/" + API_VERSION + "/batch";

// Manual Run
Expand Down

0 comments on commit 99a144d

Please sign in to comment.