From 1d7a06333f6157a2b6888381b28603bfed2bae7c Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Wed, 14 Aug 2019 14:38:51 +0200 Subject: [PATCH] Shutdown seed nodes every 24h The app returns exit code 1, which can be used to trigger a restart. --- .../app/misc/ExecutableForAppWithP2p.java | 32 +++++++++++++++++-- .../main/java/bisq/seednode/SeedNodeMain.java | 2 ++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/bisq/core/app/misc/ExecutableForAppWithP2p.java b/core/src/main/java/bisq/core/app/misc/ExecutableForAppWithP2p.java index 55e41da3827..24fec323580 100644 --- a/core/src/main/java/bisq/core/app/misc/ExecutableForAppWithP2p.java +++ b/core/src/main/java/bisq/core/app/misc/ExecutableForAppWithP2p.java @@ -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) { @@ -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()) { @@ -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(() -> { diff --git a/seednode/src/main/java/bisq/seednode/SeedNodeMain.java b/seednode/src/main/java/bisq/seednode/SeedNodeMain.java index 397a271b2f1..53523a053b6 100644 --- a/seednode/src/main/java/bisq/seednode/SeedNodeMain.java +++ b/seednode/src/main/java/bisq/seednode/SeedNodeMain.java @@ -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();