Fix SystemCommandTasklet to propagate error when exit status is failed
Fixes #4483
This commit is contained in:
committed by
Mahmoud Ben Hassine
parent
d779cadcc6
commit
692e7699c2
@@ -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.
|
||||
@@ -60,6 +60,7 @@ import org.springframework.util.StringUtils;
|
||||
* @author Robert Kasanicky
|
||||
* @author Will Schipp
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @author Injae Kim
|
||||
*/
|
||||
public class SystemCommandTasklet implements StepExecutionListener, StoppableTasklet, InitializingBean {
|
||||
|
||||
@@ -121,8 +122,15 @@ public class SystemCommandTasklet implements StepExecutionListener, StoppableTas
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
@@ -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.
|
||||
@@ -323,9 +323,8 @@ class SystemCommandTaskletIntegrationTests {
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user