Skip to content

Commit

Permalink
♻️ Improve management of RconTask
Browse files Browse the repository at this point in the history
Provide execution safety, debug logging
  • Loading branch information
MrGraversen committed Oct 2, 2023
1 parent 9d09a69 commit 5c2f1e9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
20 changes: 19 additions & 1 deletion src/main/java/io/graversen/rust/rcon/tasks/RconTask.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package io.graversen.rust.rcon.tasks;

public interface RconTask extends Runnable {
import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class RconTask implements Runnable {
@Override
public void run() {
log.debug("Executing task: {}", name());

try {
execute();
} catch (Exception e) {
log.error(String.format("Task '%s' failed: %s", name(), e.getMessage()), e);
}
}

protected String name() {
return this.getClass().getSimpleName();
}

protected abstract void execute();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

@Slf4j
@RequiredArgsConstructor
public class ReconnectRconTask implements RconTask {
public class ReconnectRconTask extends RconTask {
private final @NonNull Runnable reconnectHandle;

@Override
public void run() {
public void execute() {
log.debug("Invoking reconnect handle");
reconnectHandle.run();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

@Slf4j
@RequiredArgsConstructor
public class RustPlayersEmitTask implements RconTask {
public class RustPlayersEmitTask extends RconTask {
private final @NonNull RustServer server;
private final @NonNull Supplier<CompletableFuture<List<RustPlayerDTO>>> rustPlayersGetter;
private final @NonNull Consumer<RustPlayersEvent> rustPlayersEventEmitter;

@Override
public void run() {
public void execute() {
rustPlayersGetter.get()
.thenApply(rustPlayersEventMapper())
.thenAccept(rustPlayersEventEmitter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

@Slf4j
@RequiredArgsConstructor
public class RustTeamsEmitTask implements RconTask {
public class RustTeamsEmitTask extends RconTask {
private final @NonNull RustServer server;
private final @NonNull Supplier<CompletableFuture<List<RustTeam>>> rustTeamsGetter;
private final @NonNull Consumer<RustTeamsEvent> rustTeamsEventEmitter;

@Override
public void run() {
public void execute() {
rustTeamsGetter.get()
.thenApply(rustTeamsEventMapper())
.thenAccept(rustTeamsEventEmitter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

@Slf4j
@RequiredArgsConstructor
public class ServerInfoEmitTask implements RconTask {
public class ServerInfoEmitTask extends RconTask {
private final @NonNull RustServer server;
private final @NonNull Supplier<CompletableFuture<ServerInfoDTO>> serverInfoGetter;
private final @NonNull Consumer<ServerInfoEvent> serverInfoEmitter;

@Override
public void run() {
public void execute() {
serverInfoGetter.get()
.thenApply(serverInfoEventMapper())
.thenAccept(serverInfoEmitter);
Expand Down

0 comments on commit 5c2f1e9

Please sign in to comment.