/** * Returns the current state of the specified job type. * * @param jobType the job type * @return current state of the job type */ @Override public JobMeta getJobMeta(String jobType) { final Map<String, String> document = map.get(jobType); if (document != null) { final Map<String, String> meta = document.keySet() .stream() .filter(key -> !key.startsWith("_e_")) .collect(toMap( key -> key, document::get )); final boolean isRunning = document.containsKey(KEY_RUNNING); final boolean isDisabled = document.containsKey(KEY_DISABLED); final String comment = document.get(KEY_DISABLED); return new JobMeta(jobType, isRunning, isDisabled, comment, meta); } else { return new JobMeta(jobType, false, false, "", emptyMap()); } }
@ParameterizedTest @MethodSource("data") public void shouldGetEmptyJobMeta(final JobMetaRepository testee) { testee.deleteAll(); final JobMeta jobMeta = testee.getJobMeta("someJob"); assertThat(jobMeta.getAll(), is(emptyMap())); assertThat(jobMeta.isDisabled(), is(false)); assertThat(jobMeta.getDisabledComment(), is("")); assertThat(jobMeta.isRunning(), is(false)); assertThat(jobMeta.getJobType(), is("someJob")); }
final JobMeta jobMeta = getJobMeta(jobDefinition.jobType()); long numFailedJobs = getNumFailedJobs(jobInfos); if (!jobMeta.isDisabled()) { switch(lastJob.getStatus()) { case OK: message = format(JOB_DEACTIVATED_MESSAGE, jobMeta.getDisabledComment());
public boolean getIsDisabled() { return jobMeta != null && jobMeta.isDisabled(); }
public String getComment() { return jobMeta != null ? jobMeta.getDisabledComment() : ""; }
if (jobMeta.isDisabled()) { throw new JobBlockedException(format("Job '%s' is currently disabled", jobType));
@Override public JobMeta getJobMeta(final String jobType) { final Document document = collection .find(eq(ID, jobType)) .maxTime(mongoProperties.getDefaultReadTimeout(), TimeUnit.MILLISECONDS) .first(); if (document != null) { final Map<String, String> meta = document.keySet() .stream() .filter(key -> !key.startsWith("_e_") && !key.equals(ID)) .collect(toMap( key -> key, document::getString )); final boolean isRunning = document.containsKey(KEY_RUNNING); final boolean isDisabled = document.containsKey(KEY_DISABLED); final String comment = document.getString(KEY_DISABLED); return new JobMeta(jobType, isRunning, isDisabled, comment, meta); } else { return new JobMeta(jobType, false, false, "", emptyMap()); } }
@ParameterizedTest @MethodSource("data") public void shouldGetJobMetaForRunningJob(final JobMetaRepository testee) { testee.deleteAll(); testee.setRunningJob("someJob", "someId"); final JobMeta jobMeta = testee.getJobMeta("someJob"); assertThat(jobMeta.getAll(), is(emptyMap())); assertThat(jobMeta.isDisabled(), is(false)); assertThat(jobMeta.getDisabledComment(), is("")); assertThat(jobMeta.isRunning(), is(true)); assertThat(jobMeta.getJobType(), is("someJob")); }
@Override public JobMeta getJobMeta(final String jobType) { final Document document = collection .find(eq(ID, jobType)) .maxTime(mongoProperties.getDefaultReadTimeout(), TimeUnit.MILLISECONDS) .first(); if (document != null) { final Map<String, String> meta = document.keySet() .stream() .filter(key -> !key.startsWith("_e_") && !key.equals(ID)) .collect(toMap( key -> key, document::getString )); final boolean isRunning = document.containsKey(KEY_RUNNING); final boolean isDisabled = document.containsKey(KEY_DISABLED); final String comment = document.getString(KEY_DISABLED); return new JobMeta(jobType, isRunning, isDisabled, comment, meta); } else { return new JobMeta(jobType, false, false, "", emptyMap()); } }
@ParameterizedTest @MethodSource("data") public void shouldGetJobMetaForDisabledJob(final JobMetaRepository testee) { testee.deleteAll(); testee.disable("someJob", "some comment"); final JobMeta jobMeta = testee.getJobMeta("someJob"); assertThat(jobMeta.getAll(), is(emptyMap())); assertThat(jobMeta.isDisabled(), is(true)); assertThat(jobMeta.getDisabledComment(), is("some comment")); assertThat(jobMeta.isRunning(), is(false)); assertThat(jobMeta.getJobType(), is("someJob")); }
@Test @SuppressWarnings("unchecked") public void shouldIndicateErrorIfJobCouldNotBeRetievedFromRepository() { // given when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenThrow(RuntimeException.class); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail statusDetail = errorOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(statusDetail.getStatus(), is(Status.ERROR)); }
@ParameterizedTest @MethodSource("data") public void shouldGetJobMetaForDisabledJobWithProperties(final JobMetaRepository testee) { testee.deleteAll(); testee.disable("someJob", "some comment"); testee.setValue("someJob", "someKey", "some value"); final JobMeta jobMeta = testee.getJobMeta("someJob"); assertThat(jobMeta.getAll(), is(singletonMap("someKey", "some value"))); assertThat(jobMeta.isDisabled(), is(true)); assertThat(jobMeta.getDisabledComment(), is("some comment")); assertThat(jobMeta.isRunning(), is(false)); assertThat(jobMeta.getJobType(), is("someJob")); }
@Test public void shouldNotHaveUriOrRunningIfNoJobPresent() { // given when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(emptyList()); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail statusDetail = errorOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(statusDetail.getLinks(), is(emptyList())); assertThat(statusDetail.getDetails(), not(hasKey("Running"))); }
@Test public void shouldIndicateWarningIfJobRunWasDead() { // given final List<JobInfo> jobInfos = singletonList(someRunningJob(DEAD, 1)); when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(jobInfos); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail statusDetail = errorOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(statusDetail.getStatus(), is(Status.WARNING)); assertThat(statusDetail.getMessage(), is("Job died")); }
@Test public void shouldAquireRunLock() throws Exception { when(jobMetaRepository.findAllJobTypes()).thenReturn(emptySet()); when(jobMetaRepository.setRunningJob("myJobType", "jobId")).thenReturn(true); when(jobMetaRepository.getJobMeta("myJobType")).thenReturn(new JobMeta("myJobType", false, false, "", emptyMap())); jobMetaService.aquireRunLock("jobId", "myJobType"); verify(jobMetaRepository).setRunningJob("myJobType", "jobId"); }
@Test public void shouldIndicateWarningIfLastJobRunWasDead() { // given final List<JobInfo> jobInfos = singletonList(someStoppedJob(DEAD, 1)); when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(jobInfos); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail statusDetail = errorOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(statusDetail.getStatus(), is(Status.WARNING)); assertThat(statusDetail.getMessage(), is("Job died")); }
@Test public void shouldNotStartJobIfBlockedByAnotherJob() throws Exception { // given when(jobMetaRepository.findAllJobTypes()).thenReturn(new HashSet<>(asList("job1", "job2"))); when(jobMetaRepository.getRunningJob("job1")).thenReturn("42"); when(jobMetaRepository.getJobMeta("job2")).thenReturn(new JobMeta("job2", false, false, "", emptyMap())); when(jobMetaRepository.setRunningJob("job2", "first")).thenReturn(true); when(jobMutexGroups.mutexJobTypesFor("job2")).thenReturn(new HashSet<>(asList("job1", "job2"))); // when // then assertThrows(JobBlockedException.class, () -> jobMetaService.aquireRunLock("first", "job2")); verify(jobMetaRepository).clearRunningJob("job2"); }
@Test public void shouldNotAquireLockIfAlreadyRunning() throws Exception { // given when(jobMetaRepository.findAllJobTypes()).thenReturn(emptySet()); when(jobMetaRepository.getJobMeta("myJobType")).thenReturn(new JobMeta("myJobType", true, false, "", emptyMap())); when(jobMetaRepository.setRunningJob("myJobType", "someId")).thenReturn(false); // when assertThrows(JobBlockedException.class, () -> jobMetaService.aquireRunLock("jobId", "myJobType")); }
@Test public void shouldNotStartADisabledJob() { // given when(jobMetaRepository.findAllJobTypes()).thenReturn(singleton("jobType")); when(jobMetaRepository.getJobMeta("jobType")).thenReturn( new JobMeta("jobType", false, true, "", emptyMap()) ); // when // then assertThrows(JobBlockedException.class, () -> jobMetaService.aquireRunLock("someId", "jobType"), "Job 'jobType' is currently disabled"); }
@Test public void shouldReturnStatusDetailWithJobLink() { // given final JobInfo jobInfo = someStoppedJob(OK, 1); final List<JobInfo> jobs = singletonList(jobInfo); when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(jobs); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail first = errorOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(first.getLinks().size(), is(1)); assertThat(first.getLinks().get(0).href, is("/someInternalPath/jobs/" + jobInfo.getJobId())); }