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

Check availability of Jenkins #434

Merged
merged 3 commits into from Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/org/terasology/launcher/LauncherInitTask.java
Expand Up @@ -28,6 +28,7 @@
import org.terasology.launcher.updater.LauncherUpdater;
import org.terasology.launcher.util.BundleUtils;
import org.terasology.launcher.util.DirectoryUtils;
import org.terasology.launcher.util.DownloadUtils;
import org.terasology.launcher.util.FileUtils;
import org.terasology.launcher.util.GuiUtils;
import org.terasology.launcher.util.LauncherStartFailedException;
Expand Down Expand Up @@ -72,7 +73,8 @@ protected LauncherConfiguration call() {
// validate the settings
LauncherSettingsValidator.validate(launcherSettings);

if (launcherSettings.isSearchForLauncherUpdates()) {
final boolean serverAvailable = DownloadUtils.isJenkinsAvailable();
if (serverAvailable && launcherSettings.isSearchForLauncherUpdates()) {
final boolean selfUpdaterStarted = checkForLauncherUpdates(downloadDirectory, tempDirectory, launcherSettings.isKeepDownloadedFiles());
if (selfUpdaterStarted) {
logger.info("Exit old TerasologyLauncher: {}", TerasologyLauncherVersionInfo.getInstance());
Expand All @@ -85,6 +87,7 @@ protected LauncherConfiguration call() {
final Path gameDirectory = getGameDirectory(os, launcherSettings.getGameDirectory());
final Path gameDataDirectory = getGameDataDirectory(os, launcherSettings.getGameDataDirectory());

// TODO: Fix this for server unavailability
final TerasologyGameVersions gameVersions = getTerasologyGameVersions(launcherDirectory, gameDirectory, launcherSettings);

logger.trace("Change LauncherSettings...");
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/terasology/launcher/util/DownloadUtils.java
Expand Up @@ -33,6 +33,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -58,6 +59,8 @@ public final class DownloadUtils {

private static final Logger logger = LoggerFactory.getLogger(DownloadUtils.class);

private static final String JENKINS_URL = "http://jenkins.terasology.org";
private static final int JENKINS_TIMEOUT = 3000; // milliseconds
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 seconds might be a bit too small, but let's keep it for now - we can still adjust this value later.

private static final String JENKINS_JOB_URL = "http://jenkins.terasology.org/job/";
private static final String LAST_STABLE_BUILD = "/lastStableBuild";
private static final String LAST_SUCCESSFUL_BUILD = "/lastSuccessfulBuild";
Expand Down Expand Up @@ -188,6 +191,25 @@ public static URL createUrlJenkins(String jobName, int buildNumber, String subPa
return new URL(urlBuilder.toString());
}

public static boolean isJenkinsAvailable() {
logger.trace("Checking Jenkins availability...");
try {
HttpURLConnection conn = (HttpURLConnection) new URL(JENKINS_URL).openConnection();
try (AutoCloseable ac = conn::disconnect) {
conn.setConnectTimeout(JENKINS_TIMEOUT);
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
logger.trace("Jenkins is available at {}", JENKINS_URL);
return true;
} else {
throw new ConnectException();
}
}
} catch (Exception e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to catch the most specific type of exception you can, e.g., IOException is better than just Exception. You could also use multiple catch (…) statements with the same try.

For instance, a NullPointerException would also be caught by this, although this is (hopefully) not intended here…

logger.warn("Could not connect to Jenkins at {}", JENKINS_URL);
}
return false;
}

/**
* Get the build number of the last stable build on the Jenkins server.
* <p>
Expand Down