Search in sources :

Example 1 with ReplicationDownloaderConfiguration

use of org.openstreetmap.osmosis.replication.v0_6.impl.ReplicationDownloaderConfiguration in project osmosis by openstreetmap.

the class BaseReplicationDownloader method runImpl.

private void runImpl() {
    try {
        ReplicationDownloaderConfiguration configuration;
        ReplicationState serverState;
        ReplicationState localState;
        PropertiesPersister localStatePersistor;
        // Instantiate utility objects.
        configuration = new ReplicationDownloaderConfiguration(new File(workingDirectory, CONFIG_FILE));
        // check for custom server state file
        File customServerStateFile = new File(workingDirectory, CUSTOM_SERVER_STATE_FILE);
        if (customServerStateFile.exists()) {
            serverState = new ReplicationState(new PropertiesPersister(customServerStateFile).loadMap());
            LOG.info(String.format("Reading custom server state. [%s]", serverState.toString()));
        } else {
            // Obtain the server state.
            serverState = serverStateReader.getServerState(configuration.getBaseUrl());
            LOG.info(String.format("Reading current server state. [%s]", serverState.toString()));
        }
        // Obtain the server state.
        LOG.fine("Reading current server state.");
        serverState = serverStateReader.getServerState(configuration.getBaseUrl());
        // Build the local state persister which is used for both loading and storing local state.
        localStatePersistor = new PropertiesPersister(new File(workingDirectory, LOCAL_STATE_FILE));
        // Begin processing.
        processInitialize(Collections.<String, Object>emptyMap());
        // then exit.
        if (localStatePersistor.exists()) {
            localState = new ReplicationState(localStatePersistor.loadMap());
            // Download and process the replication files.
            localState = download(configuration, serverState, localState);
        } else {
            localState = serverState;
            processInitializeState(localState);
        }
        // Commit downstream changes.
        processComplete();
        // Persist the local state.
        localStatePersistor.store(localState.store());
    } finally {
        processRelease();
    }
}
Also used : ReplicationDownloaderConfiguration(org.openstreetmap.osmosis.replication.v0_6.impl.ReplicationDownloaderConfiguration) ReplicationState(org.openstreetmap.osmosis.replication.common.ReplicationState) File(java.io.File) PropertiesPersister(org.openstreetmap.osmosis.core.util.PropertiesPersister)

Example 2 with ReplicationDownloaderConfiguration

use of org.openstreetmap.osmosis.replication.v0_6.impl.ReplicationDownloaderConfiguration in project osmosis by openstreetmap.

the class ReplicationLagReader method getLag.

/**
 * Calculate the replication lag and print it to stdout
 */
private void getLag() {
    ReplicationDownloaderConfiguration configuration;
    ReplicationState serverState;
    ReplicationState localState;
    PropertiesPersister localStatePersistor;
    // Instantiate utility objects.
    configuration = new ReplicationDownloaderConfiguration(new File(workingDirectory, CONFIG_FILE));
    // Obtain the server state.
    LOG.fine("Reading current server state.");
    serverState = serverStateReader.getServerState(configuration.getBaseUrl());
    // Build the local state persister which is used for both loading and storing local state.
    localStatePersistor = new PropertiesPersister(new File(workingDirectory, LOCAL_STATE_FILE));
    // If local state isn't available we need to fail because no lag can be calculated.
    if (!localStatePersistor.exists()) {
        throw new OsmosisRuntimeException("Can't read local state.");
    }
    // fetch the local state from the file
    localState = new ReplicationState(localStatePersistor.loadMap());
    // extract the time of the local and the remote state files
    long local = localState.getTimestamp().getTime();
    long server = serverState.getTimestamp().getTime();
    // we assume the server being before the local state while calculating the difference
    long lag = (server - local) / 1000;
    // check if a human readable version is requested
    if (this.humanReadable) {
        if (lag > 86400) {
            // more than a day
            Object[] args = { new Long(lag / 86400), new Long((lag % 86400) / 3600) };
            System.out.println(new MessageFormat("{0} day(s) and {1} hour(s)").format(args));
        } else if (lag > 3600) {
            // morte than an hour
            Object[] args = { new Long(lag / 3600), new Long((lag % 3600) / 60) };
            System.out.println(new MessageFormat("{0} hour(s) and {1} minute(s)").format(args));
        } else if (lag > 60) {
            // more than a minute
            Object[] args = { new Long(lag / 60), new Long(lag % 60) };
            System.out.println(new MessageFormat("{0} minute(s) and {1} second(s)").format(args));
        } else {
            Object[] args = { new Long(lag) };
            // just some seconds
            System.out.println(new MessageFormat("{0} second(s)").format(args));
        }
    } else {
        // print out the raw number of seconds
        System.out.println(lag);
    }
}
Also used : MessageFormat(java.text.MessageFormat) ReplicationDownloaderConfiguration(org.openstreetmap.osmosis.replication.v0_6.impl.ReplicationDownloaderConfiguration) ReplicationState(org.openstreetmap.osmosis.replication.common.ReplicationState) File(java.io.File) OsmosisRuntimeException(org.openstreetmap.osmosis.core.OsmosisRuntimeException) PropertiesPersister(org.openstreetmap.osmosis.core.util.PropertiesPersister)

Aggregations

File (java.io.File)2 PropertiesPersister (org.openstreetmap.osmosis.core.util.PropertiesPersister)2 ReplicationState (org.openstreetmap.osmosis.replication.common.ReplicationState)2 ReplicationDownloaderConfiguration (org.openstreetmap.osmosis.replication.v0_6.impl.ReplicationDownloaderConfiguration)2 MessageFormat (java.text.MessageFormat)1 OsmosisRuntimeException (org.openstreetmap.osmosis.core.OsmosisRuntimeException)1