From 5c2f1e9e0edb6d4d986f91277f04b497864a4a50 Mon Sep 17 00:00:00 2001 From: Martin Graversen Date: Mon, 2 Oct 2023 23:35:52 +0200 Subject: [PATCH] :recycle: Improve management of RconTask Provide execution safety, debug logging --- .../graversen/rust/rcon/tasks/RconTask.java | 20 ++++++++++++++++++- .../rust/rcon/tasks/ReconnectRconTask.java | 4 ++-- .../rust/rcon/tasks/RustPlayersEmitTask.java | 4 ++-- .../rust/rcon/tasks/RustTeamsEmitTask.java | 4 ++-- .../rust/rcon/tasks/ServerInfoEmitTask.java | 4 ++-- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/graversen/rust/rcon/tasks/RconTask.java b/src/main/java/io/graversen/rust/rcon/tasks/RconTask.java index 8f33e74..dca8dc7 100644 --- a/src/main/java/io/graversen/rust/rcon/tasks/RconTask.java +++ b/src/main/java/io/graversen/rust/rcon/tasks/RconTask.java @@ -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(); } diff --git a/src/main/java/io/graversen/rust/rcon/tasks/ReconnectRconTask.java b/src/main/java/io/graversen/rust/rcon/tasks/ReconnectRconTask.java index 6f40c6c..cc63534 100644 --- a/src/main/java/io/graversen/rust/rcon/tasks/ReconnectRconTask.java +++ b/src/main/java/io/graversen/rust/rcon/tasks/ReconnectRconTask.java @@ -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(); } diff --git a/src/main/java/io/graversen/rust/rcon/tasks/RustPlayersEmitTask.java b/src/main/java/io/graversen/rust/rcon/tasks/RustPlayersEmitTask.java index b5d68d1..307d1d1 100644 --- a/src/main/java/io/graversen/rust/rcon/tasks/RustPlayersEmitTask.java +++ b/src/main/java/io/graversen/rust/rcon/tasks/RustPlayersEmitTask.java @@ -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>> rustPlayersGetter; private final @NonNull Consumer rustPlayersEventEmitter; @Override - public void run() { + public void execute() { rustPlayersGetter.get() .thenApply(rustPlayersEventMapper()) .thenAccept(rustPlayersEventEmitter); diff --git a/src/main/java/io/graversen/rust/rcon/tasks/RustTeamsEmitTask.java b/src/main/java/io/graversen/rust/rcon/tasks/RustTeamsEmitTask.java index e3aae0e..cc7ec23 100644 --- a/src/main/java/io/graversen/rust/rcon/tasks/RustTeamsEmitTask.java +++ b/src/main/java/io/graversen/rust/rcon/tasks/RustTeamsEmitTask.java @@ -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>> rustTeamsGetter; private final @NonNull Consumer rustTeamsEventEmitter; @Override - public void run() { + public void execute() { rustTeamsGetter.get() .thenApply(rustTeamsEventMapper()) .thenAccept(rustTeamsEventEmitter); diff --git a/src/main/java/io/graversen/rust/rcon/tasks/ServerInfoEmitTask.java b/src/main/java/io/graversen/rust/rcon/tasks/ServerInfoEmitTask.java index 323caf2..2b96f3f 100644 --- a/src/main/java/io/graversen/rust/rcon/tasks/ServerInfoEmitTask.java +++ b/src/main/java/io/graversen/rust/rcon/tasks/ServerInfoEmitTask.java @@ -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> serverInfoGetter; private final @NonNull Consumer serverInfoEmitter; @Override - public void run() { + public void execute() { serverInfoGetter.get() .thenApply(serverInfoEventMapper()) .thenAccept(serverInfoEmitter);