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

Commit

Permalink
command delete along with the corresponding process at timeout (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinuta committed Mar 24, 2021
1 parent 56876da commit f72b895
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.github.estuaryoss.agent.model.ProcessState;
import com.github.estuaryoss.agent.model.YamlConfig;
import com.github.estuaryoss.agent.model.api.ApiResponse;
import com.github.estuaryoss.agent.model.api.CommandDescription;
import com.github.estuaryoss.agent.utils.ProcessUtils;
import com.github.estuaryoss.agent.utils.YamlConfigParser;
import io.swagger.annotations.Api;
Expand Down Expand Up @@ -120,10 +121,18 @@ public ResponseEntity<ApiResponse> commandPost(@ApiParam(value = "Commands to ru
.stream().map(elem -> elem.strip()).collect(Collectors.toList());

log.debug("Executing commands: " + commandsList.toString());
CommandDescription commandDescription;
try {
commandDescription = commandRunner.runCommands(commandsList.toArray(new String[0]));
} catch (Exception e) {
throw new ApiException(ApiResponseCode.COMMAND_EXEC_FAILURE.getCode(),
ApiResponseMessage.getMessage(ApiResponseCode.COMMAND_EXEC_FAILURE.getCode()));
}

return new ResponseEntity<>(ApiResponse.builder()
.code(ApiResponseCode.SUCCESS.getCode())
.message(ApiResponseMessage.getMessage(ApiResponseCode.SUCCESS.getCode()))
.description(commandRunner.runCommands(commandsList.toArray(new String[0])))
.description(commandDescription)
.name(about.getAppName())
.version(about.getVersion())
.timestamp(LocalDateTime.now().format(DateTimeConstants.PATTERN))
Expand Down Expand Up @@ -153,7 +162,13 @@ public ResponseEntity<ApiResponse> commandPostYaml(@ApiParam(value = "Commands t

log.debug("Executing commands: " + commandsList.toString());
configDescriptor.setYamlConfig(yamlConfig);
configDescriptor.setDescription(commandRunner.runCommands(commandsList.toArray(new String[0])));
try {
configDescriptor.setDescription(commandRunner.runCommands(commandsList.toArray(new String[0])));
} catch (Exception e) {
throw new ApiException(ApiResponseCode.COMMAND_EXEC_FAILURE.getCode(),
ApiResponseMessage.getMessage(ApiResponseCode.COMMAND_EXEC_FAILURE.getCode()));
}

return new ResponseEntity<>(ApiResponse.builder()
.code(ApiResponseCode.SUCCESS.getCode())
.message(ApiResponseMessage.getMessage(ApiResponseCode.SUCCESS.getCode()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public ResponseEntity<ApiResponse> commandDetachedIdPost(@ApiParam(value = "Comm
log.debug("Sending args: " + argumentsList.toString());
commandRunner.runStartCommandInBackground(argumentsList);
backgroundStateHolder.setLastCommand(id);
} catch (IOException e) {
} catch (Exception e) {
throw new ApiException(ApiResponseCode.COMMAND_START_FAILURE.getCode(),
String.format(ApiResponseMessage.getMessage(ApiResponseCode.COMMAND_START_FAILURE.getCode()), id));
}
Expand Down Expand Up @@ -287,7 +287,7 @@ public ResponseEntity<ApiResponse> commandDetachedIdPostYaml(@ApiParam(value = "
log.debug("Sending args: " + argumentsList.toString());
commandRunner.runStartCommandInBackground(argumentsList);
backgroundStateHolder.setLastCommand(id);
} catch (IOException e) {
} catch (Exception e) {
throw new ApiException(ApiResponseCode.COMMAND_START_FAILURE.getCode(),
String.format(ApiResponseMessage.getMessage(ApiResponseCode.COMMAND_START_FAILURE.getCode()), id));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.github.estuaryoss.agent.constants.ApiResponseCode;
import com.github.estuaryoss.agent.constants.ApiResponseMessage;
import com.github.estuaryoss.agent.constants.DateTimeConstants;
import com.github.estuaryoss.agent.exception.ApiException;
import com.github.estuaryoss.agent.model.api.ApiResponse;
import com.github.estuaryoss.agent.model.api.CommandDescription;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
Expand Down Expand Up @@ -57,10 +59,17 @@ public ResponseEntity<ApiResponse> commandPost(@ApiParam(value = "Commands to ru
.stream().map(elem -> elem.stripLeading().stripTrailing()).collect(Collectors.toList());

log.debug("Executing commands: " + commandsList.toString());
CommandDescription commandDescription;
try {
commandDescription = commandRunner.runCommandsParallel(commandsList.toArray(new String[0]));
} catch (Exception e) {
throw new ApiException(ApiResponseCode.COMMAND_EXEC_FAILURE.getCode(),
ApiResponseMessage.getMessage(ApiResponseCode.COMMAND_EXEC_FAILURE.getCode()));
}
return new ResponseEntity<>(ApiResponse.builder()
.code(ApiResponseCode.SUCCESS.getCode())
.message(ApiResponseMessage.getMessage(ApiResponseCode.SUCCESS.getCode()))
.description(commandRunner.runCommandsParallel(commandsList.toArray(new String[0])))
.description(commandDescription)
.name(about.getAppName())
.version(about.getVersion())
.timestamp(LocalDateTime.now().format(DateTimeConstants.PATTERN))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.github.estuaryoss.agent.model.api.CommandParallel;
import com.github.estuaryoss.agent.model.api.CommandStatus;
import com.github.estuaryoss.agent.utils.CommandStatusThread;
import com.github.estuaryoss.agent.utils.ProcessUtils;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -216,6 +218,7 @@ public CommandDescription runCommandsParallel(String[] commands) throws IOExcept
* @param processState A reference to a {@link ProcessState}
* @return The command details of the command executed
*/
@SneakyThrows
public CommandDetails getCmdDetailsOfProcess(String[] command, ProcessState processState) {
CommandDetails commandDetails;
InputStream inputStream = null;
Expand Down Expand Up @@ -244,6 +247,7 @@ public CommandDetails getCmdDetailsOfProcess(String[] command, ProcessState proc
.code(DefaultConstants.PROCESS_EXCEPTION_TIMEOUT)
.args(command)
.build();
ProcessUtils.killProcessAndChildren(processState);
} catch (Exception e) {
log.debug(ExceptionUtils.getStackTrace(e));
commandDetails = CommandDetails.builder()
Expand Down

0 comments on commit f72b895

Please sign in to comment.