Skip to content

Commit

Permalink
Don't wait infinitely for client run or server stop to complete
Browse files Browse the repository at this point in the history
- Update LocalProvider to handle a timeout value
- Update LibertyClient to pass in a timeout of 300 seconds for a client
to finish so it doesn't hang
- Update LibertyServer to pass in a timeout of 300 seconds for a server
stop call to finish so it doesn't hang
  • Loading branch information
jhanders34 committed Sep 17, 2024
1 parent 63a4fe3 commit b8d6900
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ public RemoteFile getFile(RemoteFile parent, String name) {
* input path. Note that the actual file is not guaranteed to exist. The input path may
* represent either a file or a directory.
*
* @param path The absolute path to a file on the remote device.
* @param encoding The character set the file is encoded in
* @return A RemoteFile representing the input abstract path name
* @param path The absolute path to a file on the remote device.
* @param encoding The character set the file is encoded in
* @return A RemoteFile representing the input abstract path name
*/
public RemoteFile getFile(String path, Charset encoding) {
return new RemoteFile(this, path, encoding);
Expand Down Expand Up @@ -283,7 +283,7 @@ public String getOSVersion() throws Exception {
params = new String[] { "-c", "\"cat", "/proc/version\"" };
}
Log.finer(c, method, "Command to get OS version: " + cmd + " " + Arrays.toString(params));
this.osVersion = LocalProvider.executeCommand(this, cmd, params, null, null).getStdout().trim();
this.osVersion = LocalProvider.executeCommand(this, cmd, params, null, null, 0).getStdout().trim();
}
Log.exiting(c, method, this.osVersion);
return this.osVersion;
Expand Down Expand Up @@ -454,13 +454,7 @@ public ProgramOutput execute(String cmd, String[] parameters, String workDir, Pr
* @throws Exception
*/
public ProgramOutput execute(String cmd, String[] parameters, String workDir, Properties envVars, int timeout) throws Exception {
// On iSeries, we should be adding the qsh -c flag to the start of any command.
// This means commands are executed in a native-like shell, rather than a
// PASE environment.
if (OperatingSystem.ISERIES.compareTo(getOperatingSystem()) == 0) {
cmd = "qsh -c " + cmd;
}
return LocalProvider.executeCommand(this, cmd, parameters, workDir, envVars);
return LocalProvider.executeCommand(this, cmd, parameters, workDir, envVars, timeout);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.Date;
import java.util.Properties;

import com.ibm.websphere.simplicity.AsyncProgramOutput;
import com.ibm.websphere.simplicity.ConnectionInfo;
import com.ibm.websphere.simplicity.Machine;
import com.ibm.websphere.simplicity.OperatingSystem;
Expand Down Expand Up @@ -53,22 +52,10 @@ public void connect() throws Exception {}
@Override
public void disconnect() throws Exception {}

@Override
public ProgramOutput execute(String cmd, String[] parameters,
String workDir, Properties envVars) throws Exception {
return LocalProvider.executeCommand(this, cmd, parameters, workDir,
envVars);
}

public void executeAsync(String cmd, String[] parameters, String workDir, Properties envVars, OutputStream redirect) throws Exception {
LocalProvider.executeCommandAsync(this, cmd, parameters, workDir, envVars, redirect);
}

@Override
public AsyncProgramOutput executeAsync(String cmd, String[] parameters) throws Exception {
return LocalProvider.executeCommandAsync(this, cmd, parameters, workDir, null);
}

@Override
public String getBootstrapFileKey() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import com.ibm.websphere.simplicity.AsyncProgramOutput;
import com.ibm.websphere.simplicity.Machine;
Expand Down Expand Up @@ -130,26 +131,24 @@ private static boolean delete(File file) {
return file.delete();
}

//

public static ProgramOutput executeCommand(Machine machine, String cmd,
String[] parameters, String workDir, Properties envVars) throws Exception {
String[] parameters, String workDir, Properties envVars, int timeout) throws Exception {
ByteArrayOutputStream bufferOut = new ByteArrayOutputStream();
ByteArrayOutputStream bufferErr = new ByteArrayOutputStream();
int rc = execute(machine, cmd, parameters, envVars, workDir, bufferOut,
bufferErr, false);
bufferErr, false, timeout);
ProgramOutput ret = new ProgramOutput(cmd, rc, bufferOut.toString(), bufferErr.toString());
return ret;
}

public static void executeCommandAsync(Machine machine, String cmd, String[] parameters, String workDir, Properties envVars, OutputStream redirect) throws Exception {
execute(machine, cmd, parameters, envVars, workDir, redirect, null, true);
execute(machine, cmd, parameters, envVars, workDir, redirect, null, true, 0);
}

private static final int execute(Machine machine, final String command,
final String[] parameterArray, Properties envp,
final String workDir, final OutputStream stdOutStream,
final OutputStream stdErrStream, boolean async) throws Exception {
final OutputStream stdErrStream, boolean async, int timeout) throws Exception {
final String method = "execute";
Log.entering(CLASS, method, "async is " + async);

Expand All @@ -173,7 +172,7 @@ private static final int execute(Machine machine, final String command,
* to "cmd /c".
*/
if (machine.getOperatingSystem() == OperatingSystem.WINDOWS && WLP_CYGWIN_HOME == null) {
if (!cmd[0].startsWith("cmd /c")) {
if (!cmd[0].startsWith("cmd")) {
String[] tmp = new String[cmd.length + 2];
tmp[0] = "cmd";
tmp[1] = "/c";
Expand All @@ -182,10 +181,22 @@ private static final int execute(Machine machine, final String command,
cmd = tmp;
}
} else {
if (!cmd[0].startsWith("sh -c")) {
String shellCmd = "sh";

// On iSeries, we should be adding the qsh -c flag to the start of any command.
// This means commands are executed in a native-like shell, rather than a
// PASE environment.
//
// This doesn't currently work and needs additional work to make it work.
/*
* if (OperatingSystem.ISERIES.compareTo(machine.getOperatingSystem()) == 0) {
* shellCmd = "qsh";
* }
*/
if (!cmd[0].startsWith(shellCmd) && !cmd[0].endsWith(shellCmd)) {
String[] tmp = new String[3];
String parsedCommand = shArrayTransform(cmd);
tmp[0] = WLP_CYGWIN_HOME == null ? "sh" : WLP_CYGWIN_HOME + "/bin/sh";
tmp[0] = WLP_CYGWIN_HOME == null ? shellCmd : WLP_CYGWIN_HOME + "/bin/sh";
tmp[1] = "-c";
tmp[2] = parsedCommand;
cmd = tmp;
Expand Down Expand Up @@ -263,11 +274,29 @@ private static final int execute(Machine machine, final String command,
}

// wait till completion
proc.waitFor();
// A timeout value of zero or less means to wait indefinitely for the process to complete
int exitValue;
if (timeout <= 0) {
exitValue = proc.waitFor();
} else {
if (!proc.waitFor(timeout, TimeUnit.SECONDS)) {
Log.error(CLASS, method, null, "Process didn't complete within " + timeout + " seconds");
proc.destroyForcibly();

int count = 0;
while (proc.isAlive()) {
if (count++ >= 5) {
break;
}
Thread.sleep(1000);
}
}
exitValue = proc.exitValue();
}
// let the streams catch up (critical step)
outputGobbler.doJoin();
Log.exiting(CLASS, method);
return proc.exitValue();
return exitValue;
}

/**
Expand Down Expand Up @@ -399,7 +428,7 @@ public static ProgramOutput killProcess(Machine machine, int processId) throws E
parameters = new String[] { "/F", "/PID", "" + processId };
}
Log.finer(CLASS, method, cmd, parameters);
return executeCommand(machine, cmd, parameters, null, null);
return executeCommand(machine, cmd, parameters, null, null, 0);
}

public static RemoteFile ensureFileIsOnMachine(Machine target,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ public ProgramOutput startClientWithArgs(boolean preClean, boolean cleanStart,
Log.info(c, method, "Started client process in debug mode");
output = null;
} else {
output = machine.execute(cmd, parameters, envVars);
output = machine.execute(cmd, parameters, machine.getWorkDir(), envVars, 300);

int rc = output.getReturnCode();
Log.info(c, method, "Response from script is: " + output.getStdout());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3214,7 +3214,7 @@ public ProgramOutput stopServer(boolean ignoreStopped, boolean postStopServerArc
ProgramOutput output = null;

if (!runAsAWindowService) {
output = machine.execute(cmd, parameters, useEnvVars);
output = machine.execute(cmd, parameters, machine.getWorkDir(), useEnvVars, 300);
} else {
ArrayList<String> parametersList = new ArrayList<String>();
for (int i = 0; i < parameters.length; i++) {
Expand All @@ -3225,8 +3225,11 @@ public ProgramOutput stopServer(boolean ignoreStopped, boolean postStopServerArc
String[] stopServiceParameters = stopServiceParmList.toArray(new String[] {});
String[] removeServiceParameters = removeServiceParmList.toArray(new String[] {});

output = machine.execute(cmd, stopServiceParameters, useEnvVars);
output = machine.execute(cmd, removeServiceParameters, useEnvVars);
try {
output = machine.execute(cmd, stopServiceParameters, machine.getWorkDir(), useEnvVars, 300);
} finally {
output = machine.execute(cmd, removeServiceParameters, machine.getWorkDir(), useEnvVars, 300);
}
}

String stdout = output.getStdout();
Expand Down

0 comments on commit b8d6900

Please sign in to comment.