Skip to content

Commit

Permalink
Fix SystemCommandTasklet to propagate error when exit status is failed
Browse files Browse the repository at this point in the history
Fixes #4483
  • Loading branch information
injae-kim authored and fmbenhassine committed Mar 15, 2024
1 parent 5b4685e commit 899343a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -60,6 +60,7 @@
* @author Robert Kasanicky
* @author Will Schipp
* @author Mahmoud Ben Hassine
* @author Injae Kim
*/
public class SystemCommandTasklet implements StepExecutionListener, StoppableTasklet, InitializingBean {

Expand Down Expand Up @@ -121,8 +122,15 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
}

if (systemCommandTask.isDone()) {
contribution.setExitStatus(systemProcessExitCodeMapper.getExitStatus(systemCommandTask.get()));
return RepeatStatus.FINISHED;
Integer exitCode = systemCommandTask.get();
ExitStatus exitStatus = systemProcessExitCodeMapper.getExitStatus(exitCode);
contribution.setExitStatus(exitStatus);
if (ExitStatus.FAILED.equals(exitStatus)) {
throw new SystemCommandException("Execution of system command failed with exit code " + exitCode);
}
else {
return RepeatStatus.FINISHED;
}
}
else if (System.currentTimeMillis() - t0 > timeout) {
systemCommandTask.cancel(interruptOnCancel);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2023 the original author or authors.
* Copyright 2008-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -323,9 +323,8 @@ public void testExecuteWithFailedCommandRunnerMockExecution() throws Exception {
tasklet.setCommand(command);
tasklet.afterPropertiesSet();

RepeatStatus exitStatus = tasklet.execute(stepContribution, null);

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

Expand Down

0 comments on commit 899343a

Please sign in to comment.