[WIP] Better integration of the maintenance mode: added checks and auto switch to normal operations.

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-11-16 21:55:14 +01:00
parent af9b13b050
commit df7d92fc5d
3 changed files with 51 additions and 10 deletions

View File

@ -3,18 +3,27 @@ package xyz.vallat.louis;
import org.apache.commons.cli.*; import org.apache.commons.cli.*;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import xyz.vallat.louis.codes.ExitCodes;
import xyz.vallat.louis.database.DBManager; import xyz.vallat.louis.database.DBManager;
import xyz.vallat.louis.discord.DiscordManager; import xyz.vallat.louis.discord.DiscordManager;
import xyz.vallat.louis.subtitles.OpenSubtitles; import xyz.vallat.louis.subtitles.OpenSubtitles;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class MoviesQuoteBot { public class MoviesQuoteBot {
public static final String PREFIX = "!"; public static final String PREFIX = "!";
public static final String NAME = "Movies Quote Bot"; public static final String NAME = "Movies Quote Bot";
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 MAINTENANCE_MODE_FILE = "maintenance_mode_engaged";
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;
static { static {
options.addOption(Option.builder().longOpt("reimport").build()); options.addOption(Option.builder().longOpt("reimport").build());
@ -22,12 +31,16 @@ public class MoviesQuoteBot {
public static void main(String[] args) throws ParseException { public static void main(String[] args) throws ParseException {
CommandLine cmd = new DefaultParser().parse(options, args); CommandLine cmd = new DefaultParser().parse(options, args);
if (cmd.hasOption("reimport")) reimportAllSubtitleLines();
if (new File(KILL_SWITCH_FILE).exists()) {
logger.error("Kill switch file '{}' exists, exiting.", KILL_SWITCH_FILE);
System.exit(ExitCodes.URGENT_EXIT_REQUIRED.getValue());
}
DBManager.testConnection(); DBManager.testConnection();
DBManager.initDatabase(); DBManager.initDatabase();
OpenSubtitles.login(); OpenSubtitles.login();
if (cmd.hasOption("reimport")) reimportAllSubtitleLines();
DiscordManager.login(); DiscordManager.login();
Runtime.getRuntime().addShutdownHook(new Thread(() -> { Runtime.getRuntime().addShutdownHook(new Thread(() -> {
@ -37,17 +50,42 @@ public class MoviesQuoteBot {
})); }));
DiscordManager.onDisconnect(); DiscordManager.onDisconnect();
if (new File(MAINTENANCE_MODE_FILE).exists()) {
logger.warn("Starting in maintenance mode.");
switchToMaintenanceMode();
}
} }
public static void switchToMaintenanceMode() { public static void switchToMaintenanceMode() {
try {
if (new File(MAINTENANCE_MODE_FILE).createNewFile())
logger.debug("Created file '{}'.", MAINTENANCE_MODE_FILE);
} catch (IOException e) {
logger.error("Couldn't create file '{}'.", MAINTENANCE_MODE_FILE);
}
DiscordManager.setOnMaintenanceMode();
OpenSubtitles.logout();
logger.info("Maintenance mode engaged.");
} }
public static void switchToNormalOperations() { public static void switchToNormalOperations() {
try {
Files.delete(Path.of(MAINTENANCE_MODE_FILE));
} catch (IOException e) {
logger.error("Could not delete file '{}'.", MAINTENANCE_MODE_FILE, e);
}
isInMaintenanceMode = false;
DiscordManager.setOnline();
OpenSubtitles.login();
logger.info("Maintenance mode disengaged.");
}
public static boolean isInMaintenanceMode() {
if (!Files.exists(Path.of(MAINTENANCE_MODE_FILE)) && isInMaintenanceMode) switchToNormalOperations();
return isInMaintenanceMode;
} }
private static void reimportAllSubtitleLines() { private static void reimportAllSubtitleLines() {
DiscordManager.setOnMaintenanceMode();
} }
} }

View File

@ -17,6 +17,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import static xyz.vallat.louis.MoviesQuoteBot.PREFIX; import static xyz.vallat.louis.MoviesQuoteBot.PREFIX;
import static xyz.vallat.louis.MoviesQuoteBot.isInMaintenanceMode;
public final class DiscordManager { public final class DiscordManager {
private static final List<Command> commands = new ArrayList<>(); private static final List<Command> commands = new ArrayList<>();
@ -46,7 +47,7 @@ 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 -> !isInMaintenanceMode() && 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 -> command.execute(event).then())
.subscribe(); .subscribe();
@ -79,11 +80,12 @@ public final class DiscordManager {
} }
public static void setOnline() { public static void setOnline() {
if (discordClient != null) discordClient.updatePresence(Presence.online(Activity.watching("more films"))).subscribe(); if (discordClient != null)
discordClient.updatePresence(Presence.online(Activity.watching("more films"))).subscribe();
} }
public static void setOnMaintenanceMode() { public static void setOnMaintenanceMode() {
if (discordClient != null) discordClient.updatePresence(Presence.idle(Activity.playing("maintenance mode."))).subscribe(); if (discordClient != null)
isInMaintenanceMode = true; discordClient.updatePresence(Presence.doNotDisturb(Activity.playing("maintenance mode."))).subscribe();
} }
} }

View File

@ -13,9 +13,10 @@ import java.io.InputStreamReader;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import static xyz.vallat.louis.MoviesQuoteBot.KILL_SWITCH_FILE;
public class Server { public class Server {
public static final String KILL_SWITCH_FILE = "system_locked";
private static final Logger logger = LoggerFactory.getLogger(Server.class.getCanonicalName()); private static final Logger logger = LoggerFactory.getLogger(Server.class.getCanonicalName());
private static final int PORT; private static final int PORT;