Remove unnecessary synchronization & tweak some things related to startup/shutdown

This commit is contained in:
Vankka 2021-07-29 12:45:40 +03:00
parent 7a541b5c02
commit 3f126f99c8
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
2 changed files with 15 additions and 25 deletions

View File

@ -37,13 +37,7 @@ public abstract class ServerDiscordSRV<C extends MainConfig, CC extends Connecti
public abstract @NotNull ServerPlayerProvider<?> playerProvider();
public final CompletableFuture<Void> invokeServerStarted() {
return CompletableFuture.runAsync(() -> {
try {
serverStarted();
} catch (Throwable t) {
logger().error("Failed to enable", t);
}
}, scheduler().executor());
return invoke(this::serverStarted, "Failed to enable", true);
}
@OverridingMethodsMustInvokeSuper

View File

@ -78,10 +78,8 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
@Override
public @NotNull Status status() {
synchronized (status) {
return status.get();
}
}
@Override
public @NotNull EventBus eventBus() {
@ -155,13 +153,15 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
}
}
private CompletableFuture<Void> invoke(CheckedRunnable runnable, String message) {
protected CompletableFuture<Void> invoke(CheckedRunnable runnable, String message, boolean enable) {
return CompletableFuture.runAsync(() -> {
try {
runnable.run();
} catch (Throwable t) {
if (enable) {
setStatus(Status.FAILED_TO_START);
disable();
}
logger().error(message, t);
}
}, scheduler().executor());
@ -169,17 +169,17 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
@Override
public final CompletableFuture<Void> invokeEnable() {
return invoke(this::enable, "Failed to enable");
return invoke(this::enable, "Failed to enable", true);
}
@Override
public final CompletableFuture<Void> invokeDisable() {
return invoke(this::disable, "Failed to disable");
return invoke(this::disable, "Failed to disable", false);
}
@Override
public final CompletableFuture<Void> invokeReload() {
return invoke(this::reload, "Failed to reload");
return invoke(this::reload, "Failed to reload", false);
}
@OverridingMethodsMustInvokeSuper
@ -214,16 +214,12 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
@OverridingMethodsMustInvokeSuper
protected void disable() {
synchronized (this.status) {
Status status = this.status.get();
if (status.isShutdown()) {
// Already shutting down/shutdown
return;
}
if (status != Status.FAILED_TO_START) {
this.status.set(Status.SHUTTING_DOWN);
}
}
eventBus().publish(new DiscordSRVShuttingDownEvent());
// Logging