Search in sources :

Example 1 with BitsquareEnvironment

use of io.bitsquare.app.BitsquareEnvironment in project bitsquare by bitsquare.

the class SeedNodeMain method main.

public static void main(String[] args) throws Exception {
    final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("SeedNodeMain").setDaemon(true).build();
    UserThread.setExecutor(Executors.newSingleThreadExecutor(threadFactory));
    // We don't want to do the full argument parsing here as that might easily change in update versions
    // So we only handle the absolute minimum which is APP_NAME, APP_DATA_DIR_KEY and USER_DATA_DIR
    BitsquareEnvironment.setDefaultAppName("Bitsquare_seednode");
    OptionParser parser = new OptionParser();
    parser.allowsUnrecognizedOptions();
    parser.accepts(AppOptionKeys.USER_DATA_DIR_KEY, description("User data directory", DEFAULT_USER_DATA_DIR)).withRequiredArg();
    parser.accepts(AppOptionKeys.APP_NAME_KEY, description("Application name", DEFAULT_APP_NAME)).withRequiredArg();
    OptionSet options;
    try {
        options = parser.parse(args);
    } catch (OptionException ex) {
        System.out.println("error: " + ex.getMessage());
        System.out.println();
        parser.printHelpOn(System.out);
        System.exit(EXIT_FAILURE);
        return;
    }
    BitsquareEnvironment bitsquareEnvironment = new BitsquareEnvironment(options);
    // need to call that before BitsquareAppMain().execute(args)
    BitsquareExecutable.initAppDir(bitsquareEnvironment.getProperty(AppOptionKeys.APP_DATA_DIR_KEY));
    // For some reason the JavaFX launch process results in us losing the thread context class loader: reset it.
    // In order to work around a bug in JavaFX 8u25 and below, you must include the following code as the first line of your realMain method:
    Thread.currentThread().setContextClassLoader(SeedNodeMain.class.getClassLoader());
    new SeedNodeMain().execute(args);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) OptionException(joptsimple.OptionException) BitsquareEnvironment(io.bitsquare.app.BitsquareEnvironment) OptionSet(joptsimple.OptionSet) OptionParser(joptsimple.OptionParser)

Example 2 with BitsquareEnvironment

use of io.bitsquare.app.BitsquareEnvironment in project bitsquare by bitsquare.

the class SeedNodeMain method doExecute.

@Override
protected void doExecute(OptionSet options) {
    final BitsquareEnvironment environment = new BitsquareEnvironment(options);
    SeedNode.setEnvironment(environment);
    UserThread.execute(() -> seedNode = new SeedNode());
    Thread.UncaughtExceptionHandler handler = (thread, throwable) -> {
        if (throwable.getCause() != null && throwable.getCause().getCause() != null && throwable.getCause().getCause() instanceof BlockStoreException) {
            log.error(throwable.getMessage());
        } else {
            log.error("Uncaught Exception from thread " + Thread.currentThread().getName());
            log.error("throwableMessage= " + throwable.getMessage());
            log.error("throwableClass= " + throwable.getClass());
            log.error("Stack trace:\n" + ExceptionUtils.getStackTrace(throwable));
            throwable.printStackTrace();
            log.error("We shut down the app because an unhandled error occurred");
            // We don't use the restart as in case of OutOfMemory errors the restart might fail as well
            // The run loop will restart the node anyway...
            System.exit(EXIT_FAILURE);
        }
    };
    Thread.setDefaultUncaughtExceptionHandler(handler);
    Thread.currentThread().setUncaughtExceptionHandler(handler);
    String maxMemoryOption = environment.getProperty(AppOptionKeys.MAX_MEMORY);
    if (maxMemoryOption != null && !maxMemoryOption.isEmpty()) {
        try {
            maxMemory = Integer.parseInt(maxMemoryOption);
        } catch (Throwable t) {
            log.error(t.getMessage());
        }
    }
    UserThread.runPeriodically(() -> {
        Profiler.printSystemLoad(log);
        long usedMemoryInMB = Profiler.getUsedMemoryInMB();
        if (!stopped) {
            if (usedMemoryInMB > (maxMemory - 100)) {
                log.warn("\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" + "We are over our memory warn limit and call the GC. usedMemoryInMB: {}" + "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n", usedMemoryInMB);
                System.gc();
                usedMemoryInMB = Profiler.getUsedMemoryInMB();
                Profiler.printSystemLoad(log);
            }
            final long finalUsedMemoryInMB = usedMemoryInMB;
            UserThread.runAfter(() -> {
                if (finalUsedMemoryInMB > maxMemory)
                    restart(environment);
            }, 1);
        }
    }, CHECK_MEMORY_PERIOD_SEC);
    while (true) {
        try {
            Thread.sleep(Long.MAX_VALUE);
        } catch (InterruptedException ignore) {
        }
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Logger(org.slf4j.Logger) UserThread(io.bitsquare.common.UserThread) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) DEFAULT_USER_DATA_DIR(io.bitsquare.app.BitsquareEnvironment.DEFAULT_USER_DATA_DIR) Executors(java.util.concurrent.Executors) AppOptionKeys(io.bitsquare.app.AppOptionKeys) OptionException(joptsimple.OptionException) BitsquareEnvironment(io.bitsquare.app.BitsquareEnvironment) BitsquareExecutable(io.bitsquare.app.BitsquareExecutable) OptionParser(joptsimple.OptionParser) RestartUtil(io.bitsquare.common.util.RestartUtil) Profiler(io.bitsquare.common.util.Profiler) BlockStoreException(org.bitcoinj.store.BlockStoreException) ThreadFactory(java.util.concurrent.ThreadFactory) OptionSet(joptsimple.OptionSet) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) DEFAULT_APP_NAME(io.bitsquare.app.BitsquareEnvironment.DEFAULT_APP_NAME) BlockStoreException(org.bitcoinj.store.BlockStoreException) BitsquareEnvironment(io.bitsquare.app.BitsquareEnvironment) UserThread(io.bitsquare.common.UserThread)

Example 3 with BitsquareEnvironment

use of io.bitsquare.app.BitsquareEnvironment in project bitsquare by bitsquare.

the class SeedNodeModule method configure.

@Override
protected void configure() {
    bind(KeyStorage.class).in(Singleton.class);
    bind(KeyRing.class).in(Singleton.class);
    bind(User.class).in(Singleton.class);
    bind(Preferences.class).in(Singleton.class);
    bind(Clock.class).in(Singleton.class);
    File storageDir = new File(env.getRequiredProperty(Storage.DIR_KEY));
    bind(File.class).annotatedWith(named(Storage.DIR_KEY)).toInstance(storageDir);
    File keyStorageDir = new File(env.getRequiredProperty(KeyStorage.DIR_KEY));
    bind(File.class).annotatedWith(named(KeyStorage.DIR_KEY)).toInstance(keyStorageDir);
    bind(BitsquareEnvironment.class).toInstance((BitsquareEnvironment) env);
    // ordering is used for shut down sequence
    install(tradeModule());
    install(encryptionServiceModule());
    install(arbitratorModule());
    install(offerModule());
    install(torModule());
    install(bitcoinModule());
    install(alertModule());
    install(filterModule());
}
Also used : KeyRing(io.bitsquare.common.crypto.KeyRing) User(io.bitsquare.user.User) KeyStorage(io.bitsquare.common.crypto.KeyStorage) BitsquareEnvironment(io.bitsquare.app.BitsquareEnvironment) Preferences(io.bitsquare.user.Preferences) Clock(io.bitsquare.common.Clock) File(java.io.File)

Example 4 with BitsquareEnvironment

use of io.bitsquare.app.BitsquareEnvironment in project bitsquare by bitsquare.

the class BitsquareEnvironmentTests method testPropertySourcePrecedence.

@Test
public void testPropertySourcePrecedence() {
    PropertySource commandlineProps = new MockPropertySource(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME).withProperty("key.x", "x.commandline");
    PropertySource filesystemProps = new MockPropertySource(BITSQUARE_APP_DIR_PROPERTY_SOURCE_NAME).withProperty("key.x", "x.env").withProperty("key.y", "y.env");
    ConfigurableEnvironment env = new BitsquareEnvironment(commandlineProps) {

        @Override
        PropertySource<?> appDirProperties() {
            return filesystemProps;
        }
    };
    MutablePropertySources propertySources = env.getPropertySources();
    assertThat(propertySources.precedenceOf(named(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME)), equalTo(0));
    assertThat(propertySources.precedenceOf(named(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(1));
    assertThat(propertySources.precedenceOf(named(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(2));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_APP_DIR_PROPERTY_SOURCE_NAME)), equalTo(3));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_HOME_DIR_PROPERTY_SOURCE_NAME)), equalTo(4));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_CLASSPATH_PROPERTY_SOURCE_NAME)), equalTo(5));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_DEFAULT_PROPERTY_SOURCE_NAME)), equalTo(6));
    assertThat(propertySources.size(), equalTo(7));
    // commandline value wins due to precedence
    assertThat(env.getProperty("key.x"), equalTo("x.commandline"));
    // env value wins because it's the only one available
    assertThat(env.getProperty("key.y"), equalTo("y.env"));
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) BitsquareEnvironment(io.bitsquare.app.BitsquareEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) MockPropertySource(org.springframework.mock.env.MockPropertySource) PropertySource(org.springframework.core.env.PropertySource) MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Example 5 with BitsquareEnvironment

use of io.bitsquare.app.BitsquareEnvironment in project bitsquare by bitsquare.

the class StatisticsMain method doExecute.

@Override
protected void doExecute(OptionSet options) {
    final BitsquareEnvironment environment = new BitsquareEnvironment(options);
    Statistics.setEnvironment(environment);
    UserThread.execute(() -> statistics = new Statistics());
    Thread.UncaughtExceptionHandler handler = (thread, throwable) -> {
        if (throwable.getCause() != null && throwable.getCause().getCause() != null && throwable.getCause().getCause() instanceof BlockStoreException) {
            log.error(throwable.getMessage());
        } else {
            log.error("Uncaught Exception from thread " + Thread.currentThread().getName());
            log.error("throwableMessage= " + throwable.getMessage());
            log.error("throwableClass= " + throwable.getClass());
            log.error("Stack trace:\n" + ExceptionUtils.getStackTrace(throwable));
            throwable.printStackTrace();
            log.error("We shut down the app because an unhandled error occurred");
            // We don't use the restart as in case of OutOfMemory errors the restart might fail as well
            // The run loop will restart the node anyway...
            System.exit(EXIT_FAILURE);
        }
    };
    Thread.setDefaultUncaughtExceptionHandler(handler);
    Thread.currentThread().setUncaughtExceptionHandler(handler);
    String maxMemoryOption = environment.getProperty(AppOptionKeys.MAX_MEMORY);
    if (maxMemoryOption != null && !maxMemoryOption.isEmpty()) {
        try {
            maxMemory = Integer.parseInt(maxMemoryOption);
        } catch (Throwable t) {
            log.error(t.getMessage());
        }
    }
    UserThread.runPeriodically(() -> {
        Profiler.printSystemLoad(log);
        long usedMemoryInMB = Profiler.getUsedMemoryInMB();
        if (!stopped) {
            if (usedMemoryInMB > (maxMemory - 100)) {
                log.warn("\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" + "We are over our memory warn limit and call the GC. usedMemoryInMB: {}" + "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n", usedMemoryInMB);
                System.gc();
                usedMemoryInMB = Profiler.getUsedMemoryInMB();
                Profiler.printSystemLoad(log);
            }
            final long finalUsedMemoryInMB = usedMemoryInMB;
            UserThread.runAfter(() -> {
                if (finalUsedMemoryInMB > maxMemory) {
                    log.error("\n\n############################################################\n" + "We shut down as we are over our memory limit. usedMemoryInMB: {}" + "\n############################################################\n\n", finalUsedMemoryInMB);
                    System.exit(EXIT_FAILURE);
                }
            }, 1);
        }
    }, CHECK_MEMORY_PERIOD_SEC);
    while (true) {
        try {
            Thread.sleep(Long.MAX_VALUE);
        } catch (InterruptedException ignore) {
        }
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Logger(org.slf4j.Logger) UserThread(io.bitsquare.common.UserThread) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) DEFAULT_USER_DATA_DIR(io.bitsquare.app.BitsquareEnvironment.DEFAULT_USER_DATA_DIR) Executors(java.util.concurrent.Executors) AppOptionKeys(io.bitsquare.app.AppOptionKeys) OptionException(joptsimple.OptionException) BitsquareEnvironment(io.bitsquare.app.BitsquareEnvironment) BitsquareExecutable(io.bitsquare.app.BitsquareExecutable) OptionParser(joptsimple.OptionParser) RestartUtil(io.bitsquare.common.util.RestartUtil) Profiler(io.bitsquare.common.util.Profiler) BlockStoreException(org.bitcoinj.store.BlockStoreException) ThreadFactory(java.util.concurrent.ThreadFactory) OptionSet(joptsimple.OptionSet) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) DEFAULT_APP_NAME(io.bitsquare.app.BitsquareEnvironment.DEFAULT_APP_NAME) BlockStoreException(org.bitcoinj.store.BlockStoreException) BitsquareEnvironment(io.bitsquare.app.BitsquareEnvironment) UserThread(io.bitsquare.common.UserThread)

Aggregations

BitsquareEnvironment (io.bitsquare.app.BitsquareEnvironment)7 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)4 ThreadFactory (java.util.concurrent.ThreadFactory)4 OptionException (joptsimple.OptionException)4 OptionParser (joptsimple.OptionParser)4 OptionSet (joptsimple.OptionSet)4 AppOptionKeys (io.bitsquare.app.AppOptionKeys)2 DEFAULT_APP_NAME (io.bitsquare.app.BitsquareEnvironment.DEFAULT_APP_NAME)2 DEFAULT_USER_DATA_DIR (io.bitsquare.app.BitsquareEnvironment.DEFAULT_USER_DATA_DIR)2 BitsquareExecutable (io.bitsquare.app.BitsquareExecutable)2 Clock (io.bitsquare.common.Clock)2 UserThread (io.bitsquare.common.UserThread)2 KeyRing (io.bitsquare.common.crypto.KeyRing)2 KeyStorage (io.bitsquare.common.crypto.KeyStorage)2 Profiler (io.bitsquare.common.util.Profiler)2 RestartUtil (io.bitsquare.common.util.RestartUtil)2 Preferences (io.bitsquare.user.Preferences)2 User (io.bitsquare.user.User)2 File (java.io.File)2 IOException (java.io.IOException)2