Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS-100 Allow the server to restart after failed status #4995

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,23 @@ 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 (
latestOKIsAliveTimestamp != null &&
System.currentTimeMillis() - latestOKIsAliveTimestamp > TIME_AFTER_FAILURE_TO_RESTART_MS
);
}

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