Add caching to the updater

This commit is contained in:
Telesphoreo 2022-04-01 15:00:55 -05:00
parent bbb41aa052
commit bb92805fa5
10 changed files with 51 additions and 26 deletions

View File

@ -10,7 +10,6 @@ import dev.plex.PlexBase;
import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank;
import dev.plex.storage.StorageType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

View File

@ -7,7 +7,6 @@ import dev.morphia.query.experimental.filters.Filters;
import dev.morphia.query.experimental.updates.UpdateOperators;
import dev.plex.Plex;
import dev.plex.player.PlexPlayer;
import java.util.Collections;
import java.util.UUID;

View File

@ -10,7 +10,6 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
/**
* SQL fetching utilities for players

View File

@ -35,7 +35,7 @@ public class PlexCMD extends PlexCommand
send(sender, mmString("<light_purple>Authors: <gold>Telesphoreo, Taahh"));
send(sender, mmString("<light_purple>Built by: <gold>" + Plex.build.author + " <light_purple>on <gold>" + Plex.build.date));
send(sender, mmString("<light_purple>Run <gold>/plex modules <light_purple>to see a list of modules."));
plugin.getUpdateChecker().getUpdateStatusMessage(sender);
plugin.getUpdateChecker().getUpdateStatusMessage(sender, true);
return null;
}
if (args[0].equalsIgnoreCase("reload"))

View File

@ -3,7 +3,6 @@ package dev.plex.module;
import com.google.common.collect.Lists;
import dev.plex.Plex;
import dev.plex.module.exception.ModuleLoadException;
//import dev.plex.module.loader.CustomClassLoader;
import dev.plex.module.loader.LibraryLoader;
import dev.plex.util.PlexLog;
import java.io.File;

View File

@ -7,8 +7,6 @@ import dev.plex.listener.PlexListener;
import java.io.File;
import java.util.List;
import java.util.Locale;
//import dev.plex.module.loader.CustomClassLoader;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

View File

@ -1,9 +1,8 @@
package dev.plex.module;
import com.google.common.collect.ImmutableList;
import lombok.Data;
import java.util.List;
import lombok.Data;
@Data
public class PlexModuleFile

View File

@ -4,6 +4,20 @@ import com.google.common.collect.Lists;
import dev.plex.Plex;
import dev.plex.module.PlexModule;
import dev.plex.module.PlexModuleFile;
import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
@ -29,18 +43,6 @@ import org.eclipse.aether.transport.http.HttpTransporterFactory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;
//TODO: doesn't work
public class LibraryLoader {

View File

@ -17,7 +17,7 @@ public class UpdateCheckerService extends AbstractService
{
if (!newVersion)
{
if (plugin.getUpdateChecker().getUpdateStatusMessage(Bukkit.getConsoleSender()))
if (plugin.getUpdateChecker().getUpdateStatusMessage(Bukkit.getConsoleSender(), false))
{
newVersion = true;
}

View File

@ -18,7 +18,16 @@ import org.bukkit.command.CommandSender;
public class UpdateChecker extends PlexBase
{
private static final String DOWNLOAD_PAGE = "https://ci.plex.us.org/job/Plex/";
/*
* -4 = Never checked for updates
* -3 = Likely rate limited
* -2 = Unknown commit
* -1 = Error occurred
* 0 = Up to date
* > 0 = Number of commits behind
*/
private final String DOWNLOAD_PAGE = "https://ci.plex.us.org/job/Plex/";
private int distance = -4;
// Adapted from Paper
private int fetchDistanceFromGitHub(@Nonnull String repo, @Nonnull String branch, @Nonnull String hash)
@ -31,6 +40,10 @@ public class UpdateChecker extends PlexBase
{
return -2; // Unknown commit
}
if (connection.getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN)
{
return -3; // Rate limited likely
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charsets.UTF_8)))
{
JsonObject obj = new Gson().fromJson(reader, JsonObject.class);
@ -55,10 +68,27 @@ public class UpdateChecker extends PlexBase
}
}
public boolean getUpdateStatusMessage(CommandSender sender)
public boolean getUpdateStatusMessage(CommandSender sender, boolean cached)
{
// If it's -4, it hasn't checked for updates yet
if (distance == -4)
{
int distance;
distance = fetchDistanceFromGitHub("plexusorg/Plex", "master", Plex.build.head);
PlexLog.debug("Never checked for updates, checking now...");
}
else
{
// If the request isn't asked to be cached, fetch it
if (!cached)
{
distance = fetchDistanceFromGitHub("plexusorg/Plex", "master", Plex.build.head);
PlexLog.debug("We have checked for updates before, but this request was not asked to be cached.");
}
else
{
PlexLog.debug("We have checked for updates before, using cache.");
}
}
switch (distance)
{