correctly do stop

This commit is contained in:
Chayapak 2023-10-19 11:46:45 +07:00
parent 5cac01b152
commit 031c592b33
5 changed files with 92 additions and 20 deletions

View file

@ -264,6 +264,11 @@ public class Bot {
session.connect(false);
}
public void stop () {
session.disconnect("Received stop signal");
Main.bots.remove(this);
}
public void addListener (Listener listener) {
listeners.add(listener);
}

View file

@ -4,6 +4,7 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
import land.chipmunk.chayapak.chomens_bot.plugins.ConsolePlugin;
import land.chipmunk.chayapak.chomens_bot.util.HttpUtilities;
import land.chipmunk.chayapak.chomens_bot.util.LoggerUtilities;
import land.chipmunk.chayapak.chomens_bot.util.PersistentDataUtilities;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
@ -44,6 +45,8 @@ public class Main {
private static final List<Thread> alreadyAddedThreads = new ArrayList<>();
private static JDA jda = null;
public static void main(String[] args) throws IOException {
final Path configPath = Path.of("config.yml");
@ -125,17 +128,6 @@ public class Main {
System.exit(1);
}
}, 0, 1, TimeUnit.MINUTES);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
final boolean success = executorService.awaitTermination(3, TimeUnit.SECONDS);
if (!success) System.exit(1);
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
}));
}
public static void initializeBots() {
@ -145,7 +137,6 @@ public class Main {
Configuration.BotOption[] botsOptions = config.bots;
// idk if these should be here lol, but it is just the discord stuff
JDA jda = null;
if (config.discord.enabled) {
JDABuilder builder = JDABuilder.createDefault(config.discord.token);
builder.enableIntents(GatewayIntent.MESSAGE_CONTENT);
@ -193,4 +184,73 @@ public class Main {
System.exit(1);
}
}
// most of these are stolen from HBot
public static void stop () {
executor.shutdownNow();
PersistentDataUtilities.stop();
executorService.shutdownNow();
try {
final boolean executorDone = executor.awaitTermination(5, TimeUnit.SECONDS);
final boolean executorServiceDone = executorService.awaitTermination(5, TimeUnit.SECONDS);
if (executorDone || executorServiceDone) {
System.out.println("Executors failed to shut down");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
ArrayList<Bot> copiedList;
synchronized (bots) {
copiedList = new ArrayList<>(bots);
}
final boolean ircEnabled = config.irc.enabled;
final boolean discordEnabled = config.discord.enabled;
for (Bot bot : copiedList) {
if (discordEnabled) {
final String channelId = bot.discord.servers.get(bot.host + ":" + bot.port);
bot.discord.sendMessageInstantly("Stopping..", channelId);
}
if (ircEnabled) {
bot.irc.quit("Stopping..");
}
bot.stop();
}
jda.shutdownNow();
if (discordEnabled) {
for (int i = 0; i < 150; i++) {
boolean stoppedDiscord = true;
for (Bot bot : bots) {
if (!bot.discord.shuttedDown) {
stoppedDiscord = false;
break;
}
}
if (!stoppedDiscord) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
break;
}
}
}
System.exit(69); // nice
}
}

View file

@ -1,6 +1,7 @@
package land.chipmunk.chayapak.chomens_bot.commands;
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.Main;
import land.chipmunk.chayapak.chomens_bot.command.Command;
import land.chipmunk.chayapak.chomens_bot.command.CommandContext;
import land.chipmunk.chayapak.chomens_bot.command.CommandException;
@ -26,7 +27,7 @@ public class StopCommand extends Command {
final Bot bot = context.bot;
System.exit(0);
Main.stop();
return Component.text("Stopping").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}

View file

@ -17,6 +17,7 @@ import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.session.ShutdownEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
@ -44,6 +45,8 @@ public class DiscordPlugin {
public final String discordUrl;
public boolean shuttedDown = false;
public DiscordPlugin (Configuration config, JDA jda) {
final Configuration.Discord options = config.discord;
this.prefix = options.prefix;
@ -296,6 +299,11 @@ public class DiscordPlugin {
bot.chat.tellraw(component);
}
@Override
public void onShutdown(@NotNull ShutdownEvent event) {
shuttedDown = true;
}
});
bot.discord = this;

View file

@ -51,13 +51,6 @@ public class PersistentDataUtilities {
e.printStackTrace();
}
}, 0, 100, TimeUnit.MILLISECONDS);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
future.cancel(false);
}
});
}
private static void init () {
@ -92,6 +85,11 @@ public class PersistentDataUtilities {
} catch (IOException ignored) {}
}
public static void stop () {
future.cancel(false);
write(jsonObject.toString());
}
public static void put (String property, JsonElement value) {
Main.executorService.submit(() -> queue.put(property, value));
}