Skip to content

Commit

Permalink
Improve bot threading & fix shutdown of scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Sep 16, 2024
1 parent df72f4c commit 7b100b5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,6 @@ public CompletableFuture<?> stopAttackPermanently() {

public CompletableFuture<?> stopAttackSession() {
return CompletableFuture.runAsync(() -> {
logger.info("Draining attack executor");
scheduler.blockNewTasks(true);
scheduler.drainQueue();

logger.info("Disconnecting bots");
do {
var eventLoopGroups = new HashSet<EventLoopGroup>();
Expand Down Expand Up @@ -398,8 +394,6 @@ public CompletableFuture<?> stopAttackSession() {

// Notify plugins of state change
postEvent(new AttackEndedEvent(this));

scheduler.blockNewTasks(false);
}, scheduler);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void onAttackTick(AttackTickEvent event) {
var instanceManager = event.instanceManager();
for (var entries : List.copyOf(instanceManager.botConnections().entrySet())) {
var bot = entries.getValue();
if (!bot.session().isDisconnected()) {
if (!bot.session().isDisconnected() || bot.explicitlyShutdown()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public final class BotConnection implements EventBusOwner<SoulFireBotEvent> {
private final SessionDataManager dataManager;
private final BotControlAPI botControl;
private final Object shutdownLock = new Object();
private boolean explicitlyShutdown = false;
private boolean running = true;

public BotConnection(
Expand Down Expand Up @@ -133,7 +134,7 @@ public BotConnection(
this.botControl = new BotControlAPI(this, dataManager);

// Start the tick loop
scheduler.schedule(this::tickLoop);
scheduler.scheduleWithFixedDelay(this::tickLoop, 0, 1, TimeUnit.MILLISECONDS);
}

public CompletableFuture<?> connect() {
Expand All @@ -149,28 +150,29 @@ public boolean isOnline() {
}

private void tickLoop() {
if (!running) {
return;
}

MDC.put("connectionId", connectionId.toString());
MDC.put("botName", accountName);
MDC.put("botUuid", accountProfileId.toString());

while (this.running) {
var tickTimer = dataManager.tickTimer();
var ticks = tickTimer.advanceTime(System.currentTimeMillis());

if (session.isDisconnected()) {
wasDisconnected();
break;
}

try {
tick(ticks);
} catch (Throwable t) {
logger.error("Exception ticking bot", t);
}
if (session.isDisconnected()) {
wasDisconnected();
return;
}

var tickTimer = dataManager.tickTimer();
var ticks = tickTimer.advanceTime(System.currentTimeMillis());
tick(ticks);
}

public void tick(int ticks) {
if (ticks <= 0) {
return;
}

try {
session.tick(); // Ensure all packets are handled before ticking

Expand Down Expand Up @@ -208,6 +210,8 @@ public void gracefulDisconnect() {

running = false;

explicitlyShutdown = true;

// Run all shutdown hooks
shutdownHooks.forEach(Runnable::run);

Expand Down

0 comments on commit 7b100b5

Please sign in to comment.