JobService { public Optional<String> startAsyncJob(String jobType) { try { final JobRunnable jobRunnable = findJobRunnable(jobType); final JobInfo jobInfo = createJobInfo(jobType); jobMetaService.aquireRunLock(jobInfo.getJobId(), jobInfo.getJobType()); jobRepository.createOrUpdate(jobInfo); return Optional.of(startAsync(metered(jobRunnable), jobInfo.getJobId())); } catch (JobBlockedException e) { LOG.info(e.getMessage()); return Optional.empty(); } } JobService();   JobService(final JobRepository jobRepository,
               final JobMetaService jobMetaService,
               final List<JobRunnable> jobRunnables,
               final ScheduledExecutorService executor,
               final ApplicationEventPublisher applicationEventPublisher,
               final Clock clock,
               final SystemInfo systemInfo,
               final UuidProvider uuidProvider); @PostConstruct void postConstruct(); Optional<String> startAsyncJob(String jobType); Optional<JobInfo> findJob(final String id); List<JobInfo> findJobs(final Optional<String> type, final int count); List<JobInfo> findJobsDistinct(); void deleteJobs(final Optional<String> type); void stopJob(final String jobId); void killJobsDeadSince(final int seconds); void killJob(final String jobId); void appendMessage(final String jobId,
                              final JobMessage jobMessage); void keepAlive(final String jobId); void markSkipped(final String jobId); void markRestarted(final String jobId); void handleTooBigJobLogs();  }
@Test public void shouldReturnCreatedJobId() { when(jobRunnable.getJobDefinition()).thenReturn(someJobDefinition("BAR")); Optional<String> jobId = jobService.startAsyncJob("BAR"); assertThat(jobId.isPresent(), is(true)); assertThat(jobId.get(), not(isEmptyOrNullString())); }
@Test public void shouldRunJob() { String jobType = "bar"; when(jobRunnable.getJobDefinition()).thenReturn(someJobDefinition(jobType)); Optional<String> optionalJobId = jobService.startAsyncJob(jobType); final JobInfo expectedJobInfo = JobInfo.newJobInfo(optionalJobId.get(), jobType, clock, systemInfo.hostname); verify(executorService).execute(any(Runnable.class)); verify(jobRepository).createOrUpdate(expectedJobInfo); verify(jobRunnable).execute(any(JobEventPublisher.class)); verify(jobMetaService).aquireRunLock(expectedJobInfo.getJobId(), expectedJobInfo.getJobType()); }
@Test public void shouldNotStartJobOnBlockedException() { doAnswer((x) -> {throw new JobBlockedException("");}) .when(jobMetaService).aquireRunLock(anyString(), anyString()); Optional<String> jobUri = jobService.startAsyncJob("someType"); assertThat(jobUri.isPresent(), is(false)); verify(jobRepository, never()).createOrUpdate(any()); }