openvidu-server: parameterized wait timeout for CommandExecutor

pull/494/head
pabloFuente 2020-06-05 11:00:15 +02:00
parent 9ad21624d3
commit ac81abf7fd
6 changed files with 27 additions and 28 deletions

View File

@ -215,7 +215,7 @@ public class OpenViduServer implements JsonRpcConfigurer {
}
public static String getContainerIp() throws IOException, InterruptedException {
return CommandExecutor.execCommand("/bin/sh", "-c", "hostname -i | awk '{print $1}'");
return CommandExecutor.execCommand(5000, "/bin/sh", "-c", "hostname -i | awk '{print $1}'");
}
public static void main(String[] args) throws Exception {

View File

@ -35,7 +35,7 @@ public class BashCoturnCredentialsService extends CoturnCredentialsService {
@PostConstruct
private void initialize() {
try {
String response = CommandExecutor.execCommand("/bin/sh", "-c",
String response = CommandExecutor.execCommand(10000, "/bin/sh", "-c",
"turnadmin -l -N " + this.coturnDatabaseString);
if (response.contains("turnadmin: not found")) {
// No coturn installed in the host machine
@ -54,10 +54,10 @@ public class BashCoturnCredentialsService extends CoturnCredentialsService {
this.logPath = logFile.substring(0, logFile.lastIndexOf('/') + 1);
log.info("Path of COTURN log files: " + this.logPath);
}
response = CommandExecutor.execCommand("/bin/sh", "-c",
response = CommandExecutor.execCommand(10000, "/bin/sh", "-c",
"redis-cli -a " + this.openviduConfig.getCoturnDatabasePassword() + " -n "
+ this.openviduConfig.getCoturnDatabaseDbname() + " flushdb");
String response2 = CommandExecutor.execCommand("/bin/sh", "-c",
String response2 = CommandExecutor.execCommand(10000, "/bin/sh", "-c",
"redis-cli -a " + this.openviduConfig.getCoturnDatabasePassword() + " -n "
+ this.openviduConfig.getCoturnDatabaseDbname() + " --scan --pattern '*'");
if ("OK".equals(response) && response2.isEmpty()) {
@ -80,7 +80,7 @@ public class BashCoturnCredentialsService extends CoturnCredentialsService {
String user = RandomStringUtils.randomAlphanumeric(6).toUpperCase();
String pass = RandomStringUtils.randomAlphanumeric(6).toLowerCase();
String command = "turnadmin -a -u " + user + " -r openvidu -p " + pass + " -N " + this.coturnDatabaseString;
String response = CommandExecutor.execCommand("/bin/sh", "-c", command);
String response = CommandExecutor.execCommand(10000, "/bin/sh", "-c", command);
if (response.contains("connection success: " + this.trimmedCoturnDatabaseString)) {
credentials = new TurnCredentials(user, pass);
this.cleanTurnLogFiles();
@ -99,7 +99,7 @@ public class BashCoturnCredentialsService extends CoturnCredentialsService {
String command = "turnadmin -d -u " + user + " -r openvidu -N " + this.coturnDatabaseString;
String response = "";
try {
response = CommandExecutor.execCommand("/bin/sh", "-c", command);
response = CommandExecutor.execCommand(10000, "/bin/sh", "-c", command);
this.cleanTurnLogFiles();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
@ -111,7 +111,7 @@ public class BashCoturnCredentialsService extends CoturnCredentialsService {
private void cleanTurnLogFiles() throws IOException, InterruptedException {
if (this.logCounter.incrementAndGet() > LOG_LIMIT) {
CommandExecutor.execCommand("/bin/sh", "-c", "rm " + this.logPath + "turn_*.log");
CommandExecutor.execCommand(10000, "/bin/sh", "-c", "rm " + this.logPath + "turn_*.log");
log.info("Garbage collector cleaning turn log files at path " + this.logPath);
this.logCounter.set(0);
}

View File

@ -193,7 +193,7 @@ public class PublisherEndpoint extends MediaEndpoint {
public synchronized String publish(SdpType sdpType, String sdpString, boolean doLoopback) {
registerOnIceCandidateEventListener(this.getOwner().getParticipantPublicId());
if (doLoopback) {
connect(this.getEndpoint(), null);
connect(this.getEndpoint());
} else {
innerConnect();
}

View File

@ -160,7 +160,7 @@ public abstract class RecordingService {
protected void updateFilePermissions(String folder) {
String command = "chmod -R 777 " + folder;
try {
String response = CommandExecutor.execCommand("/bin/sh", "-c", command);
String response = CommandExecutor.execCommand(5000, "/bin/sh", "-c", command);
if ("".equals(response)) {
log.info("KMS recording file permissions successfully updated");
} else {

View File

@ -34,34 +34,33 @@ public class CommandExecutor {
private static final Logger log = LoggerFactory.getLogger(CommandExecutor.class);
private static final long MILLIS_TIMEOUT = 10000;
public static String execCommand(String... command) throws IOException, InterruptedException {
public static String execCommand(long msTimeout, String... command) throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.redirectErrorStream(true);
return commonExecCommand(processBuilder);
return commonExecCommand(msTimeout, processBuilder);
}
public static String execCommandRedirectError(File errorOutputFile, String... command)
public static String execCommandRedirectError(long msTimeout, File errorOutputFile, String... command)
throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder(command).redirectError(errorOutputFile);
return commonExecCommand(processBuilder);
return commonExecCommand(msTimeout, processBuilder);
}
public static void execCommandRedirectStandardOutputAndError(File standardOutputFile, File errorOutputFile,
String... command) throws IOException, InterruptedException {
public static void execCommandRedirectStandardOutputAndError(long msTimeout, File standardOutputFile,
File errorOutputFile, String... command) throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder(command).redirectOutput(standardOutputFile)
.redirectError(errorOutputFile);
Process process = processBuilder.start();
if (!process.waitFor(MILLIS_TIMEOUT, TimeUnit.MILLISECONDS)) {
log.error("Command {} did not receive a response in {} ms", Arrays.toString(command), MILLIS_TIMEOUT);
if (!process.waitFor(msTimeout, TimeUnit.MILLISECONDS)) {
log.error("Command {} did not receive a response in {} ms", Arrays.toString(command), msTimeout);
String errorMsg = "Current process status of host:\n" + gatherLinuxHostInformation();
log.error(errorMsg);
throw new IOException(errorMsg);
}
}
private static String commonExecCommand(ProcessBuilder processBuilder) throws IOException, InterruptedException {
private static String commonExecCommand(long msTimeout, ProcessBuilder processBuilder)
throws IOException, InterruptedException {
Process process = processBuilder.start();
StringBuilder processOutput = new StringBuilder();
String output;
@ -75,9 +74,9 @@ public class CommandExecutor {
processOutput.append(readLine + System.lineSeparator());
}
if (!process.waitFor(MILLIS_TIMEOUT, TimeUnit.MILLISECONDS)) {
if (!process.waitFor(msTimeout, TimeUnit.MILLISECONDS)) {
log.error("Command {} did not receive a response in {} ms",
Arrays.toString(processBuilder.command().toArray()), MILLIS_TIMEOUT);
Arrays.toString(processBuilder.command().toArray()), msTimeout);
String errorMsg = "Current process status of host:\n" + gatherLinuxHostInformation();
log.error(errorMsg);
throw new IOException(errorMsg);
@ -96,7 +95,7 @@ public class CommandExecutor {
public static String gatherLinuxHostInformation() throws IOException, InterruptedException {
final String psCommand = "ps -eo pid,ppid,user,%mem,%cpu,cmd --sort=-%cpu";
return execCommand("/bin/sh", "-c", psCommand);
return execCommand(5000, "/bin/sh", "-c", psCommand);
}
}

View File

@ -103,8 +103,8 @@ public class DockerManager {
}
}
public String runContainer(String container, String containerName, String user, List<Volume> volumes, List<Bind> binds,
String networkMode, List<String> envs, List<String> command) throws Exception {
public String runContainer(String container, String containerName, String user, List<Volume> volumes,
List<Bind> binds, String networkMode, List<String> envs, List<String> command) throws Exception {
CreateContainerCmd cmd = dockerClient.createContainerCmd(container).withEnv(envs);
if (containerName != null) {
@ -194,7 +194,7 @@ public class DockerManager {
public String getContainerIp(String containerId) {
try {
return CommandExecutor.execCommand("/bin/sh", "-c",
return CommandExecutor.execCommand(5000, "/bin/sh", "-c",
"docker inspect -f \"{{ .NetworkSettings.IPAddress }}\" " + containerId);
} catch (IOException | InterruptedException e) {
log.error(e.getMessage());
@ -215,7 +215,7 @@ public class DockerManager {
static public String getDockerGatewayIp() {
try {
return CommandExecutor.execCommand("/bin/sh", "-c",
return CommandExecutor.execCommand(5000, "/bin/sh", "-c",
"docker network inspect bridge --format='{{(index .IPAM.Config 0).Gateway}}'");
} catch (IOException | InterruptedException e) {
log.error(e.getMessage());