Skip to content
Snippets Groups Projects
Commit 30f6511b authored by ribeaudc's avatar ribeaudc
Browse files

change:

- RenamingProcess moved to common/FileRenamingProcess
- datamover uses FileRenamingProcess

SVN: 2219
parent c9755b19
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.utilities;
import java.io.File;
import org.apache.log4j.Logger;
import ch.systemsx.cisd.common.logging.LogCategory;
import ch.systemsx.cisd.common.logging.LogFactory;
/**
* A <code>IProcess</code> implementation for file renaming.
*
* @author Christian Ribeaud
*/
public final class FileRenamingProcess implements IProcess
{
private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, FileRenamingProcess.class);
public static final long DEFAULT_MILLIS_TO_SLEEP = 5000L;
public static final int DEFAULT_MAX_RETRIES = 12;
private final File sourceFile;
private final File destinationFile;
private final int maxRetries;
private final long millisToSleep;
private int failures;
private boolean renamed;
public FileRenamingProcess(final File sourceFile, final File destinationFile)
{
this(DEFAULT_MAX_RETRIES, DEFAULT_MILLIS_TO_SLEEP, sourceFile, destinationFile);
}
public FileRenamingProcess(final int maxRetries, final long millisToSleep, final File sourceFile,
final File destinationFile)
{
this.sourceFile = sourceFile;
this.maxRetries = maxRetries;
this.millisToSleep = millisToSleep;
this.destinationFile = destinationFile;
}
/**
* Whether the file has been renamed.
* <p>
* This is the return value of {@link File#renameTo(File)}.
* </p>
*/
public final boolean isRenamed()
{
return renamed;
}
//
// IProcess
//
public final int getMaxRetryOnFailure()
{
return maxRetries;
}
public final long getMillisToSleepOnFailure()
{
return millisToSleep;
}
public final void run()
{
renamed = sourceFile.renameTo(destinationFile);
}
public final boolean succeeded()
{
if (renamed == false)
{
if (sourceFile.exists() == false)
{
operationLog.error(String.format("Path '%s' doesn't exist, so it can't be moved to '%s'.", sourceFile,
destinationFile));
// Nothing to do here. So exit the looping by returning true.
return true;
}
operationLog.warn(String.format("Moving path '%s' to directory '%s' failed (attempt %d).", sourceFile,
destinationFile, ++failures));
}
return true;
}
}
\ No newline at end of file
...@@ -24,7 +24,9 @@ import org.apache.log4j.Logger; ...@@ -24,7 +24,9 @@ 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.FileRenamingProcess;
import ch.systemsx.cisd.common.utilities.FileUtilities; import ch.systemsx.cisd.common.utilities.FileUtilities;
import ch.systemsx.cisd.common.utilities.ProcessRunner;
import ch.systemsx.cisd.datamover.filesystem.intf.IPathMover; import ch.systemsx.cisd.datamover.filesystem.intf.IPathMover;
/** /**
...@@ -67,39 +69,10 @@ class RetryingPathMover implements IPathMover ...@@ -67,39 +69,10 @@ class RetryingPathMover implements IPathMover
operationLog.info(String.format("Moving path '%s' to '%s'", sourcePath.getPath(), destinationPath)); operationLog.info(String.format("Moving path '%s' to '%s'", sourcePath.getPath(), destinationPath));
} }
File destFile = new File(destinationPath); File destFile = new File(destinationPath);
int failures = 0; final FileRenamingProcess process =
boolean movedOK = false; new FileRenamingProcess(maxRetriesOnFailure, millisToSleepOnFailure, sourcePath, destFile);
while (true) new ProcessRunner(process);
{ if (process.isRenamed() == false)
movedOK = sourcePath.renameTo(destFile);
if (movedOK)
{
break;
} else
{
if (sourcePath.exists() == false)
{
operationLog.error(String.format("Path '%s' doesn't exist, so it can't be moved to '%s'.",
sourcePath, destinationDirectory));
break;
}
++failures;
operationLog.warn(String.format("Moving path '%s' to directory '%s' failed (attempt %d).", sourcePath,
destinationDirectory, failures));
if (failures >= maxRetriesOnFailure)
{
break;
}
try
{
Thread.sleep(millisToSleepOnFailure);
} catch (InterruptedException ex)
{
break;
}
}
}
if (movedOK == false)
{ {
notificationLog.error(String.format("Moving path '%s' to directory '%s' failed, giving up.", sourcePath, notificationLog.error(String.format("Moving path '%s' to directory '%s' failed, giving up.", sourcePath,
destinationDirectory)); destinationDirectory));
......
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