Skip to content
Snippets Groups Projects
Commit 541efdc3 authored by brinn's avatar brinn
Browse files

refactor:

- rename CmdLineHelper to ProcessExecutionHelper
- improve interface
add:
- unit test for ProcessExecutionHelper
- support for process watch dog for terminating processes that have been run for too long

SVN: 2025
parent eddcd10d
No related branches found
No related tags found
No related merge requests found
...@@ -24,7 +24,7 @@ import org.apache.log4j.Logger; ...@@ -24,7 +24,7 @@ import org.apache.log4j.Logger;
import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogCategory;
import ch.systemsx.cisd.common.logging.LogFactory; import ch.systemsx.cisd.common.logging.LogFactory;
import ch.systemsx.cisd.common.utilities.CmdLineHelper; import ch.systemsx.cisd.common.utilities.ProcessExecutionHelper;
import ch.systemsx.cisd.common.utilities.OSUtilities; import ch.systemsx.cisd.common.utilities.OSUtilities;
import ch.systemsx.cisd.datamover.filesystem.intf.IPathImmutableCopier; import ch.systemsx.cisd.datamover.filesystem.intf.IPathImmutableCopier;
...@@ -151,7 +151,7 @@ public class RecursiveHardLinkMaker implements IPathImmutableCopier ...@@ -151,7 +151,7 @@ public class RecursiveHardLinkMaker implements IPathImmutableCopier
assert file.isFile(); assert file.isFile();
File destFile = new File(destDir, file.getName()); File destFile = new File(destDir, file.getName());
List<String> cmd = createLnCmdLine(file, destFile); List<String> cmd = createLnCmdLine(file, destFile);
boolean ok = CmdLineHelper.run(cmd, operationLog, machineLog); boolean ok = ProcessExecutionHelper.runAndLog(cmd, operationLog, machineLog);
return ok ? destFile : null; return ok ? destFile : null;
} }
......
...@@ -34,7 +34,7 @@ import ch.systemsx.cisd.common.exceptions.Status; ...@@ -34,7 +34,7 @@ import ch.systemsx.cisd.common.exceptions.Status;
import ch.systemsx.cisd.common.exceptions.StatusFlag; import ch.systemsx.cisd.common.exceptions.StatusFlag;
import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogCategory;
import ch.systemsx.cisd.common.logging.LogFactory; import ch.systemsx.cisd.common.logging.LogFactory;
import ch.systemsx.cisd.common.utilities.CmdLineHelper; import ch.systemsx.cisd.common.utilities.ProcessExecutionHelper;
import ch.systemsx.cisd.common.utilities.FileUtilities; import ch.systemsx.cisd.common.utilities.FileUtilities;
import ch.systemsx.cisd.common.utilities.OSUtilities; import ch.systemsx.cisd.common.utilities.OSUtilities;
import ch.systemsx.cisd.datamover.filesystem.intf.IPathCopier; import ch.systemsx.cisd.datamover.filesystem.intf.IPathCopier;
...@@ -147,7 +147,7 @@ public class RsyncCopier implements IPathCopier ...@@ -147,7 +147,7 @@ public class RsyncCopier implements IPathCopier
copyProcessReference.set(copyProcess); copyProcessReference.set(copyProcess);
final int exitValue = copyProcess.waitFor(); final int exitValue = copyProcess.waitFor();
logRsyncExitValue(exitValue, copyProcess); logRsyncExitValue(copyProcess);
return createStatus(exitValue); return createStatus(exitValue);
} catch (IOException e) } catch (IOException e)
{ {
...@@ -173,7 +173,7 @@ public class RsyncCopier implements IPathCopier ...@@ -173,7 +173,7 @@ public class RsyncCopier implements IPathCopier
return "path '" + path.getAbsolutePath() + "' does not exist"; return "path '" + path.getAbsolutePath() + "' does not exist";
} }
} }
private List<String> createCommandLine(File sourcePath, String sourceHost, File destinationDirectory, private List<String> createCommandLine(File sourcePath, String sourceHost, File destinationDirectory,
String destinationHost) String destinationHost)
{ {
...@@ -249,7 +249,7 @@ public class RsyncCopier implements IPathCopier ...@@ -249,7 +249,7 @@ public class RsyncCopier implements IPathCopier
private static Status createStatus(final int exitValue) private static Status createStatus(final int exitValue)
{ {
if (CmdLineHelper.processTerminated(exitValue)) if (ProcessExecutionHelper.isProcessTerminated(exitValue))
{ {
return TERMINATED_STATUS; return TERMINATED_STATUS;
} }
...@@ -261,9 +261,12 @@ public class RsyncCopier implements IPathCopier ...@@ -261,9 +261,12 @@ public class RsyncCopier implements IPathCopier
return new Status(flag, RsyncExitValueTranslator.getMessage(exitValue)); return new Status(flag, RsyncExitValueTranslator.getMessage(exitValue));
} }
private static void logRsyncExitValue(final int exitValue, final Process copyProcess) private static void logRsyncExitValue(final Process copyProcess)
{ {
CmdLineHelper.logProcessExitValue(exitValue, copyProcess, "rsync", operationLog, machineLog); final int exitValue = copyProcess.exitValue();
final List<String> processOutput =
ProcessExecutionHelper.readProcessOutputLines(copyProcess, machineLog);
ProcessExecutionHelper.logProcessExecution("rsync", exitValue, processOutput, operationLog, machineLog);
} }
/** /**
...@@ -339,7 +342,7 @@ public class RsyncCopier implements IPathCopier ...@@ -339,7 +342,7 @@ public class RsyncCopier implements IPathCopier
final Timer watchDogTimer = initWatchDog(listProcess); final Timer watchDogTimer = initWatchDog(listProcess);
final int exitValue = listProcess.waitFor(); final int exitValue = listProcess.waitFor();
watchDogTimer.cancel(); watchDogTimer.cancel();
logRsyncExitValue(exitValue, listProcess); logRsyncExitValue(listProcess);
return Status.OK == createStatus(exitValue); return Status.OK == createStatus(exitValue);
} catch (IOException ex) } catch (IOException ex)
{ {
......
...@@ -16,17 +16,13 @@ ...@@ -16,17 +16,13 @@
package ch.systemsx.cisd.datamover.filesystem.remote.rsync; package ch.systemsx.cisd.datamover.filesystem.remote.rsync;
import java.io.BufferedReader; import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel;
import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogCategory;
import ch.systemsx.cisd.common.logging.LogFactory; import ch.systemsx.cisd.common.logging.LogFactory;
import ch.systemsx.cisd.common.utilities.ProcessExecutionHelper;
/** /**
* A class that helps checking an <code>rsync</code> binary for its version. * A class that helps checking an <code>rsync</code> binary for its version.
...@@ -36,12 +32,12 @@ import ch.systemsx.cisd.common.logging.LogFactory; ...@@ -36,12 +32,12 @@ import ch.systemsx.cisd.common.logging.LogFactory;
final class RsyncVersionChecker final class RsyncVersionChecker
{ {
private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, RsyncVersionChecker.class);
private static final Logger machineLog = LogFactory.getLogger(LogCategory.MACHINE, RsyncVersionChecker.class); private static final Logger machineLog = LogFactory.getLogger(LogCategory.MACHINE, RsyncVersionChecker.class);
/** /**
* The class holding the version information about an <code>rsync</code> binary. * The class holding the version information about an <code>rsync</code> binary.
*
* @author bernd
*/ */
static class RsyncVersion static class RsyncVersion
{ {
...@@ -138,53 +134,13 @@ final class RsyncVersionChecker ...@@ -138,53 +134,13 @@ final class RsyncVersionChecker
private static String getRsyncVersion(String rsyncExecutableToCheck) private static String getRsyncVersion(String rsyncExecutableToCheck)
{ {
BufferedReader reader = null; final long TIME_TO_WAIT_FOR_COMPLETION = 2 * 1000;
try final ProcessExecutionHelper.ProcessResult result =
{ ProcessExecutionHelper.run(Arrays.asList(rsyncExecutableToCheck, "--version"),
final Process process = new ProcessBuilder(rsyncExecutableToCheck, "--version").start(); TIME_TO_WAIT_FOR_COMPLETION, operationLog, machineLog);
if (machineLog.isDebugEnabled()) result.log();
{ final String versionString = extractRsyncVersion(result.getProcessOutput().get(0));
machineLog.debug("Waiting for rsync process to finish."); return versionString;
}
final long TIME_TO_WAIT_FOR_COMPLETION = 2 * 1000;
final Timer watchDog = new Timer("Rsync Watch Dog");
watchDog.schedule(new TimerTask()
{
@Override
public void run()
{
process.destroy();
}
}, TIME_TO_WAIT_FOR_COMPLETION);
final int exitValue = process.waitFor();
watchDog.cancel();
if (machineLog.isDebugEnabled())
{
machineLog.debug(String.format("Rsync process finished with exit value %s", exitValue));
}
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
final String versionString = extractRsyncVersion(reader.readLine());
return versionString;
} catch (IOException e)
{
return null;
} catch (InterruptedException e)
{
// This should never happen.
throw new CheckedExceptionTunnel(e);
} finally
{
if (reader != null)
{
try
{
reader.close();
} catch (IOException e)
{
// Ignore.
}
}
}
} }
private static String extractRsyncVersion(String rsyncVersionLine) private static String extractRsyncVersion(String rsyncVersionLine)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment