Skip to content
Open
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 @@ -245,10 +245,13 @@ public JobExecution restart(JobExecution jobExecution) throws JobRestartExceptio
try {
return run(job, parameters);
}
catch (Exception e) {
catch (JobExecutionAlreadyRunningException e) {
throw new JobRestartException(
String.format(ILLEGAL_STATE_MSG, "job execution already running", jobName, parameters), e);
}
catch (JobInstanceAlreadyCompleteException | InvalidJobParametersException e) {
throw new JobRestartException(e.getMessage(), e);
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.batch.core.launch.support;

import java.time.LocalDateTime;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -127,4 +129,24 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
Assertions.assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
}

@Test
void testRestartFromUnknownStatusSurfacesRealReason() throws Exception {
JobParameters jobParameters = new JobParameters();
JobExecution jobExecution = jobOperator.start(job, jobParameters);

Assertions.assertNotNull(jobExecution);
Assertions.assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

// Simulate an execution that ended in UNKNOWN status.
jobExecution.setStatus(BatchStatus.UNKNOWN);
jobExecution.setEndTime(LocalDateTime.now());
jobRepository.update(jobExecution);

JobRestartException exception = Assertions.assertThrows(JobRestartException.class,
() -> jobOperator.restart(jobExecution));

Assertions.assertTrue(exception.getMessage().contains("Cannot restart job from UNKNOWN status"),
"Expected the UNKNOWN status reason to be surfaced but was: " + exception.getMessage());
}

}