JobBuildService { @Transactional public void stop(Build build) { List<JobBuild> jobBuilds = jobBuildDao.findByBuild(build); jobBuilds.stream() .filter(jobBuild -> jobBuild.getStatus().equals(JobBuild.Status.IN_PROGRESS)) .forEach(jobBuild -> { jobBuild.setStatus(JobBuild.Status.STOPPED); workerService.readyWorker(Optional.of(jobBuild.getWorker())); jobBuild.setWorker(null); jobBuildDao.save(jobBuild); }); jobBuilds.stream() .filter(jobBuild -> jobBuild.getStatus().equals(JobBuild.Status.READY)) .forEach(jobBuild -> { jobBuild.setStatus(JobBuild.Status.STOPPED); workerService.readyWorker(Optional.ofNullable(jobBuild.getWorker())); jobBuild.setWorker(null); jobBuildDao.save(jobBuild); }); updateBuildStatus(build); }  @Transactional void prepareJobs(Build build, Dickfile dickfile); @Transactional void buildStage(Build build, Dickfile dickfile, Stage stage); @Transactional Optional<BuildOrder> peekBuildFor(String workerName); boolean isStopped(Long id); @Transactional void reportProgress(Long id, String partialLog); @Transactional void reportFailure(Long id, String fullLog); @Transactional void reportSuccess(Long id, String fullLog); Build.Status updateBuildStatus(Build build); @Transactional void stop(Build build); @Transactional List<LogChunkModel> getLogChunks(Long id, Date creationDate); @Transactional OutputModel getOutput(Long id); @Transactional void deleteJobBuilds(Build build);  }
@Test public void shouldStopBuild() { Worker worker = produceWorker(Worker.Status.BUSY); Build build = produceBuild(); JobBuild jobBuild = produceJobBuild(worker, build, JobBuild.Status.IN_PROGRESS); jobBuildService.stop(build); jobBuild = jobBuildDao.findOne(jobBuild.getId()); worker = workerDao.findByName(jobBuild.getWorkerName()).get(); assertThat(jobBuild.getStatus()).isEqualTo(JobBuild.Status.STOPPED); assertThat(jobBuild.getWorker()).isNull(); assertThat(worker.getStatus()).isEqualTo(Worker.Status.READY); assertThat(jobBuild.getBuild().getStatus()).isEqualTo(Build.Status.STOPPED); }