refactor(LocalRunnerClient): allow injecting action watch path

Context:
The hardcoded action watch path made LocalRunnerClient difficult to test and
tightened it to a specific runtime layout. Injecting the watch path improves
testability and allows the runner to work in different runtime environments.
This commit is contained in:
2025-12-16 21:02:29 +08:00
parent d546148d69
commit ad58c0cc7c
3 changed files with 12 additions and 6 deletions

View File

@@ -3,7 +3,7 @@ package work.slhaf.partner.common;
public final class Constant { public final class Constant {
public static final class Path { public static final class Path {
public static final String DATA = "./data"; public static final String DATA = "data";
public static final String MEMORY_DATA = DATA + "/memory"; public static final String MEMORY_DATA = DATA + "/memory";
public static final String ACTION_PROGRAM = DATA + "/action"; public static final String ACTION_PROGRAM = DATA + "/action";
public static final String TMP_ACTION_DIR_LOCAL = ACTION_PROGRAM + "/tmp"; public static final String TMP_ACTION_DIR_LOCAL = ACTION_PROGRAM + "/tmp";

View File

@@ -5,7 +5,7 @@ import com.alibaba.fastjson2.JSONObject;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import work.slhaf.partner.core.action.entity.McpData; import work.slhaf.partner.core.action.entity.McpData;
import work.slhaf.partner.core.action.entity.MetaAction; import work.slhaf.partner.core.action.entity.MetaAction;
import work.slhaf.partner.core.action.entity.MetaActionInfo; import work.slhaf.partner.core.action.entity.MetaActionInfo;
@@ -24,6 +24,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import static work.slhaf.partner.common.Constant.Path.ACTION_PROGRAM; import static work.slhaf.partner.common.Constant.Path.ACTION_PROGRAM;
import static work.slhaf.partner.common.Constant.Path.TMP_ACTION_DIR_LOCAL; import static work.slhaf.partner.common.Constant.Path.TMP_ACTION_DIR_LOCAL;
@@ -31,9 +32,9 @@ import static work.slhaf.partner.common.Constant.Path.TMP_ACTION_DIR_LOCAL;
@Slf4j @Slf4j
public class LocalRunnerClient extends RunnerClient { public class LocalRunnerClient extends RunnerClient {
public LocalRunnerClient(Map<String, MetaActionInfo> existedMetaActions, ExecutorService executor) { public LocalRunnerClient(Map<String, MetaActionInfo> existedMetaActions, ExecutorService executor, @Nullable String actionWatchPath) {
super(existedMetaActions, executor); super(existedMetaActions, executor);
ActionWatchService watchService = new ActionWatchService(); ActionWatchService watchService = new ActionWatchService(actionWatchPath);
watchService.launch(); watchService.launch();
} }
@@ -149,9 +150,14 @@ public class LocalRunnerClient extends RunnerClient {
private class ActionWatchService { private class ActionWatchService {
private final HashMap<Path, WatchKey> registeredPaths = new HashMap<>(); private final HashMap<Path, WatchKey> registeredPaths = new HashMap<>();
private final String actionWatchPath;
private ActionWatchService(String actionWatchPath) {
this.actionWatchPath = actionWatchPath;
}
private void launch() { private void launch() {
Path path = Path.of(ACTION_PROGRAM); Path path = Path.of(actionWatchPath != null ? actionWatchPath : ACTION_PROGRAM);
scanActions(path.toFile()); scanActions(path.toFile());
launchActionDirectoryWatcher(path); launchActionDirectoryWatcher(path);
} }

View File

@@ -41,7 +41,7 @@ public class SystemTest {
void localRunnerClientTest() { void localRunnerClientTest() {
Map<String, MetaActionInfo> existedMetaActions = new HashMap<>(); Map<String, MetaActionInfo> existedMetaActions = new HashMap<>();
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor(); ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
RunnerClient client = new LocalRunnerClient(existedMetaActions, executor); RunnerClient client = new LocalRunnerClient(existedMetaActions, executor, null);
JSONObject res = client.listSysDependencies(); JSONObject res = client.listSysDependencies();
System.out.println(res.toString()); System.out.println(res.toString());
} }