Skip to content

Fix stopping of service #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DiscordRequestDispatcher implements Runnable {
private final HttpClient httpClient;
private final BlockingQueue<DiscordRequestBuilder> queue;
private final String botToken;
private AtomicBoolean running = new AtomicBoolean(false);
private final AtomicBoolean running = new AtomicBoolean(false);
private int numberOfRequestsSent;
private long timeSinceLastRequest;

Expand All @@ -45,6 +45,8 @@ public DiscordResponseFuture queue(DiscordRequest discordRequest) {
public void run() {
running.set(true);

LOGGER.info("Request dispatcher has started");

while (running.get()) {
long currentTime = System.currentTimeMillis();
long elapsed = currentTime - timeSinceLastRequest;
Expand All @@ -64,6 +66,8 @@ public void run() {
/* Ignore */
}
}

LOGGER.info("Request dispatcher has shutdown");
}

public void stop() {
Expand Down
15 changes: 9 additions & 6 deletions core/src/main/java/com/javadiscord/jdi/core/Discord.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ private void loadAnnotations() {
}

public void start() {
this.webSocketManager =
webSocketManager =
new WebSocketManager(
new GatewaySetting().setApiVersion(10).setEncoding(GatewayEncoding.JSON),
identifyRequest,
cache
);

WebSocketManagerProxy webSocketManagerProxy =
new WebSocketManagerProxy(this.webSocketManager);
new WebSocketManagerProxy(webSocketManager);
ConnectionDetails connectionDetails =
new ConnectionDetails(gateway.url(), botToken, gatewaySetting);
ConnectionMediator connectionMediator =
Expand All @@ -159,13 +159,16 @@ public void start() {
}

public void stop() {
if (this.webSocketManager != null) {
this.webSocketManager.stop();
LOGGER.info("Shutdown initiated");

if (webSocketManager != null) {
webSocketManager.stop();
}

discordRequestDispatcher.stop();

EXECUTOR.shutdown();

try {
if (!EXECUTOR.awaitTermination(30, TimeUnit.SECONDS)) {
EXECUTOR.shutdownNow();
Expand All @@ -176,8 +179,8 @@ public void stop() {
);
}
}
} catch (InterruptedException ie) {
LOGGER.error("Termination was interrupted within {} seconds.", 30);
} catch (InterruptedException e) {
LOGGER.error("Termination was interrupted within {} seconds", 30, e);
Thread.currentThread().interrupt();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.WebSocket;
import io.vertx.core.http.WebSocketClient;
import io.vertx.core.http.WebSocketConnectOptions;
import io.vertx.core.http.WebSocketFrame;
import org.apache.logging.log4j.LogManager;
Expand All @@ -21,6 +22,8 @@ public class WebSocketManager {
private final WebSocketRetryHandler retryHandler;
private final Cache cache;
private WebSocket webSocket;
private WebSocketClient webSocketClient;
private boolean retryAllowed;

public WebSocketManager(
GatewaySetting gatewaySetting, IdentifyRequest identifyRequest, Cache cache
Expand Down Expand Up @@ -48,8 +51,9 @@ public void start(ConnectionMediator connectionMediator) {
)
.setSsl(true);

vertx.createWebSocketClient()
.connect(webSocketConnectOptions)
webSocketClient = vertx.createWebSocketClient();
retryAllowed = true;
webSocketClient.connect(webSocketConnectOptions)
.onSuccess(
webSocket -> {
LOGGER.info("Connected to Discord");
Expand All @@ -74,7 +78,9 @@ public void start(ConnectionMediator connectionMediator) {
.onFailure(
error -> {
LOGGER.warn("Failed to connect to {} {}", gatewayURL, error.getCause());
retryHandler.retry(() -> restart(connectionMediator));
if (retryAllowed) {
retryHandler.retry(() -> restart(connectionMediator));
}
}
);
}
Expand All @@ -86,14 +92,22 @@ private void frameHandler(WebSocketFrame frame, WebSocketHandler webSocketHandle
}

public void restart(ConnectionMediator connectionMediator) {
retryAllowed = true;
stop();
start(connectionMediator);
}

public void stop() {
if (!webSocket.isClosed()) {
if (webSocket != null && !webSocket.isClosed()) {
webSocket.close();
}
webSocketClient.close()
.onSuccess(res -> LOGGER.info("Web socket client has been shutdown"))
.onFailure(err -> LOGGER.error("Failed to shutdown web socket client", err));
retryAllowed = false;
vertx.close()
.onSuccess(res -> LOGGER.info("Gateway has shutdown"))
.onFailure(err -> LOGGER.error("Failed to shutdown gateway", err));
}

public WebSocket getWebSocket() {
Expand Down
Loading