Skip to content

Commit

Permalink
Only store the job status when explicitly stated
Browse files Browse the repository at this point in the history
Co-authored-by: Erik Wölfel <erik.woelfel@otto.de>
  • Loading branch information
MediaMarco and ewoelfel committed Aug 7, 2018
1 parent b06300a commit 4345c00
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
Expand Down Expand Up @@ -190,7 +191,7 @@ private void stopJob(final String jobId,

public void appendMessage(final String jobId,
final JobMessage jobMessage) {
writeMessageAndStatus(jobId, jobMessage.getLevel(), jobMessage.getMessage(), jobMessage.getLevel() == Level.ERROR ? ERROR : OK, jobMessage.getTimestamp());
writeMessageAndStatus(jobId, jobMessage.getLevel(), jobMessage.getMessage(), jobMessage.getLevel() == Level.ERROR ? ERROR : null, jobMessage.getTimestamp());
}

public void keepAlive(final String jobId) {
Expand All @@ -210,10 +211,12 @@ private void writeMessageAndStatus(final String jobId, Level messageLevel, Strin
writeMessageAndStatus(jobId, messageLevel, message, jobStatus, currentTimestamp);
}

private void writeMessageAndStatus(final String jobId, Level messageLevel, String message, final JobStatus jobStatus, OffsetDateTime timestamp) {
private void writeMessageAndStatus(final String jobId, Level messageLevel, String message, @Nullable final JobStatus jobStatus, OffsetDateTime timestamp) {
// TODO: Refactor JobRepository so only a single update is required
jobRepository.appendMessage(jobId, jobMessage(messageLevel, message, timestamp));
jobRepository.setJobStatus(jobId, jobStatus);
if (jobStatus != null) {
jobRepository.setJobStatus(jobId, jobStatus);
}
}

private JobInfo createJobInfo(final String jobType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,25 @@ public void shouldAppendErrorMessageAndSetErrorStatus() {
verify(jobRepository).setJobStatus(JOB_ID, JobInfo.JobStatus.ERROR);
}

@Test
public void shouldNotChangeStatusToOKWhenOnlyAppendingAMessage() {
OffsetDateTime now = OffsetDateTime.now(clock);
OffsetDateTime earlier = now.minus(10, MINUTES);
JobMessage message = JobMessage.jobMessage(Level.INFO, "Some info message", now);
JobInfo jobInfo = defaultJobInfo()
.setLastUpdated(earlier)
.setStatus(JobInfo.JobStatus.SKIPPED)
.build();
when(jobRepository.findOne(JOB_ID)).thenReturn(Optional.of(jobInfo));

// when
jobService.appendMessage(JOB_ID, message);

// then
verify(jobRepository).appendMessage(JOB_ID, message);
verifyNoMoreInteractions(jobRepository);
}

private JobInfo.Builder defaultJobInfo() {
return newJobInfo(JOB_ID, JOB_TYPE, clock, HOSTNAME).copy();
}
Expand Down

0 comments on commit 4345c00

Please sign in to comment.