Commit c3696dad authored by Enriko Käsper's avatar Enriko Käsper
Browse files

Merge branch 'develop' into 'master'

Release: merge 'develop' into 'master' created by Enriko Käsper

See merge request teis/scheduler-service!14
parents 045f591d 6c54a4b8
theGroup=ee.sm.ti.teis
theVersion=1.4.0
commonsVersion=1.4.0
commonApiGatewayVersion=1.4.0
theVersion=1.5.0
commonsVersion=1.5.1
commonApiGatewayVersion=1.5.1
adminApiGatewayVersion=1.3.0
pluginVersion=1.0.1
mapstructVersion=1.3.0.Final
......@@ -2,6 +2,7 @@ package ee.sm.ti.teis.scheduler.job.executor;
import ee.sm.ti.teis.domainrequest.SystemJobFinishedEventDto;
import ee.sm.ti.teis.domainrequest.SystemJobStartedEventDto;
import ee.sm.ti.teis.exceptions.TeisRestException;
import ee.sm.ti.teis.scheduler.domain.SystemJob;
import ee.sm.ti.teis.scheduler.message.SystemJobFinishedRequest;
import ee.sm.ti.teis.scheduler.message.SystemJobStartedRequest;
......@@ -16,16 +17,21 @@ import java.util.UUID;
@Slf4j
public abstract class SystemJobExecutor {
private static final String WARNING_MESSAGE_PREFIX = "Warning! ";
private final RabbitTemplate msRabbitTemplate;
protected abstract String executeInternal(SystemJob payload, RequestMetaDTO requestMetaDto);
protected abstract String getJobId();
public SystemJobExecutor(RabbitTemplate msRabbitTemplate) {
this.msRabbitTemplate = msRabbitTemplate;
}
public static String composeWarningMessage(String message) {
return WARNING_MESSAGE_PREFIX + message;
}
protected abstract String executeInternal(SystemJob payload, RequestMetaDTO requestMetaDto);
protected abstract String getJobId();
public void execute(SystemJob systemJob, RequestMetaDTO requestMetaDto) {
if (systemJob.getExecutionId() == null) {
systemJob.setExecutionId(UUID.randomUUID());
......@@ -34,18 +40,32 @@ public abstract class SystemJobExecutor {
notifyJobStartedEvent(systemJob, requestMetaDto);
try {
String result = executeInternal(systemJob, requestMetaDto);
systemJob.setResponseCode(SystemJobResponseCode.SUCCESS);
if (isWarningMessage(result)) {
systemJob.setResponseCode(SystemJobResponseCode.WARNINGS);
log.warn("System job execution with warnings. {}", result);
} else {
systemJob.setResponseCode(SystemJobResponseCode.SUCCESS);
log.info("System job execution success. {}", result);
}
systemJob.setResponseMessage(result);
log.info("System job execution success. {}", result);
} catch (Exception e) {
log.error("System job execution failed.", e);
systemJob.setResponseCode(SystemJobResponseCode.FAILED);
systemJob.setResponseMessage(e.getMessage());
// FIXME: remove if clause, after TeisRestException returns message from getMessage() method in commons-public version 1.6.0
if (e instanceof TeisRestException && ((TeisRestException) e).getError() != null) {
systemJob.setResponseMessage(((TeisRestException) e).getError().getMessage());
} else {
systemJob.setResponseMessage(e.getMessage());
}
}
systemJob.setFinishedAt(LocalDateTime.now());
notifyJobFinishedEvent(systemJob, requestMetaDto);
}
protected boolean isWarningMessage(String message) {
return message.startsWith(WARNING_MESSAGE_PREFIX);
}
private void notifyJobStartedEvent(SystemJob payload, RequestMetaDTO requestMetaDto) {
SystemJobStartedEventDto requestDTO = new SystemJobStartedRequest();
requestDTO.setPayload(payload, requestMetaDto);
......
......@@ -16,6 +16,7 @@ import org.springframework.amqp.rabbit.core.RabbitTemplate;
import java.util.UUID;
import static ee.sm.ti.teis.scheduler.job.executor.SystemJobExecutor.composeWarningMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
......@@ -24,6 +25,8 @@ import static org.mockito.Mockito.verify;
public class SystemJobExecutorTest {
private static final String JOB_ID = "testJob";
private static final String JOB2_ID = "testJob2";
@Mock
RabbitTemplate msRabbitTemplate;
@Captor
......@@ -31,15 +34,13 @@ public class SystemJobExecutorTest {
@Captor
ArgumentCaptor<String> routingKeyCaptor;
private static final String JOB_ID = "testJob";
@Test
public void should_sendStartedAndFinishedNotifications_success() {
SystemJobExecutor testJob = new TestJob(msRabbitTemplate);
SystemJob systemJob = SystemJob.builder().id(JOB_ID).build();
testJob.execute(systemJob, createRequestMeta());
verify(msRabbitTemplate, times(2)).convertAndSend(routingKeyCaptor.capture(), systemJobRequestCaptor.capture());
// started notification
assertThat(routingKeyCaptor.getAllValues().get(0)).containsIgnoringCase("started");
......@@ -63,6 +64,47 @@ public class SystemJobExecutorTest {
assertThat(finishedEventMessage.getPayload().getResponseMessage()).isEqualTo("Done");
}
@Test
public void correctResultType_when_job_executed_with_warnings() {
SystemJobExecutor testJob = new TestJobWithWarnings(msRabbitTemplate);
SystemJob systemJob = SystemJob.builder().id(JOB2_ID).build();
testJob.execute(systemJob, createRequestMeta());
verify(msRabbitTemplate, times(2)).convertAndSend(routingKeyCaptor.capture(), systemJobRequestCaptor.capture());
// started notification
assertThat(routingKeyCaptor.getAllValues().get(0)).containsIgnoringCase("started");
// finished notification
assertThat(routingKeyCaptor.getAllValues().get(1)).containsIgnoringCase("finished");
SystemJobFinishedRequest finishedEventMessage = (SystemJobFinishedRequest) systemJobRequestCaptor.getAllValues().get(1);
assertThat(finishedEventMessage.getPayload().getId()).isEqualTo(JOB2_ID);
assertThat(finishedEventMessage.getPayload().getStartedAt()).isNotNull();
assertThat(finishedEventMessage.getPayload().getFinishedAt()).isNotNull();
assertThat(finishedEventMessage.getPayload().getResponseCode()).isEqualTo(SystemJobResponseCode.WARNINGS);
assertThat(finishedEventMessage.getPayload().getResponseMessage()).isEqualTo("Warning! Done with warnings");
}
@Test
void test_is_warning_message() {
SystemJobExecutor testJob = new TestJob(msRabbitTemplate);
assertThat(testJob.isWarningMessage("Some message")).isFalse();
assertThat(testJob.isWarningMessage("Warning! Some message")).isTrue();
}
@Test
void test_compose_warning_message() {
SystemJobExecutor testJob = new TestJob(msRabbitTemplate);
assertThat(composeWarningMessage("Some message")).isEqualTo("Warning! Some message");
}
RequestMetaDTO createRequestMeta() {
return RequestMetaDTO.builder()
.requestId(UUID.randomUUID().toString())
.userType(UserType.SYSTEM)
.userId("testSystem")
.build();
}
public class TestJob extends SystemJobExecutor {
public TestJob(RabbitTemplate msRabbitTemplate) {
......@@ -80,11 +122,20 @@ public class SystemJobExecutorTest {
}
}
RequestMetaDTO createRequestMeta() {
return RequestMetaDTO.builder()
.requestId(UUID.randomUUID().toString())
.userType(UserType.SYSTEM)
.userId("testSystem")
.build();
public class TestJobWithWarnings extends SystemJobExecutor {
public TestJobWithWarnings(RabbitTemplate msRabbitTemplate) {
super(msRabbitTemplate);
}
@Override
protected String executeInternal(SystemJob payload, RequestMetaDTO requestMetaDto) {
return composeWarningMessage("Done with warnings");
}
@Override
protected String getJobId() {
return JOB2_ID;
}
}
}
......@@ -17,6 +17,7 @@ import java.time.LocalDateTime;
import java.util.Optional;
import static java.time.LocalDateTime.now;
import static java.util.Optional.ofNullable;
@Service
@Transactional
......@@ -78,21 +79,23 @@ public class JobProcessingService {
jobExecutionService.update(jobExecution);
if (SystemJobResponseCode.FAILED == jobExecution.getResponseCode() || SystemJobResponseCode.WARNINGS == jobExecution.getResponseCode()) {
log.warn("Job event 'jobFinished' processed for jobId={} with executionId={}. Response code={} and message={}",
systemJob.getId(), systemJob.getExecutionId(), systemJob.getResponseCode(), systemJob.getResponseMessage());
systemJob.getId(), systemJob.getExecutionId(), systemJob.getResponseCode(),
ofNullable(systemJob.getResponseMessage()).orElse("null"));
} else {
log.info("Job event 'jobFinished' processed for jobId={} with executionId={}. Response code={} and message={}",
systemJob.getId(), systemJob.getExecutionId(), systemJob.getResponseCode(), systemJob.getResponseMessage());
systemJob.getId(), systemJob.getExecutionId(), systemJob.getResponseCode(),
ofNullable(systemJob.getResponseMessage()).orElse("null"));
}
}
public void timeoutJob(SystemJob systemJob) {
if (systemJob.getStartedAt() != null) {
systemJob.setResponseCode(SystemJobResponseCode.FAILED_FINISH_TIMEOUT);
systemJob.setResponseMessage("Job starting timed out. No job executors available.");
systemJob.setFinishedAt(now());
systemJob.setResponseMessage("Job finishing timed out.");
} else {
systemJob.setResponseCode(SystemJobResponseCode.FAILED_START_TIMEOUT);
systemJob.setResponseMessage("Job finishing timed out.");
systemJob.setResponseMessage("Job starting timed out. No job executors available.");
}
finishJob(systemJob);
log.warn("Job event 'jobTimeout' processed for jobId={} with executionId={}. Response code={}.",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment