[WIP] Maintenance mode can be enabled through socket calls and client/server communication works fine.

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-11-16 23:38:05 +01:00
parent 0c23268ed3
commit 9890b005fd
5 changed files with 31 additions and 24 deletions

View File

@ -22,7 +22,7 @@ public class MoviesQuoteBot {
public static final String DESCRIPTION = "I may know quotes from movies."; public static final String DESCRIPTION = "I may know quotes from movies.";
public static final String VERSION = "0.3.8-SNAPSHOT"; public static final String VERSION = "0.3.8-SNAPSHOT";
public static final String KILL_SWITCH_FILE = "system_locked"; public static final String KILL_SWITCH_FILE = "system_locked";
public static final String MAINTENANCE_MODE_FILE = "maintenance_mode_engaged"; public static final String MAINTENANCE_MODE_FILE = "maintenance_mode_locked";
private static final Logger logger = LoggerFactory.getLogger(MoviesQuoteBot.class.getCanonicalName()); private static final Logger logger = LoggerFactory.getLogger(MoviesQuoteBot.class.getCanonicalName());
private static final Options options = new Options(); private static final Options options = new Options();
private static boolean isInMaintenanceMode = false; private static boolean isInMaintenanceMode = false;
@ -52,27 +52,31 @@ public class MoviesQuoteBot {
DiscordManager.logout(); DiscordManager.logout();
})); }));
DiscordManager.onDisconnect();
if (new File(MAINTENANCE_MODE_FILE).exists()) { if (new File(MAINTENANCE_MODE_FILE).exists()) {
logger.warn("Starting in maintenance mode."); logger.warn("Starting in maintenance mode.");
switchToMaintenanceMode(); switchToMaintenanceMode();
} }
DiscordManager.onDisconnect();
} }
public static void switchToMaintenanceMode() { public static void switchToMaintenanceMode() {
if (!isInMaintenanceMode) {
try { try {
if (new File(MAINTENANCE_MODE_FILE).createNewFile()) if (new File(MAINTENANCE_MODE_FILE).createNewFile())
logger.debug("Created file '{}'.", MAINTENANCE_MODE_FILE); logger.debug("Created file '{}'.", MAINTENANCE_MODE_FILE);
} catch (IOException e) { } catch (IOException e) {
logger.error("Couldn't create file '{}'.", MAINTENANCE_MODE_FILE); logger.error("Couldn't create file '{}'.", MAINTENANCE_MODE_FILE);
} }
isInMaintenanceMode = true;
DiscordManager.setOnMaintenanceMode(); DiscordManager.setOnMaintenanceMode();
OpenSubtitles.logout(); OpenSubtitles.logout();
logger.info("Maintenance mode engaged."); logger.info("Maintenance mode engaged.");
} }
}
public static void switchToNormalOperations() { public static void switchToNormalOperations() {
if (isInMaintenanceMode) {
try { try {
Files.delete(Path.of(MAINTENANCE_MODE_FILE)); Files.delete(Path.of(MAINTENANCE_MODE_FILE));
} catch (IOException e) { } catch (IOException e) {
@ -83,9 +87,9 @@ public class MoviesQuoteBot {
OpenSubtitles.login(); OpenSubtitles.login();
logger.info("Maintenance mode disengaged."); logger.info("Maintenance mode disengaged.");
} }
}
public static boolean isInMaintenanceMode() { public static boolean isInMaintenanceMode() {
if (!Files.exists(Path.of(MAINTENANCE_MODE_FILE)) && isInMaintenanceMode) switchToNormalOperations();
return isInMaintenanceMode; return isInMaintenanceMode;
} }
@ -93,5 +97,6 @@ public class MoviesQuoteBot {
CommandsClient.checkVersions(); CommandsClient.checkVersions();
CommandsClient.setMaintenanceMode(true); CommandsClient.setMaintenanceMode(true);
CommandsClient.setMaintenanceMode(false); CommandsClient.setMaintenanceMode(false);
System.exit(0);
} }
} }

View File

@ -47,9 +47,9 @@ public final class DiscordManager {
private static void registerDiscordCommands() { private static void registerDiscordCommands() {
for (Command command : commands) for (Command command : commands)
discordClient.getEventDispatcher().on(MessageCreateEvent.class) discordClient.getEventDispatcher().on(MessageCreateEvent.class)
.filter(event -> !isInMaintenanceMode() && event.getMessage().getAuthor().map(user -> !user.isBot()).orElse(false)) .filter(event -> event.getMessage().getAuthor().map(user -> !user.isBot()).orElse(false))
.filter(event -> event.getMessage().getContent().split(" ")[0].equals(command.getName())) .filter(event -> event.getMessage().getContent().split(" ")[0].equals(command.getName()))
.flatMap(event -> command.execute(event).then()) .flatMap(event -> !isInMaintenanceMode() ? command.execute(event).then() : Mono.just("").then())
.subscribe(); .subscribe();
} }

View File

@ -20,6 +20,8 @@ public class CommandsClient {
private static final String SOCKET_HOST = System.getenv(EnvironmentVariables.SOCKET_HOST.getValue()) == null ? private static final String SOCKET_HOST = System.getenv(EnvironmentVariables.SOCKET_HOST.getValue()) == null ?
"localhost" : System.getenv(EnvironmentVariables.SOCKET_HOST.getValue()); "localhost" : System.getenv(EnvironmentVariables.SOCKET_HOST.getValue());
private CommandsClient() {}
private static String sendMessage(String message) { private static String sendMessage(String message) {
logger.debug("Sending message '{}'.", message); logger.debug("Sending message '{}'.", message);
try (Socket clientSocket = new Socket(SOCKET_HOST, PORT)) { try (Socket clientSocket = new Socket(SOCKET_HOST, PORT)) {

View File

@ -57,7 +57,7 @@ public class CommandsServer {
logger.error("Couldn't even create kill switch file... Exiting anyway.", e); logger.error("Couldn't even create kill switch file... Exiting anyway.", e);
} }
System.exit(ExitCodes.URGENT_EXIT_REQUIRED.getValue()); System.exit(ExitCodes.URGENT_EXIT_REQUIRED.getValue());
}); }).start();
} }
private static boolean computeMessage(PrintWriter out, String message) { private static boolean computeMessage(PrintWriter out, String message) {

View File

@ -50,7 +50,7 @@ public final class OpenSubtitles {
public static void logout() { public static void logout() {
try { try {
client.logout(); if (client.isLoggedIn()) client.logout();
} catch (XmlRpcException e) { } catch (XmlRpcException e) {
logger.error("Could not log out. Skipping."); logger.error("Could not log out. Skipping.");
} }