Skip to content

Commit

Permalink
Shutdown seed nodes every 24h
Browse files Browse the repository at this point in the history
The app returns exit code 1, which can be used to trigger a restart.
  • Loading branch information
freimair committed Aug 14, 2019
1 parent 3bf3ff8 commit 1d7a063
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
32 changes: 29 additions & 3 deletions core/src/main/java/bisq/core/app/misc/ExecutableForAppWithP2p.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class ExecutableForAppWithP2p extends BisqExecutable implements UncaughtExceptionHandler {
private static final long MAX_MEMORY_MB_DEFAULT = 500;
private static final long MAX_MEMORY_MB_DEFAULT = 1200;
private static final long CHECK_MEMORY_PERIOD_SEC = 300;
private static final long CHECK_SHUTDOWN = TimeUnit.HOURS.toMillis(1);
private static final long SHUTDOWN_INTERVAL = TimeUnit.HOURS.toMillis(24);
private volatile boolean stopped;
private final long startTime = System.currentTimeMillis();
private static long maxMemory = MAX_MEMORY_MB_DEFAULT;

public ExecutableForAppWithP2p(String fullName, String scriptName, String version) {
Expand Down Expand Up @@ -120,6 +124,20 @@ protected void keepRunning() {
}
}

protected void startShutDownInterval(GracefulShutDownHandler gracefulShutDownHandler) {
UserThread.runPeriodically(() -> {
if (System.currentTimeMillis() - startTime > SHUTDOWN_INTERVAL) {
log.warn("\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" +
"Shut down as node was running longer as {} hours" +
"\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n",
SHUTDOWN_INTERVAL / 3600000);

shutDown(gracefulShutDownHandler);
}

}, CHECK_SHUTDOWN);
}

protected void checkMemory(BisqEnvironment environment, GracefulShutDownHandler gracefulShutDownHandler) {
String maxMemoryOption = environment.getProperty(AppOptionKeys.MAX_MEMORY);
if (maxMemoryOption != null && !maxMemoryOption.isEmpty()) {
Expand Down Expand Up @@ -153,16 +171,24 @@ protected void checkMemory(BisqEnvironment environment, GracefulShutDownHandler
long usedMemory = Profiler.getUsedMemoryInMB();
if (usedMemory > maxMemory) {
log.warn("\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" +
"We are over our memory limit ({}) and trigger a restart. usedMemory: {} MB. freeMemory: {} MB" +
"We are over our memory limit ({}) and trigger a shutdown. usedMemory: {} MB. freeMemory: {} MB" +
"\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n",
(int) maxMemory, usedMemory, Profiler.getFreeMemoryInMB());
restart(environment, gracefulShutDownHandler);
shutDown(gracefulShutDownHandler);
}
}, 5);
}
}, CHECK_MEMORY_PERIOD_SEC);
}

protected void shutDown(GracefulShutDownHandler gracefulShutDownHandler) {
stopped = true;
gracefulShutDownHandler.gracefulShutDown(() -> {
log.info("Shutdown complete");
System.exit(1);
});
}

protected void restart(BisqEnvironment bisqEnvironment, GracefulShutDownHandler gracefulShutDownHandler) {
stopped = true;
gracefulShutDownHandler.gracefulShutDown(() -> {
Expand Down
2 changes: 2 additions & 0 deletions seednode/src/main/java/bisq/seednode/SeedNodeMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public static void main(String[] args) throws Exception {
protected void doExecute(OptionSet options) {
super.doExecute(options);

checkMemory(bisqEnvironment, this);
startShutDownInterval(this);
CommonSetup.setup(this);

keepRunning();
Expand Down

0 comments on commit 1d7a063

Please sign in to comment.