Skip to content

Avoid duplicated job execution when an execution is waiting to start #4100

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ public StepExecution createStepExecution(String stepName) {
return stepExecution;
}

/**
* Test if this {@link JobExecution} indicates that it is waiting to start.
* @return true if the status is {@link BatchStatus#STARTING}.
*/
public boolean isStarting() {
return status == BatchStatus.STARTING;
}

/**
* Test if this {@link JobExecution} indicates that it is running.
* Note that this does not necessarily mean that it has been persisted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public JobExecution createJobExecution(String jobName, JobParameters jobParamete

// check for running executions and find the last started
for (JobExecution execution : executions) {
if (execution.isRunning() || execution.isStopping()) {
if (execution.isStarting() || execution.isRunning() || execution.isStopping()) {
throw new JobExecutionAlreadyRunningException("A job execution for this job is already running: "
+ jobInstance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,25 @@ public void testSaveExecutionContext() throws Exception {
// assertEquals(ctx, retrievedJobExec.getExecutionContext());
}

/*
* If JobExecution is waiting to start, exception will be thrown in attempt
* to create new execution.
*/
@Transactional
@Test
public void testStartingJobRejectsAnotherJobExecution() throws Exception {
job.setRestartable(true);
JobExecution jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);

try {
jobRepository.createJobExecution(job.getName(), jobParameters);
fail();
}
catch (JobExecutionAlreadyRunningException e) {
// expected
}
}

/*
* If JobExecution is already running, exception will be thrown in attempt
* to create new execution.
Expand Down