Skip to content

Commit

Permalink
Allow the server to restart after failed status
Browse files Browse the repository at this point in the history
  • Loading branch information
zglicz committed Dec 4, 2024
1 parent 89e788a commit f05972b
Showing 1 changed file with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import com.google.gson.JsonSyntaxException;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -64,6 +62,7 @@ private enum Status {
private static final Logger LOG = LoggerFactory.getLogger(BridgeServerImpl.class);

private static final int DEFAULT_TIMEOUT_SECONDS = 5 * 60;
private static final int TIME_AFTER_FAILURE_TO_RESTART_MS = 60 * 1000;
// internal property to set "--max-old-space-size" for Node process running this server
private static final String MAX_OLD_SPACE_SIZE_PROPERTY = "sonar.javascript.node.maxspace";
private static final String ALLOW_TS_PARSER_JS_FILES = "sonar.javascript.allowTsParserJsFiles";
Expand All @@ -89,6 +88,7 @@ private enum Status {
private final ScheduledExecutorService heartbeatService;
private ScheduledFuture<?> heartbeatFuture;
private final Http http;
private long latestOKIsAliveTimestamp;

// Used by pico container for dependency injection
public BridgeServerImpl(
Expand Down Expand Up @@ -310,8 +310,13 @@ private static Map<String, String> getEnv() {
@Override
public void startServerLazily(BridgeServerConfig serverConfig) throws IOException {
if (status == Status.FAILED) {
// required for SonarLint context to avoid restarting already failed server
throw new ServerAlreadyFailedException();
if (shouldRestartFailedServer()) {
// Reset the status, which will cause the server to retry deployment
status = Status.NOT_STARTED;
} else {
// required for SonarLint context to avoid restarting already failed server
throw new ServerAlreadyFailedException();
}
}
var providedPort = nodeAlreadyRunningPort();
// if SONARJS_EXISTING_NODE_PROCESS_PORT is set, use existing node process
Expand Down Expand Up @@ -455,12 +460,20 @@ public boolean isAlive() {
}
try {
String res = http.get(url("status"));
return "OK!".equals(res);
var result = "OK!".equals(res);
if (result) {
latestOKIsAliveTimestamp = System.currentTimeMillis();
}
return result;
} catch (IOException e) {
return false;
}
}

private boolean shouldRestartFailedServer() {
return System.currentTimeMillis() - latestOKIsAliveTimestamp > TIME_AFTER_FAILURE_TO_RESTART_MS;
}

@Override
public boolean newTsConfig() {
var response = request("", "new-tsconfig").json();
Expand Down

0 comments on commit f05972b

Please sign in to comment.