mirror of https://github.com/OpenVidu/openvidu.git
openvidu-server: parameterized wait timeout for CommandExecutor
parent
9ad21624d3
commit
ac81abf7fd
|
@ -215,7 +215,7 @@ public class OpenViduServer implements JsonRpcConfigurer {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getContainerIp() throws IOException, InterruptedException {
|
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 {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
|
@ -35,7 +35,7 @@ public class BashCoturnCredentialsService extends CoturnCredentialsService {
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
private void initialize() {
|
private void initialize() {
|
||||||
try {
|
try {
|
||||||
String response = CommandExecutor.execCommand("/bin/sh", "-c",
|
String response = CommandExecutor.execCommand(10000, "/bin/sh", "-c",
|
||||||
"turnadmin -l -N " + this.coturnDatabaseString);
|
"turnadmin -l -N " + this.coturnDatabaseString);
|
||||||
if (response.contains("turnadmin: not found")) {
|
if (response.contains("turnadmin: not found")) {
|
||||||
// No coturn installed in the host machine
|
// No coturn installed in the host machine
|
||||||
|
@ -54,10 +54,10 @@ public class BashCoturnCredentialsService extends CoturnCredentialsService {
|
||||||
this.logPath = logFile.substring(0, logFile.lastIndexOf('/') + 1);
|
this.logPath = logFile.substring(0, logFile.lastIndexOf('/') + 1);
|
||||||
log.info("Path of COTURN log files: " + this.logPath);
|
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 "
|
"redis-cli -a " + this.openviduConfig.getCoturnDatabasePassword() + " -n "
|
||||||
+ this.openviduConfig.getCoturnDatabaseDbname() + " flushdb");
|
+ 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 "
|
"redis-cli -a " + this.openviduConfig.getCoturnDatabasePassword() + " -n "
|
||||||
+ this.openviduConfig.getCoturnDatabaseDbname() + " --scan --pattern '*'");
|
+ this.openviduConfig.getCoturnDatabaseDbname() + " --scan --pattern '*'");
|
||||||
if ("OK".equals(response) && response2.isEmpty()) {
|
if ("OK".equals(response) && response2.isEmpty()) {
|
||||||
|
@ -80,7 +80,7 @@ public class BashCoturnCredentialsService extends CoturnCredentialsService {
|
||||||
String user = RandomStringUtils.randomAlphanumeric(6).toUpperCase();
|
String user = RandomStringUtils.randomAlphanumeric(6).toUpperCase();
|
||||||
String pass = RandomStringUtils.randomAlphanumeric(6).toLowerCase();
|
String pass = RandomStringUtils.randomAlphanumeric(6).toLowerCase();
|
||||||
String command = "turnadmin -a -u " + user + " -r openvidu -p " + pass + " -N " + this.coturnDatabaseString;
|
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)) {
|
if (response.contains("connection success: " + this.trimmedCoturnDatabaseString)) {
|
||||||
credentials = new TurnCredentials(user, pass);
|
credentials = new TurnCredentials(user, pass);
|
||||||
this.cleanTurnLogFiles();
|
this.cleanTurnLogFiles();
|
||||||
|
@ -99,7 +99,7 @@ public class BashCoturnCredentialsService extends CoturnCredentialsService {
|
||||||
String command = "turnadmin -d -u " + user + " -r openvidu -N " + this.coturnDatabaseString;
|
String command = "turnadmin -d -u " + user + " -r openvidu -N " + this.coturnDatabaseString;
|
||||||
String response = "";
|
String response = "";
|
||||||
try {
|
try {
|
||||||
response = CommandExecutor.execCommand("/bin/sh", "-c", command);
|
response = CommandExecutor.execCommand(10000, "/bin/sh", "-c", command);
|
||||||
this.cleanTurnLogFiles();
|
this.cleanTurnLogFiles();
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -111,7 +111,7 @@ public class BashCoturnCredentialsService extends CoturnCredentialsService {
|
||||||
|
|
||||||
private void cleanTurnLogFiles() throws IOException, InterruptedException {
|
private void cleanTurnLogFiles() throws IOException, InterruptedException {
|
||||||
if (this.logCounter.incrementAndGet() > LOG_LIMIT) {
|
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);
|
log.info("Garbage collector cleaning turn log files at path " + this.logPath);
|
||||||
this.logCounter.set(0);
|
this.logCounter.set(0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -193,7 +193,7 @@ public class PublisherEndpoint extends MediaEndpoint {
|
||||||
public synchronized String publish(SdpType sdpType, String sdpString, boolean doLoopback) {
|
public synchronized String publish(SdpType sdpType, String sdpString, boolean doLoopback) {
|
||||||
registerOnIceCandidateEventListener(this.getOwner().getParticipantPublicId());
|
registerOnIceCandidateEventListener(this.getOwner().getParticipantPublicId());
|
||||||
if (doLoopback) {
|
if (doLoopback) {
|
||||||
connect(this.getEndpoint(), null);
|
connect(this.getEndpoint());
|
||||||
} else {
|
} else {
|
||||||
innerConnect();
|
innerConnect();
|
||||||
}
|
}
|
||||||
|
|
|
@ -160,7 +160,7 @@ public abstract class RecordingService {
|
||||||
protected void updateFilePermissions(String folder) {
|
protected void updateFilePermissions(String folder) {
|
||||||
String command = "chmod -R 777 " + folder;
|
String command = "chmod -R 777 " + folder;
|
||||||
try {
|
try {
|
||||||
String response = CommandExecutor.execCommand("/bin/sh", "-c", command);
|
String response = CommandExecutor.execCommand(5000, "/bin/sh", "-c", command);
|
||||||
if ("".equals(response)) {
|
if ("".equals(response)) {
|
||||||
log.info("KMS recording file permissions successfully updated");
|
log.info("KMS recording file permissions successfully updated");
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -34,34 +34,33 @@ public class CommandExecutor {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(CommandExecutor.class);
|
private static final Logger log = LoggerFactory.getLogger(CommandExecutor.class);
|
||||||
|
|
||||||
private static final long MILLIS_TIMEOUT = 10000;
|
public static String execCommand(long msTimeout, String... command) throws IOException, InterruptedException {
|
||||||
|
|
||||||
public static String execCommand(String... command) throws IOException, InterruptedException {
|
|
||||||
ProcessBuilder processBuilder = new ProcessBuilder(command);
|
ProcessBuilder processBuilder = new ProcessBuilder(command);
|
||||||
processBuilder.redirectErrorStream(true);
|
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 {
|
throws IOException, InterruptedException {
|
||||||
ProcessBuilder processBuilder = new ProcessBuilder(command).redirectError(errorOutputFile);
|
ProcessBuilder processBuilder = new ProcessBuilder(command).redirectError(errorOutputFile);
|
||||||
return commonExecCommand(processBuilder);
|
return commonExecCommand(msTimeout, processBuilder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void execCommandRedirectStandardOutputAndError(File standardOutputFile, File errorOutputFile,
|
public static void execCommandRedirectStandardOutputAndError(long msTimeout, File standardOutputFile,
|
||||||
String... command) throws IOException, InterruptedException {
|
File errorOutputFile, String... command) throws IOException, InterruptedException {
|
||||||
ProcessBuilder processBuilder = new ProcessBuilder(command).redirectOutput(standardOutputFile)
|
ProcessBuilder processBuilder = new ProcessBuilder(command).redirectOutput(standardOutputFile)
|
||||||
.redirectError(errorOutputFile);
|
.redirectError(errorOutputFile);
|
||||||
Process process = processBuilder.start();
|
Process process = processBuilder.start();
|
||||||
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(command), MILLIS_TIMEOUT);
|
log.error("Command {} did not receive a response in {} ms", Arrays.toString(command), msTimeout);
|
||||||
String errorMsg = "Current process status of host:\n" + gatherLinuxHostInformation();
|
String errorMsg = "Current process status of host:\n" + gatherLinuxHostInformation();
|
||||||
log.error(errorMsg);
|
log.error(errorMsg);
|
||||||
throw new IOException(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();
|
Process process = processBuilder.start();
|
||||||
StringBuilder processOutput = new StringBuilder();
|
StringBuilder processOutput = new StringBuilder();
|
||||||
String output;
|
String output;
|
||||||
|
@ -75,9 +74,9 @@ public class CommandExecutor {
|
||||||
processOutput.append(readLine + System.lineSeparator());
|
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",
|
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();
|
String errorMsg = "Current process status of host:\n" + gatherLinuxHostInformation();
|
||||||
log.error(errorMsg);
|
log.error(errorMsg);
|
||||||
throw new IOException(errorMsg);
|
throw new IOException(errorMsg);
|
||||||
|
@ -96,7 +95,7 @@ public class CommandExecutor {
|
||||||
|
|
||||||
public static String gatherLinuxHostInformation() throws IOException, InterruptedException {
|
public static String gatherLinuxHostInformation() throws IOException, InterruptedException {
|
||||||
final String psCommand = "ps -eo pid,ppid,user,%mem,%cpu,cmd --sort=-%cpu";
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -103,8 +103,8 @@ public class DockerManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String runContainer(String container, String containerName, String user, List<Volume> volumes, List<Bind> binds,
|
public String runContainer(String container, String containerName, String user, List<Volume> volumes,
|
||||||
String networkMode, List<String> envs, List<String> command) throws Exception {
|
List<Bind> binds, String networkMode, List<String> envs, List<String> command) throws Exception {
|
||||||
|
|
||||||
CreateContainerCmd cmd = dockerClient.createContainerCmd(container).withEnv(envs);
|
CreateContainerCmd cmd = dockerClient.createContainerCmd(container).withEnv(envs);
|
||||||
if (containerName != null) {
|
if (containerName != null) {
|
||||||
|
@ -123,7 +123,7 @@ public class DockerManager {
|
||||||
hostConfig.withBinds(binds);
|
hostConfig.withBinds(binds);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(command != null) {
|
if (command != null) {
|
||||||
cmd.withCmd(command);
|
cmd.withCmd(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,7 +194,7 @@ public class DockerManager {
|
||||||
|
|
||||||
public String getContainerIp(String containerId) {
|
public String getContainerIp(String containerId) {
|
||||||
try {
|
try {
|
||||||
return CommandExecutor.execCommand("/bin/sh", "-c",
|
return CommandExecutor.execCommand(5000, "/bin/sh", "-c",
|
||||||
"docker inspect -f \"{{ .NetworkSettings.IPAddress }}\" " + containerId);
|
"docker inspect -f \"{{ .NetworkSettings.IPAddress }}\" " + containerId);
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
log.error(e.getMessage());
|
log.error(e.getMessage());
|
||||||
|
@ -215,7 +215,7 @@ public class DockerManager {
|
||||||
|
|
||||||
static public String getDockerGatewayIp() {
|
static public String getDockerGatewayIp() {
|
||||||
try {
|
try {
|
||||||
return CommandExecutor.execCommand("/bin/sh", "-c",
|
return CommandExecutor.execCommand(5000, "/bin/sh", "-c",
|
||||||
"docker network inspect bridge --format='{{(index .IPAM.Config 0).Gateway}}'");
|
"docker network inspect bridge --format='{{(index .IPAM.Config 0).Gateway}}'");
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
log.error(e.getMessage());
|
log.error(e.getMessage());
|
||||||
|
|
Loading…
Reference in New Issue