Skip to content
This repository was archived by the owner on Sep 23, 2023. It is now read-only.

Commit f72b895

Browse files
authored
command delete along with the corresponding process at timeout (#35)
1 parent 56876da commit f72b895

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

src/main/java/com/github/estuaryoss/agent/api/CommandApiController.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.github.estuaryoss.agent.model.ProcessState;
1515
import com.github.estuaryoss.agent.model.YamlConfig;
1616
import com.github.estuaryoss.agent.model.api.ApiResponse;
17+
import com.github.estuaryoss.agent.model.api.CommandDescription;
1718
import com.github.estuaryoss.agent.utils.ProcessUtils;
1819
import com.github.estuaryoss.agent.utils.YamlConfigParser;
1920
import io.swagger.annotations.Api;
@@ -120,10 +121,18 @@ public ResponseEntity<ApiResponse> commandPost(@ApiParam(value = "Commands to ru
120121
.stream().map(elem -> elem.strip()).collect(Collectors.toList());
121122

122123
log.debug("Executing commands: " + commandsList.toString());
124+
CommandDescription commandDescription;
125+
try {
126+
commandDescription = commandRunner.runCommands(commandsList.toArray(new String[0]));
127+
} catch (Exception e) {
128+
throw new ApiException(ApiResponseCode.COMMAND_EXEC_FAILURE.getCode(),
129+
ApiResponseMessage.getMessage(ApiResponseCode.COMMAND_EXEC_FAILURE.getCode()));
130+
}
131+
123132
return new ResponseEntity<>(ApiResponse.builder()
124133
.code(ApiResponseCode.SUCCESS.getCode())
125134
.message(ApiResponseMessage.getMessage(ApiResponseCode.SUCCESS.getCode()))
126-
.description(commandRunner.runCommands(commandsList.toArray(new String[0])))
135+
.description(commandDescription)
127136
.name(about.getAppName())
128137
.version(about.getVersion())
129138
.timestamp(LocalDateTime.now().format(DateTimeConstants.PATTERN))
@@ -153,7 +162,13 @@ public ResponseEntity<ApiResponse> commandPostYaml(@ApiParam(value = "Commands t
153162

154163
log.debug("Executing commands: " + commandsList.toString());
155164
configDescriptor.setYamlConfig(yamlConfig);
156-
configDescriptor.setDescription(commandRunner.runCommands(commandsList.toArray(new String[0])));
165+
try {
166+
configDescriptor.setDescription(commandRunner.runCommands(commandsList.toArray(new String[0])));
167+
} catch (Exception e) {
168+
throw new ApiException(ApiResponseCode.COMMAND_EXEC_FAILURE.getCode(),
169+
ApiResponseMessage.getMessage(ApiResponseCode.COMMAND_EXEC_FAILURE.getCode()));
170+
}
171+
157172
return new ResponseEntity<>(ApiResponse.builder()
158173
.code(ApiResponseCode.SUCCESS.getCode())
159174
.message(ApiResponseMessage.getMessage(ApiResponseCode.SUCCESS.getCode()))

src/main/java/com/github/estuaryoss/agent/api/CommandDetachedApiController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public ResponseEntity<ApiResponse> commandDetachedIdPost(@ApiParam(value = "Comm
229229
log.debug("Sending args: " + argumentsList.toString());
230230
commandRunner.runStartCommandInBackground(argumentsList);
231231
backgroundStateHolder.setLastCommand(id);
232-
} catch (IOException e) {
232+
} catch (Exception e) {
233233
throw new ApiException(ApiResponseCode.COMMAND_START_FAILURE.getCode(),
234234
String.format(ApiResponseMessage.getMessage(ApiResponseCode.COMMAND_START_FAILURE.getCode()), id));
235235
}
@@ -287,7 +287,7 @@ public ResponseEntity<ApiResponse> commandDetachedIdPostYaml(@ApiParam(value = "
287287
log.debug("Sending args: " + argumentsList.toString());
288288
commandRunner.runStartCommandInBackground(argumentsList);
289289
backgroundStateHolder.setLastCommand(id);
290-
} catch (IOException e) {
290+
} catch (Exception e) {
291291
throw new ApiException(ApiResponseCode.COMMAND_START_FAILURE.getCode(),
292292
String.format(ApiResponseMessage.getMessage(ApiResponseCode.COMMAND_START_FAILURE.getCode()), id));
293293
}

src/main/java/com/github/estuaryoss/agent/api/CommandParallelApiController.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import com.github.estuaryoss.agent.constants.ApiResponseCode;
88
import com.github.estuaryoss.agent.constants.ApiResponseMessage;
99
import com.github.estuaryoss.agent.constants.DateTimeConstants;
10+
import com.github.estuaryoss.agent.exception.ApiException;
1011
import com.github.estuaryoss.agent.model.api.ApiResponse;
12+
import com.github.estuaryoss.agent.model.api.CommandDescription;
1113
import io.swagger.annotations.Api;
1214
import io.swagger.annotations.ApiParam;
1315
import org.slf4j.Logger;
@@ -57,10 +59,17 @@ public ResponseEntity<ApiResponse> commandPost(@ApiParam(value = "Commands to ru
5759
.stream().map(elem -> elem.stripLeading().stripTrailing()).collect(Collectors.toList());
5860

5961
log.debug("Executing commands: " + commandsList.toString());
62+
CommandDescription commandDescription;
63+
try {
64+
commandDescription = commandRunner.runCommandsParallel(commandsList.toArray(new String[0]));
65+
} catch (Exception e) {
66+
throw new ApiException(ApiResponseCode.COMMAND_EXEC_FAILURE.getCode(),
67+
ApiResponseMessage.getMessage(ApiResponseCode.COMMAND_EXEC_FAILURE.getCode()));
68+
}
6069
return new ResponseEntity<>(ApiResponse.builder()
6170
.code(ApiResponseCode.SUCCESS.getCode())
6271
.message(ApiResponseMessage.getMessage(ApiResponseCode.SUCCESS.getCode()))
63-
.description(commandRunner.runCommandsParallel(commandsList.toArray(new String[0])))
72+
.description(commandDescription)
6473
.name(about.getAppName())
6574
.version(about.getVersion())
6675
.timestamp(LocalDateTime.now().format(DateTimeConstants.PATTERN))

src/main/java/com/github/estuaryoss/agent/component/CommandRunner.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.github.estuaryoss.agent.model.api.CommandParallel;
1010
import com.github.estuaryoss.agent.model.api.CommandStatus;
1111
import com.github.estuaryoss.agent.utils.CommandStatusThread;
12+
import com.github.estuaryoss.agent.utils.ProcessUtils;
13+
import lombok.SneakyThrows;
1214
import org.apache.commons.io.IOUtils;
1315
import org.apache.commons.lang3.exception.ExceptionUtils;
1416
import org.slf4j.Logger;
@@ -216,6 +218,7 @@ public CommandDescription runCommandsParallel(String[] commands) throws IOExcept
216218
* @param processState A reference to a {@link ProcessState}
217219
* @return The command details of the command executed
218220
*/
221+
@SneakyThrows
219222
public CommandDetails getCmdDetailsOfProcess(String[] command, ProcessState processState) {
220223
CommandDetails commandDetails;
221224
InputStream inputStream = null;
@@ -244,6 +247,7 @@ public CommandDetails getCmdDetailsOfProcess(String[] command, ProcessState proc
244247
.code(DefaultConstants.PROCESS_EXCEPTION_TIMEOUT)
245248
.args(command)
246249
.build();
250+
ProcessUtils.killProcessAndChildren(processState);
247251
} catch (Exception e) {
248252
log.debug(ExceptionUtils.getStackTrace(e));
249253
commandDetails = CommandDetails.builder()

0 commit comments

Comments
 (0)