Skip to content

Commit

Permalink
Fix startup procedure: Fixes potential ghost death and console handler
Browse files Browse the repository at this point in the history
  • Loading branch information
tterrag1098 committed Apr 29, 2021
1 parent f074a08 commit 29d7b5a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/main/java/com/tterrag/k9/K9.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public Mono<Void> start() {
GatewayBootstrap<GatewayOptions> gateway = client.gateway()
.setEventDispatcher(ReplayingEventDispatcher.builder()
.replayEventFilter(e -> e instanceof ReadyEvent)
.eventScheduler(Schedulers.boundedElastic())
.build())
.eventScheduler(Schedulers.boundedElastic())
.build())
.setEnabledIntents(IntentSet.of(
Intent.GUILDS, Intent.GUILD_MEMBERS, Intent.GUILD_PRESENCES,
Intent.GUILD_MESSAGES, Intent.GUILD_MESSAGE_REACTIONS,
Expand Down Expand Up @@ -193,8 +193,15 @@ public Mono<Void> start() {

return Mono.fromRunnable(commands::slurpCommands)
.then(gateway.login())
.flatMap(c -> Mono.when(onInitialReady.apply(c.getEventDispatcher()), services.start(c)).thenReturn(c))
.flatMap(this::teardown);
.flatMap(c ->
Mono.when(onInitialReady.apply(c.getEventDispatcher()), services.start(c))
.doOnError(t -> log.error("Unexpected error received in main bot subscriber:", t))
.doOnTerminate(() -> log.error("Unexpected completion of main bot subscriber!"))
.zipWith(teardown(c))
.thenReturn(c)
.onErrorResume($ -> teardown(c).thenReturn(c)))
.flatMap(c -> Mono.fromRunnable(commands::onShutdown)
.then(c.logout()));
}

private boolean isUser(MessageCreateEvent evt) {
Expand All @@ -210,7 +217,7 @@ private Mono<Void> teardown(GatewayDiscordClient gatewayClient) {
while (scan.hasNextLine()) {
if (scan.nextLine().equals("stop")) {
scan.close();
System.exit(0);
return null; // Empty completion will bubble up to zip below
}
}
Threads.sleep(100);
Expand All @@ -226,7 +233,7 @@ private Mono<Void> teardown(GatewayDiscordClient gatewayClient) {

return Mono.zip(consoleHandler, gatewayClient.onDisconnect())
.then()
.doOnTerminate(() -> log.error("Unexpected completion of main bot subscriber!"));
.doOnError(t -> log.error("Disconnect listener error:", t));
}

public static String getVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -58,6 +59,7 @@ public class CommandRegistrar {

private boolean finishedDefaultSlurp;
private boolean locked;
private AtomicBoolean shutdown = new AtomicBoolean(false);

private Disposable autoSaveSubscriber;

Expand Down Expand Up @@ -250,6 +252,7 @@ private void saveAll() {
}

public void onShutdown() {
if (shutdown.getAndSet(true)) return;
saveAll();
for (ICommand c : commands.values()) {
c.onShutdown();
Expand Down

0 comments on commit 29d7b5a

Please sign in to comment.