Detect restarts by reading *everything* logged

The server uses sout to print the message we're interested in...
Hopefully a check like this won't put any significant load on the server
This commit is contained in:
Norbi Peti 2020-10-27 15:02:42 +01:00
parent 2b549227a6
commit 324f5e756c
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 52 additions and 3 deletions

View file

@ -0,0 +1,26 @@
package buttondevteam.discordplugin;
import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
import buttondevteam.discordplugin.util.DPState;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.filter.LevelRangeFilter;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.bukkit.Bukkit;
public class BukkitLogWatcher extends AbstractAppender {
protected BukkitLogWatcher() {
super("ChromaDiscord",
LevelRangeFilter.createFilter(Level.INFO, Level.INFO, Filter.Result.ACCEPT, Filter.Result.DENY),
PatternLayout.createDefaultLayout());
}
@Override
public void append(LogEvent logEvent) {
Bukkit.getLogger().warning("Message: " + logEvent.getMessage().getFormattedMessage());
if (logEvent.getMessage().getFormattedMessage().contains("Attempting to restart with "))
MinecraftChatModule.state = DPState.RESTARTING_SERVER;
}
}

View file

@ -32,6 +32,8 @@ import discord4j.core.object.reaction.ReactionEmoji;
import discord4j.store.jdk.JdkStoreService; import discord4j.store.jdk.JdkStoreService;
import lombok.Getter; import lombok.Getter;
import lombok.val; import lombok.val;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.mockito.internal.util.MockUtil; import org.mockito.internal.util.MockUtil;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -49,6 +51,7 @@ public class DiscordPlugin extends ButtonPlugin {
@Getter @Getter
private Command2DC manager; private Command2DC manager;
private boolean starting; private boolean starting;
private BukkitLogWatcher logWatcher;
/** /**
* The prefix to use with Discord commands like /role. It only works in the bot channel. * The prefix to use with Discord commands like /role. It only works in the bot channel.
@ -215,6 +218,20 @@ public class DiscordPlugin extends ButtonPlugin {
TBMCCoreAPI.SendUnsentExceptions(); TBMCCoreAPI.SendUnsentExceptions();
TBMCCoreAPI.SendUnsentDebugMessages(); TBMCCoreAPI.SendUnsentDebugMessages();
//Bukkit.getLogger().addHandler(new BukkitLogWatcher());
/*val lc = (LoggerContext) LogManager.getContext(false);
var blw = new BukkitLogWatcher();
blw.start();
lc.getConfiguration().addAppender(blw);
Logger logger = lc.getRootLogger();
logger.addAppender(lc.getConfiguration().getAppender(blw.getName()));
logger.get().addAppender(blw, Level.INFO, blw.getFilter());
lc.updateLoggers();*/
var blw = new BukkitLogWatcher();
blw.start();
((Logger) LogManager.getRootLogger()).addAppender(blw);
logWatcher = blw;
if (!TBMCCoreAPI.IsTestServer()) { if (!TBMCCoreAPI.IsTestServer()) {
dc.updatePresence(Presence.online(Activity.playing("Minecraft"))).subscribe(); dc.updatePresence(Presence.online(Activity.playing("Minecraft"))).subscribe();
} else { } else {
@ -259,6 +276,7 @@ public class DiscordPlugin extends ButtonPlugin {
ChromaBot.delete(); ChromaBot.delete();
//timings.printElapsed("Updating presence..."); //timings.printElapsed("Updating presence...");
//dc.updatePresence(Presence.idle(Activity.playing("logging out"))).block(); //No longer using the same account for testing //dc.updatePresence(Presence.idle(Activity.playing("logging out"))).block(); //No longer using the same account for testing
((Logger) LogManager.getRootLogger()).removeAppender(logWatcher);
timings.printElapsed("Logging out..."); timings.printElapsed("Logging out...");
dc.logout().block(); dc.logout().block();
mainServer = null; //Allow ReadyEvent again mainServer = null; //Allow ReadyEvent again

View file

@ -5,7 +5,7 @@ import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.commands.ConnectCommand; import buttondevteam.discordplugin.commands.ConnectCommand;
import buttondevteam.discordplugin.mcchat.MinecraftChatModule; import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
import buttondevteam.discordplugin.util.DPState; import buttondevteam.discordplugin.util.DPState;
import buttondevteam.lib.TBMCCommandPreprocessEvent; import buttondevteam.lib.ScheduledServerRestartEvent;
import buttondevteam.lib.player.TBMCPlayerGetInfoEvent; import buttondevteam.lib.player.TBMCPlayerGetInfoEvent;
import discord4j.common.util.Snowflake; import discord4j.common.util.Snowflake;
import discord4j.core.object.entity.Member; import discord4j.core.object.entity.Member;
@ -53,11 +53,16 @@ public class MCListener implements Listener {
} }
} }
@EventHandler /*@EventHandler
public void onCommandPreprocess(TBMCCommandPreprocessEvent e) { public void onCommandPreprocess(TBMCCommandPreprocessEvent e) {
if (e.getMessage().equalsIgnoreCase("/stop")) if (e.getMessage().equalsIgnoreCase("/stop"))
MinecraftChatModule.state = DPState.STOPPING_SERVER; MinecraftChatModule.state = DPState.STOPPING_SERVER;
else else if (e.getMessage().equalsIgnoreCase("/restart"))
MinecraftChatModule.state = DPState.RESTARTING_SERVER;
}*/
@EventHandler //We don't really need this with the logger stuff but hey
public void onScheduledRestart(ScheduledServerRestartEvent e) {
MinecraftChatModule.state = DPState.RESTARTING_SERVER; MinecraftChatModule.state = DPState.RESTARTING_SERVER;
} }
} }