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

change: simplify exception handling in startup phase and improve logging

SVN: 7444
parent a5839b91
No related branches found
No related tags found
No related merge requests found
...@@ -96,45 +96,31 @@ public final class Main ...@@ -96,45 +96,31 @@ public final class Main
*/ */
private static void selfTest(final Parameters parameters) private static void selfTest(final Parameters parameters)
{ {
final String msgStart = "Datamover self test failed:"; final ArrayList<IFileStore> stores = new ArrayList<IFileStore>();
try final FileSysOperationsFactory factory = new FileSysOperationsFactory(parameters);
{ stores.add(parameters.getIncomingStore(factory));
final ArrayList<IFileStore> stores = new ArrayList<IFileStore>(); final IFileStore bufferStore =
final FileSysOperationsFactory factory = new FileSysOperationsFactory(parameters); FileStoreFactory.createLocal(parameters.getBufferDirectoryPath(),
stores.add(parameters.getIncomingStore(factory)); Parameters.BUFFER_KIND_DESC, factory);
final IFileStore bufferStore = stores.add(bufferStore);
FileStoreFactory.createLocal(parameters.getBufferDirectoryPath(), stores.add(parameters.getOutgoingStore(factory));
Parameters.BUFFER_KIND_DESC, factory); if (parameters.tryGetManualInterventionDir() != null)
stores.add(bufferStore);
stores.add(parameters.getOutgoingStore(factory));
if (parameters.tryGetManualInterventionDir() != null)
{
final IFileStore dummyStore =
FileStoreFactory.createLocal(parameters.tryGetManualInterventionDir(),
"manual intervention", factory);
stores.add(dummyStore);
}
if (parameters.tryGetExtraCopyDir() != null)
{
final IFileStore dummyStore =
FileStoreFactory.createLocal(parameters.tryGetExtraCopyDir(), "extra-copy",
factory);
stores.add(dummyStore);
}
final IPathCopier copyProcess = factory.getCopier(false);
SelfTest.check(copyProcess, stores.toArray(IFileStore.EMPTY_ARRAY),
FileStoreSelfTestables.getSelfTestables());
} catch (final HighLevelException e)
{ {
System.err.printf(msgStart + " [%s: %s]\n", e.getClass().getSimpleName(), e final IFileStore dummyStore =
.getMessage()); FileStoreFactory.createLocal(parameters.tryGetManualInterventionDir(),
System.exit(1); "manual intervention", factory);
} catch (final RuntimeException e) stores.add(dummyStore);
}
if (parameters.tryGetExtraCopyDir() != null)
{ {
System.err.println(msgStart); final IFileStore dummyStore =
e.printStackTrace(); FileStoreFactory.createLocal(parameters.tryGetExtraCopyDir(), "extra-copy",
System.exit(1); factory);
stores.add(dummyStore);
} }
final IPathCopier copyProcess = factory.getCopier(false);
SelfTest.check(copyProcess, stores.toArray(IFileStore.EMPTY_ARRAY),
FileStoreSelfTestables.getSelfTestables());
} }
private final static void createShutdownHookTimer(final ITerminable terminable) private final static void createShutdownHookTimer(final ITerminable terminable)
...@@ -176,14 +162,32 @@ public final class Main ...@@ -176,14 +162,32 @@ public final class Main
public static void main(final String[] args) public static void main(final String[] args)
{ {
initLog(); String msgStart = "";
final Parameters parameters = new Parameters(args); try
printInitialLogMessage(parameters); {
startupServer(parameters); initLog();
selfTest(parameters); msgStart = "Datamover parameters wrong:";
if (operationLog.isInfoEnabled()) final Parameters parameters = new Parameters(args);
msgStart = "";
printInitialLogMessage(parameters);
msgStart = "Failure in starting up server";
startupServer(parameters);
msgStart = "Datamover self test failed:";
selfTest(parameters);
if (operationLog.isInfoEnabled())
{
operationLog.info("Datamover ready and waiting for data.");
}
} catch (final HighLevelException e)
{
System.err.printf(msgStart + " [%s: %s]\n", e.getClass().getSimpleName(), e
.getMessage());
System.exit(1);
} catch (final RuntimeException e)
{ {
operationLog.info("Datamover ready and waiting for data."); System.err.println(msgStart);
e.printStackTrace();
System.exit(1);
} }
} }
......
...@@ -41,15 +41,12 @@ import ch.systemsx.cisd.args4j.spi.LongOptionHandler; ...@@ -41,15 +41,12 @@ import ch.systemsx.cisd.args4j.spi.LongOptionHandler;
import ch.systemsx.cisd.args4j.spi.OptionHandler; import ch.systemsx.cisd.args4j.spi.OptionHandler;
import ch.systemsx.cisd.args4j.spi.Setter; import ch.systemsx.cisd.args4j.spi.Setter;
import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException; import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException;
import ch.systemsx.cisd.common.exceptions.HighLevelException;
import ch.systemsx.cisd.common.highwatermark.HostAwareFileWithHighwaterMark; import ch.systemsx.cisd.common.highwatermark.HostAwareFileWithHighwaterMark;
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.BuildAndEnvironmentInfo; import ch.systemsx.cisd.common.utilities.BuildAndEnvironmentInfo;
import ch.systemsx.cisd.common.utilities.IExitHandler;
import ch.systemsx.cisd.common.utilities.OSUtilities; import ch.systemsx.cisd.common.utilities.OSUtilities;
import ch.systemsx.cisd.common.utilities.PropertyUtils; import ch.systemsx.cisd.common.utilities.PropertyUtils;
import ch.systemsx.cisd.common.utilities.SystemExit;
import ch.systemsx.cisd.datamover.filesystem.FileStoreFactory; import ch.systemsx.cisd.datamover.filesystem.FileStoreFactory;
import ch.systemsx.cisd.datamover.filesystem.intf.IFileStore; import ch.systemsx.cisd.datamover.filesystem.intf.IFileStore;
import ch.systemsx.cisd.datamover.filesystem.intf.IFileSysOperationsFactory; import ch.systemsx.cisd.datamover.filesystem.intf.IFileSysOperationsFactory;
...@@ -334,57 +331,43 @@ public final class Parameters implements ITimingParameters, IFileSysParameters ...@@ -334,57 +331,43 @@ public final class Parameters implements ITimingParameters, IFileSysParameters
static final String OUTGOING_KIND_DESC = "outgoing"; static final String OUTGOING_KIND_DESC = "outgoing";
Parameters(final String[] args) Parameters(final String[] args)
{
this(args, SystemExit.SYSTEM_EXIT);
}
Parameters(final String[] args, final IExitHandler systemExitHandler)
{ {
initParametersFromProperties(); initParametersFromProperties();
try parser.parseArgument(args);
if (incomingTarget == null)
{ {
parser.parseArgument(args); throw createConfigurationFailureException(PropertyNames.INCOMING_TARGET);
if (incomingTarget == null) } else if (incomingTarget.getHighwaterMark() > -1L)
{ {
throw createConfigurationFailureException(PropertyNames.INCOMING_TARGET); throw ConfigurationFailureException.fromTemplate(
} else if (incomingTarget.getHighwaterMark() > -1L) "Can not specify a high water mark for '%s'.",
PropertyNames.INCOMING_TARGET);
}
if (bufferDirectory == null)
{
throw createConfigurationFailureException(PropertyNames.BUFFER_DIR);
} else if (bufferDirectory.tryGetHost() != null)
{
throw ConfigurationFailureException.fromTemplate("Remote '%s' not supported.",
PropertyNames.BUFFER_DIR);
}
if (outgoingTarget == null)
{
throw createConfigurationFailureException(PropertyNames.OUTGOING_TARGET);
}
if (manualInterventionDirectoryOrNull == null && manualInterventionRegex != null)
{
throw ConfigurationFailureException.fromTemplate("No '%s' defined, but '%s'.",
PropertyNames.MANUAL_INTERVENTION_DIR,
PropertyNames.MANUAL_INTERVENTION_REGEX);
}
if (getDataCompletedScript() != null)
{
if (OSUtilities.executableExists(dataCompletedScript) == false)
{ {
throw ConfigurationFailureException.fromTemplate( throw ConfigurationFailureException.fromTemplate(
"Can not specify a high water mark for '%s'.", DATA_COMPLETED_SCRIPT_NOT_FOUND_TEMPLATE, dataCompletedScript);
PropertyNames.INCOMING_TARGET);
}
if (bufferDirectory == null)
{
throw createConfigurationFailureException(PropertyNames.BUFFER_DIR);
} else if (bufferDirectory.tryGetHost() != null)
{
throw ConfigurationFailureException.fromTemplate("Remote '%s' not supported.",
PropertyNames.BUFFER_DIR);
}
if (outgoingTarget == null)
{
throw createConfigurationFailureException(PropertyNames.OUTGOING_TARGET);
}
if (manualInterventionDirectoryOrNull == null && manualInterventionRegex != null)
{
throw ConfigurationFailureException.fromTemplate("No '%s' defined, but '%s'.",
PropertyNames.MANUAL_INTERVENTION_DIR,
PropertyNames.MANUAL_INTERVENTION_REGEX);
}
if (getDataCompletedScript() != null)
{
if (OSUtilities.executableExists(dataCompletedScript) == false)
{
throw ConfigurationFailureException.fromTemplate(
DATA_COMPLETED_SCRIPT_NOT_FOUND_TEMPLATE, dataCompletedScript);
}
} }
} catch (final Exception ex)
{
outputException(ex);
systemExitHandler.exit(1);
// Only reached in unit tests.
throw new AssertionError(ex.getMessage());
} }
} }
...@@ -394,22 +377,6 @@ public final class Parameters implements ITimingParameters, IFileSysParameters ...@@ -394,22 +377,6 @@ public final class Parameters implements ITimingParameters, IFileSysParameters
return ConfigurationFailureException.fromTemplate("No '%s' defined.", propertyKey); return ConfigurationFailureException.fromTemplate("No '%s' defined.", propertyKey);
} }
private final void outputException(final Exception ex)
{
if (ex instanceof HighLevelException || ex instanceof CmdLineException)
{
System.err.println(ex.getMessage());
} else
{
System.err.println("An exception occurred.");
ex.printStackTrace();
}
if (ex instanceof CmdLineException)
{
printHelp(false);
}
}
private final static long toMillis(final long seconds) private final static long toMillis(final long seconds)
{ {
return seconds * DateUtils.MILLIS_PER_SECOND; return seconds * DateUtils.MILLIS_PER_SECOND;
......
...@@ -21,20 +21,17 @@ import static org.testng.AssertJUnit.assertFalse; ...@@ -21,20 +21,17 @@ import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertNull;
import static org.testng.AssertJUnit.assertTrue; import static org.testng.AssertJUnit.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.PrintStream;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.time.DateUtils; import org.apache.commons.lang.time.DateUtils;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import ch.rinn.restrictions.Friend; import ch.rinn.restrictions.Friend;
import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException;
import ch.systemsx.cisd.common.highwatermark.HostAwareFileWithHighwaterMark; import ch.systemsx.cisd.common.highwatermark.HostAwareFileWithHighwaterMark;
import ch.systemsx.cisd.common.utilities.AbstractFileSystemTestCase; import ch.systemsx.cisd.common.utilities.AbstractFileSystemTestCase;
import ch.systemsx.cisd.common.utilities.SystemExit; import ch.systemsx.cisd.common.utilities.SystemExit;
...@@ -54,21 +51,9 @@ import ch.systemsx.cisd.datamover.intf.IFileSysParameters; ...@@ -54,21 +51,9 @@ import ch.systemsx.cisd.datamover.intf.IFileSysParameters;
{ SystemExit.class, Parameters.class, FileStoreFactory.class }) { SystemExit.class, Parameters.class, FileStoreFactory.class })
public final class ParametersTest extends AbstractFileSystemTestCase public final class ParametersTest extends AbstractFileSystemTestCase
{ {
private ByteArrayOutputStream logRecorder;
private PrintStream systemOut;
private PrintStream systemErr;
private final Parameters parse(final String... args) private final Parameters parse(final String... args)
{ {
return new Parameters(args, SystemExit.SYSTEM_EXIT); return new Parameters(args);
}
/** Returns the content of the log recorder. */
private final String getLogContent()
{
return logRecorder.toString().trim();
} }
@BeforeClass @BeforeClass
...@@ -83,30 +68,6 @@ public final class ParametersTest extends AbstractFileSystemTestCase ...@@ -83,30 +68,6 @@ public final class ParametersTest extends AbstractFileSystemTestCase
SystemExit.setThrowException(false); SystemExit.setThrowException(false);
} }
@Override
@BeforeMethod
public final void setUp()
{
logRecorder = new ByteArrayOutputStream();
systemOut = System.out;
systemErr = System.err;
System.setErr(new PrintStream(logRecorder));
System.setOut(new PrintStream(logRecorder));
}
@AfterMethod
public void tearDown()
{
if (systemOut != null)
{
System.setOut(systemOut);
}
if (systemErr != null)
{
System.setErr(systemErr);
}
}
@Test @Test
public void testSetRsyncExecutableLong() throws Exception public void testSetRsyncExecutableLong() throws Exception
{ {
...@@ -241,10 +202,10 @@ public final class ParametersTest extends AbstractFileSystemTestCase ...@@ -241,10 +202,10 @@ public final class ParametersTest extends AbstractFileSystemTestCase
try try
{ {
parameters = parse("--" + PropertyNames.DATA_COMPLETED_SCRIPT, scriptName); parameters = parse("--" + PropertyNames.DATA_COMPLETED_SCRIPT, scriptName);
} catch (final RuntimeException ex) } catch (final ConfigurationFailureException ex)
{ {
assertEquals(String.format(Parameters.DATA_COMPLETED_SCRIPT_NOT_FOUND_TEMPLATE, assertEquals(String.format(Parameters.DATA_COMPLETED_SCRIPT_NOT_FOUND_TEMPLATE,
scriptName), getLogContent()); scriptName), ex.getMessage());
} }
final File scriptFile = new File(workingDirectory, scriptName); final File scriptFile = new File(workingDirectory, scriptName);
FileUtils.touch(scriptFile); FileUtils.touch(scriptFile);
......
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