Moved auto restart node as feature in settings

This commit is contained in:
AlphaX-Reloaded 2024-11-08 09:59:18 +01:00
parent 3f2fc5c6a2
commit a0b4853518
3 changed files with 38 additions and 23 deletions

View File

@ -43,7 +43,7 @@ public class ApplyRestart {
private static final String JAVA_TOOL_OPTIONS_NAME = "JAVA_TOOL_OPTIONS";
private static final String JAVA_TOOL_OPTIONS_VALUE = "";
private static final long CHECK_INTERVAL = 10 * 1000L; // ms
private static final long CHECK_INTERVAL = 30 * 1000L; // ms
private static final int MAX_ATTEMPTS = 12;
public static void main(String[] args) {
@ -56,7 +56,7 @@ public class ApplyRestart {
else
Settings.getInstance();
LOGGER.info("Applying restart...");
LOGGER.info("Applying restart this can take up to 5 minutes...");
// Shutdown node using API
if (!shutdownNode())
@ -64,19 +64,19 @@ public class ApplyRestart {
try {
// Give some time for shutdown
TimeUnit.SECONDS.sleep(30);
TimeUnit.SECONDS.sleep(60);
// Remove blockchain lock if exist
ReentrantLock blockchainLock = Controller.getInstance().getBlockchainLock();
if (blockchainLock.isLocked())
blockchainLock.unlock();
// Remove blockchain lock file if exist
// Remove blockchain lock file if still exist
TimeUnit.SECONDS.sleep(60);
deleteLock();
// Restart node
TimeUnit.SECONDS.sleep(30);
TimeUnit.SECONDS.sleep(15);
restartNode(args);
LOGGER.info("Restarting...");
@ -117,10 +117,17 @@ public class ApplyRestart {
String response = ApiRequest.perform(baseUri + "admin/stop", params);
if (response == null) {
// No response - consider node shut down
try {
TimeUnit.SECONDS.sleep(30);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (apiKeyNewlyGenerated) {
// API key was newly generated for restarting node, so we need to remove it
ApplyRestart.removeGeneratedApiKey();
}
return true;
}
@ -171,7 +178,7 @@ public class ApplyRestart {
LOGGER.debug("Lockfile is: {}", lockFile);
FileUtils.forceDelete(FileUtils.getFile(lockFile));
} catch (IOException e) {
LOGGER.error("Error deleting blockchain lock file: {}", e.getMessage());
LOGGER.debug("Error deleting blockchain lock file: {}", e.getMessage());
}
}

View File

@ -568,26 +568,28 @@ public class Controller extends Thread {
// If GUI is enabled, we're no longer starting up but actually running now
Gui.getInstance().notifyRunning();
// Check every 10 minutes if we have enough connected peers
Timer checkConnectedPeers = new Timer();
if (Settings.getInstance().isAutoRestartEnabled()) {
// Check every 10 minutes if we have enough connected peers
Timer checkConnectedPeers = new Timer();
checkConnectedPeers.schedule(new TimerTask() {
@Override
public void run() {
// Get the connected peers
int myConnectedPeers = Network.getInstance().getImmutableHandshakedPeers().size();
LOGGER.debug("Node have {} connected peers", myConnectedPeers);
if (myConnectedPeers == 0) {
// Restart node if we have 0 peers
LOGGER.info("Node have no connected peers, restarting node");
try {
RestartNode.attemptToRestart();
} catch (Exception e) {
LOGGER.error("Unable to restart the node", e);
checkConnectedPeers.schedule(new TimerTask() {
@Override
public void run() {
// Get the connected peers
int myConnectedPeers = Network.getInstance().getImmutableHandshakedPeers().size();
LOGGER.debug("Node have {} connected peers", myConnectedPeers);
if (myConnectedPeers == 0) {
// Restart node if we have 0 peers
LOGGER.info("Node have no connected peers, restarting node");
try {
RestartNode.attemptToRestart();
} catch (Exception e) {
LOGGER.error("Unable to restart the node", e);
}
}
}
}
}, 10*60*1000, 10*60*1000);
}, 10*60*1000, 10*60*1000);
}
// Check every 10 minutes to see if the block minter is running
Timer checkBlockMinter = new Timer();

View File

@ -114,6 +114,8 @@ public class Settings {
/** Whether we check, fetch and install auto-updates */
private boolean autoUpdateEnabled = true;
/** Whether we check, restart node without connected peers */
private boolean autoRestartEnabled = false;
/** How long between repository backups (ms), or 0 if disabled. */
private long repositoryBackupInterval = 0; // ms
/** Whether to show a notification when we backup repository. */
@ -913,6 +915,10 @@ public class Settings {
return this.autoUpdateEnabled;
}
public boolean isAutoRestartEnabled() {
return this.autoRestartEnabled;
}
public String[] getAutoUpdateRepos() {
return this.autoUpdateRepos;
}