Skip to content

Commit 692e769

Browse files
injae-kimfmbenhassine
authored andcommitted
Fix SystemCommandTasklet to propagate error when exit status is failed
Fixes #4483
1 parent d779cad commit 692e769

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/SystemCommandTasklet.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,6 +60,7 @@
6060
* @author Robert Kasanicky
6161
* @author Will Schipp
6262
* @author Mahmoud Ben Hassine
63+
* @author Injae Kim
6364
*/
6465
public class SystemCommandTasklet implements StepExecutionListener, StoppableTasklet, InitializingBean {
6566

@@ -121,8 +122,15 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
121122
}
122123

123124
if (systemCommandTask.isDone()) {
124-
contribution.setExitStatus(systemProcessExitCodeMapper.getExitStatus(systemCommandTask.get()));
125-
return RepeatStatus.FINISHED;
125+
Integer exitCode = systemCommandTask.get();
126+
ExitStatus exitStatus = systemProcessExitCodeMapper.getExitStatus(exitCode);
127+
contribution.setExitStatus(exitStatus);
128+
if (ExitStatus.FAILED.equals(exitStatus)) {
129+
throw new SystemCommandException("Execution of system command failed with exit code " + exitCode);
130+
}
131+
else {
132+
return RepeatStatus.FINISHED;
133+
}
126134
}
127135
else if (System.currentTimeMillis() - t0 > timeout) {
128136
systemCommandTask.cancel(interruptOnCancel);

spring-batch-core/src/test/java/org/springframework/batch/core/step/tasklet/SystemCommandTaskletIntegrationTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2023 the original author or authors.
2+
* Copyright 2008-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -323,9 +323,8 @@ public void testExecuteWithFailedCommandRunnerMockExecution() throws Exception {
323323
tasklet.setCommand(command);
324324
tasklet.afterPropertiesSet();
325325

326-
RepeatStatus exitStatus = tasklet.execute(stepContribution, null);
327-
328-
assertEquals(RepeatStatus.FINISHED, exitStatus);
326+
Exception exception = assertThrows(SystemCommandException.class, () -> tasklet.execute(stepContribution, null));
327+
assertTrue(exception.getMessage().contains("failed with exit code"));
329328
assertEquals(ExitStatus.FAILED, stepContribution.getExitStatus());
330329
}
331330

0 commit comments

Comments
 (0)