Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide method for stopping Batch 5 Jobs upon user request #5990

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<module>spring-cloud-dataflow-shell</module>
<module>spring-cloud-dataflow-shell-core</module>
<module>spring-cloud-dataflow-completion</module>
<module>spring-cloud-skipper</module>
<!-- <module>spring-cloud-skipper</module>-->
onobc marked this conversation as resolved.
Show resolved Hide resolved
<module>spring-cloud-starter-dataflow-server</module>
<module>spring-cloud-starter-dataflow-ui</module>
<module>spring-cloud-dataflow-server</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ public int stopAll() {
Collection<JobExecution> result = jobExecutionDao.getRunningJobExecutions();
for (JobExecution jobExecution : result) {
try {
jobExecution.getStepExecutions().forEach(StepExecution::setTerminateOnly);
jobExecution.setStatus( BatchStatus.STOPPING);
jobRepository.update(jobExecution);
stopJobExecution(jobExecution);
} catch (Exception e) {
throw new IllegalArgumentException("The following JobExecutionId was not found: " + jobExecution.getId(), e);
}
Expand All @@ -238,14 +236,19 @@ public int stopAll() {

@Override
public JobExecution stop(Long jobExecutionId) throws NoSuchJobExecutionException, JobExecutionNotRunningException {
JobExecution jobExecution = getJobExecution(jobExecutionId);
return stopJobExecution(getJobExecution(jobExecutionId));
}

private JobExecution stopJobExecution(JobExecution jobExecution) throws JobExecutionNotRunningException{
if (!jobExecution.isRunning()) {
throw new JobExecutionNotRunningException("JobExecution is not running and therefore cannot be stopped");
}

logger.info("Stopping job execution: " + jobExecution);

jobExecution.setStatus(BatchStatus.STOPPED);
// Indicate the execution should be stopped by setting it's status to
// 'STOPPING'. It is assumed that
// the step implementation will check this status at chunk boundaries.
logger.info("Stopping job execution: {}", jobExecution);
jobExecution.getStepExecutions().forEach(StepExecution::setTerminateOnly);
jobExecution.setStatus(BatchStatus.STOPPING);
onobc marked this conversation as resolved.
Show resolved Hide resolved
jobRepository.update(jobExecution);
return jobExecution;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import javax.sql.DataSource;

import org.junit.jupiter.api.Test;
import org.springframework.batch.core.launch.JobExecutionNotRunningException;
import org.springframework.batch.core.launch.NoSuchJobExecutionException;
import org.testcontainers.containers.JdbcDatabaseContainer;

import org.springframework.batch.core.BatchStatus;
Expand Down Expand Up @@ -191,13 +193,25 @@ void exceptionsShouldBeThrownIfRequestForNonExistingJobInstance() {

@Test
void stoppingJobExecutionShouldLeaveJobExecutionWithStatusOfStopping() throws Exception {
JobExecution jobExecution = createJobExecution(BASE_JOB_INST_NAME,true);
jobExecution = jobService.getJobExecution(jobExecution.getId());
assertThat(jobExecution.isRunning()).isTrue();
assertThat(jobExecution.getStatus()).isNotEqualTo(BatchStatus.STOPPING);
JobExecution jobExecution = createRunningJobExecution(BASE_JOB_INST_NAME);
jobService.stop(jobExecution.getId());
assertJobHasStopped(jobExecution);
}

@Test
void stoppingAllJobExecutionsShouldLeaveJobExecutionsWithStatusOfStopping() throws Exception {
JobExecution jobExecutionOne = createRunningJobExecution(BASE_JOB_INST_NAME);
JobExecution jobExecutionTwo = createRunningJobExecution(BASE_JOB_INST_NAME+"_TWO");
jobService.stop(jobExecutionOne.getId());
assertJobHasStopped(jobExecutionOne);
jobService.stop(jobExecutionTwo.getId());
assertJobHasStopped(jobExecutionTwo);
}

private void assertJobHasStopped(JobExecution jobExecution) throws NoSuchJobExecutionException, JobExecutionNotRunningException {
jobExecution = jobService.getJobExecution(jobExecution.getId());
assertThat(jobExecution.getStatus()).isEqualTo(BatchStatus.STOPPED);
assertThat(jobExecution.getStatus()).isEqualTo(BatchStatus.STOPPING);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we wait for STOPPED as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

STOPPED only occurs once the batch job has completed. In this test we are just testing that SCDF sets the state to STOPPING. We are not running real jobs here, just creating / modifying the state of the JobExecution.

assertThat(jobExecution.isRunning()).isTrue();
}

private void verifyJobInstance(long id, String name) throws Exception {
Expand All @@ -221,9 +235,13 @@ private JobExecution createJobExecution(String name) throws Exception {
return createJobExecution(name, BatchStatus.STARTING, false);
}

private JobExecution createJobExecution(String name, boolean isRunning)
private JobExecution createRunningJobExecution(String name)
throws Exception {
return createJobExecution(name, BatchStatus.STARTING, isRunning);
JobExecution jobExecution = createJobExecution(name, BatchStatus.STARTING, true);
jobExecution = jobService.getJobExecution(jobExecution.getId());
assertThat(jobExecution.isRunning()).isTrue();
assertThat(jobExecution.getStatus()).isNotEqualTo(BatchStatus.STOPPING);
return jobExecution;
}

private JobExecution createJobExecution(String name, BatchStatus batchStatus, boolean isRunning) throws Exception {
Expand Down
Loading