Skip to content

Commit

Permalink
修复配置文件重载后 BTN 模块可能提示已加载 0 个能力并停止工作的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Jan 9, 2025
1 parent eb26641 commit 272d479
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main/java/com/ghostchu/peerbanhelper/btn/BtnNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
Expand All @@ -40,7 +41,8 @@
// 特别注意:该类不允许静态初始化任何内容
public class BtnNetwork implements Reloadable {
@Getter
private final Map<Class<? extends BtnAbility>, BtnAbility> abilities = new HashMap<>();
private final Map<Class<? extends BtnAbility>, BtnAbility> abilities = Collections.synchronizedMap(new HashMap<>());

private final ScriptEngine scriptEngine;
private final AtomicBoolean configSuccess = new AtomicBoolean(false);
private TranslationComponent configResult;
Expand Down Expand Up @@ -70,6 +72,7 @@ public BtnNetwork(PeerBanHelperServer server, ScriptEngine scriptEngine, ModuleM
@Override
public ReloadResult reloadModule() throws Exception {
reloadConfig();
log.info("BtnNetwork reloaded");
return Reloadable.super.reloadModule();
}

Expand All @@ -80,6 +83,8 @@ public void reloadConfig() {
this.appId = Main.getMainConfig().getString("btn.app-id");
this.appSecret = Main.getMainConfig().getString("btn.app-secret");
this.scriptExecute = Main.getMainConfig().getBoolean("btn.allow-script-execute");
configSuccess.set(false);
configResult = null;
resetAbilities();
setupHttpClient();
resetScheduler();
Expand All @@ -97,13 +102,13 @@ private void resetScheduler() {
}
if (enabled) {
executeService = Executors.newScheduledThreadPool(2);
executeService.scheduleWithFixedDelay(this::checkIfNeedRetryConfig, 600, 600, TimeUnit.SECONDS);
executeService.scheduleWithFixedDelay(this::checkIfNeedRetryConfig, 0, 600, TimeUnit.SECONDS);
} else {
executeService = null;
}
}

public void configBtnNetwork() {
public synchronized void configBtnNetwork() {
String response = "<Not Provided>";
int statusCode = 0;
try {
Expand Down

0 comments on commit 272d479

Please sign in to comment.