@Override public void setJobStatus(String jobId, JobStatus jobStatus) { JobInfo jobInfo = jobs.get(jobId); jobs.replace(jobId, jobInfo.copy().setStatus(jobStatus).build()); }
@Test public void shouldFindAllJobsOfSpecificType() throws Exception { // Given final String type = "TEST"; final String otherType = "OTHERTEST"; repository.createOrUpdate(builder() .setJobId("1") .setJobType(type) .setStarted(now(fixed(Instant.now().minusSeconds(10), systemDefault()))) .setStopped(now(fixed(Instant.now().minusSeconds(7), systemDefault()))) .setHostname("localhost") .setStatus(JobStatus.OK) .build()); repository.createOrUpdate(newJobInfo("2", otherType, systemDefaultZone(), "localhost")); repository.createOrUpdate(newJobInfo("3", type, systemDefaultZone(), "localhost")); // When final List<JobInfo> jobsType1 = repository.findByType(type); final List<JobInfo> jobsType2 = repository.findByType(otherType); // Then assertThat(jobsType1.size(), is(2)); assertThat(jobsType1.stream().anyMatch(job -> job.getJobId().equals("1")), is(true)); assertThat(jobsType1.stream().anyMatch(job -> job.getJobId().equals("3")), is(true)); assertThat(jobsType2.size(), is(1)); assertThat(jobsType2.stream().anyMatch(job -> job.getJobId().equals("2")), is(true)); }
@Test public void shouldKillJob() { OffsetDateTime now = OffsetDateTime.now(clock); JobInfo jobInfo = JobInfo.newJobInfo("superId", "superType", clock, HOSTNAME); when(jobRepository.findOne("superId")).thenReturn(Optional.of(jobInfo)); jobService.killJob("superId"); JobInfo expected = jobInfo.copy().setStatus(JobInfo.JobStatus.DEAD).setStopped(now).setLastUpdated(now).build(); verify(jobMetaService).releaseRunLock("superType"); verify(jobRepository).createOrUpdate(expected); }
@Test public void shouldKeepAtLeastOneSuccessfulJob() { // given JobInfo job = builder() .setJobId("foobar") .setJobType("TYPE") .setStarted(now(muchEarlier)) .setStopped(now(now)) .setHostname("localhost") .setStatus(ERROR) .build(); JobRepository repository = new InMemJobRepository() {{ createOrUpdate(job.copy().setStatus(OK).build()); createOrUpdate(job.copy().setStarted(now(now)).setJobId("foo").build()); createOrUpdate(job.copy().setJobId("bar").setStarted(now(earlier)).build()); createOrUpdate(job.copy().setJobId("barzig").setStarted(now(evenEarlier)).build()); createOrUpdate(job.copy().setJobId("foozification").setStarted(now(evenEarlier)).build()); }}; KeepLastJobs strategy = new KeepLastJobs(repository, 2); // when strategy.doCleanUp(); // then assertThat(repository.size(), is(3L)); assertThat(repository.findOne("foo"), isPresent()); assertThat(repository.findOne("bar"), isPresent()); assertThat(repository.findOne("barzig"), isAbsent()); assertThat(repository.findOne("foobar"), isPresent()); assertThat(repository.findOne("foozification"), isAbsent()); }
@Test public void shouldKeepNJobsOfEachTypePresentAndNotRemoveRunningJobs() { // given JobInfo stoppedJob = builder() .setJobId("foo1") .setJobType("TYPE1") .setStarted(now(now)) .setStopped(now(now)) .setHostname("localhost") .setStatus(OK) .build(); JobRepository repository = new InMemJobRepository() {{ createOrUpdate(stoppedJob); createOrUpdate(stoppedJob.copy().setJobId("foo2").setStopped(now(muchEarlier)).setStarted(now(muchEarlier)).build()); createOrUpdate(stoppedJob.copy().setJobId("foo3").setStopped(now(evenEarlier)).setStarted(now(evenEarlier)).build()); createOrUpdate(stoppedJob.copy().setJobId("bar1").setJobType("TYPE2").setStopped(now(earlier)).setStarted(now(earlier)).build()); createOrUpdate(stoppedJob.copy().setJobId("bar2").setJobType("TYPE2").setStopped(now(muchEarlier)).setStarted(now(muchEarlier)).build()); createOrUpdate(stoppedJob.copy().setJobId("bar3").setJobType("TYPE2").setStopped(now(evenEarlier)).setStarted(now(evenEarlier)).build()); }}; KeepLastJobs strategy = new KeepLastJobs(repository, 2); // when strategy.doCleanUp(); // then assertThat(repository.size(), is(4L)); assertThat(repository.findByType("TYPE1"), hasSize(2)); assertThat(repository.findByType("TYPE2"), hasSize(2)); }
@Test public void shouldOnlyRemoveStoppedJobs() { // given JobInfo job = builder() .setJobId("foo") .setJobType("TYPE") .setHostname("localhost") .setStatus(SKIPPED) .build(); JobRepository repository = new InMemJobRepository() {{ createOrUpdate(job.copy().setStarted(now(now)).setStopped(now(now)).build()); createOrUpdate(job.copy().setStarted(now(earlier)).setJobId("foobar").setStarted(now(earlier)).build()); createOrUpdate(job.copy().setStarted(now(muchEarlier)).setJobId("bar").setStopped(now(now)).build()); }}; KeepLastJobs strategy = new KeepLastJobs(repository, 1); // when strategy.doCleanUp(); // then assertThat(repository.findOne("foo"), isPresent()); assertThat(repository.findOne("foobar"), isPresent()); assertThat(repository.findOne("bar"), isAbsent()); assertThat(repository.size(), is(2L)); } }
@Test public void shouldOnlyRemoveStoppedJobs() { // given JobInfo job = builder() .setJobId("foo") .setJobType("TYPE") .setHostname("localhost") .setStatus(OK) .build(); JobRepository repository = new InMemJobRepository() {{ createOrUpdate(job.copy().setStarted(now(now)).setStopped(now(now)).build()); createOrUpdate(job.copy().setStarted(now(earlier)).setJobId("foobar").setStarted(now(earlier)).build()); createOrUpdate(job.copy().setStarted(now(muchEarlier)).setJobId("bar").setStopped(now(now)).build()); }}; KeepLastJobs strategy = new KeepLastJobs(repository, 1); // when strategy.doCleanUp(); // then assertThat(repository.findOne("foo"), isPresent()); assertThat(repository.findOne("foobar"), isPresent()); assertThat(repository.findOne("bar"), isAbsent()); assertThat(repository.size(), is(2L)); }
@Test public void shouldRemoveOldestJobs() { // given JobInfo job = builder() .setJobId("foo") .setJobType("TYPE") .setStarted(now(now)) .setStopped(now(now)) .setHostname("localhost") .setStatus(OK) .build(); JobRepository repository = new InMemJobRepository() {{ createOrUpdate(job); createOrUpdate(job.copy().setJobId("foobar").setStarted(now(earlier)).build()); createOrUpdate(job.copy().setJobId("bar").setStarted(now(muchEarlier)).build()); }}; KeepLastJobs strategy = new KeepLastJobs(repository, 2); // when strategy.doCleanUp(); // then assertThat(repository.size(), is(2L)); assertThat(repository.findOne("bar"), isAbsent()); }
@Test public void shouldRemoveOldestJobs() { // given JobInfo job = builder() .setJobId("foo") .setJobType("TYPE") .setStarted(now(now)) .setStopped(now(now)) .setHostname("localhost") .setStatus(SKIPPED) .build(); JobRepository repository = new InMemJobRepository() {{ createOrUpdate(job); createOrUpdate(job.copy().setJobId("foobar").setStarted(now(earlier)).build()); createOrUpdate(job.copy().setJobId("bar").setStarted(now(muchEarlier)).build()); }}; KeepLastJobs strategy = new KeepLastJobs(repository, 2); // when strategy.doCleanUp(); // then assertThat(repository.size(), is(2L)); assertThat(repository.findOne("bar"), isAbsent()); }
@Test public void shouldBeOkToKeepAllJobs() { // given JobRepository repository = new InMemJobRepository() {{ createOrUpdate(builder() .setJobId("foo1") .setJobType("TYPE1") .setStarted(now(now)) .setStopped(now(now)) .setHostname("localhost") .setStatus(OK) .build()); }}; KeepLastJobs strategy = new KeepLastJobs(repository, 2); // when strategy.doCleanUp(); // then assertThat(repository.size(), is(1L)); } }
@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); }
@Test public void shouldClearJobInfos() throws Exception { //Given JobInfo stoppedJob = builder() .setJobId("some/job/stopped") .setJobType("test") .setStarted(now(fixed(Instant.now().minusSeconds(10), systemDefault()))) .setStopped(now(fixed(Instant.now().minusSeconds(7), systemDefault()))) .setHostname("localhost") .setStatus(JobStatus.OK) .build(); repository.createOrUpdate(stoppedJob); //When repository.deleteAll(); //Then assertThat(repository.findAll(), is(emptyList())); }
@Test public void shouldRemoveJob() throws Exception { JobInfo stoppedJob = builder() .setJobId("some/job/stopped") .setJobType("test") .setStarted(now(fixed(Instant.now().minusSeconds(10), systemDefault()))) .setStopped(now(fixed(Instant.now().minusSeconds(7), systemDefault()))) .setHostname("localhost") .setStatus(JobStatus.OK) .build(); repository.createOrUpdate(stoppedJob); repository.createOrUpdate(stoppedJob); repository.removeIfStopped(stoppedJob.getJobId()); assertThat(repository.size(), is(0L)); }
private void stopJob(final String jobId, final JobStatus status) { jobRepository.findOne(jobId).ifPresent((JobInfo jobInfo) -> { jobMetaService.releaseRunLock(jobInfo.getJobType()); final OffsetDateTime now = now(clock); final Builder builder = jobInfo.copy() .setStopped(now) .setLastUpdated(now); if (status != null) { builder.setStatus(status); } jobRepository.createOrUpdate(builder.build()); }); }
@Test public void shouldStopJob() { OffsetDateTime now = OffsetDateTime.now(clock); Clock earlierClock = offset(clock, Duration.of(-1, MINUTES)); JobInfo jobInfo = JobInfo.newJobInfo("superId", "superType", earlierClock, HOSTNAME); when(jobRepository.findOne("superId")).thenReturn(Optional.of(jobInfo)); jobService.stopJob("superId"); JobInfo expected = jobInfo.copy().setStatus(JobInfo.JobStatus.OK).setStopped(now).setLastUpdated(now).build(); verify(jobMetaService).releaseRunLock("superType"); verify(jobRepository).createOrUpdate(expected); }