refactor(runner): refactor CommandExecutionService into single instance

This commit is contained in:
2026-03-18 16:52:52 +08:00
parent 7f4b82204a
commit d8b19ebcea
6 changed files with 15 additions and 19 deletions

View File

@@ -9,7 +9,6 @@ import work.slhaf.partner.core.action.entity.ActionFileMetaData;
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;
import work.slhaf.partner.core.action.exception.ActionInitFailedException; import work.slhaf.partner.core.action.exception.ActionInitFailedException;
import work.slhaf.partner.core.action.runner.execution.CommandExecutionService;
import work.slhaf.partner.core.action.runner.execution.McpActionExecutor; import work.slhaf.partner.core.action.runner.execution.McpActionExecutor;
import work.slhaf.partner.core.action.runner.execution.OriginExecutionService; import work.slhaf.partner.core.action.runner.execution.OriginExecutionService;
import work.slhaf.partner.core.action.runner.mcp.*; import work.slhaf.partner.core.action.runner.mcp.*;
@@ -38,7 +37,6 @@ public class LocalRunnerClient extends RunnerClient implements AutoCloseable {
private final McpClientRegistry mcpClientRegistry; private final McpClientRegistry mcpClientRegistry;
private final McpTransportFactory mcpTransportFactory; private final McpTransportFactory mcpTransportFactory;
private final CommandExecutionService commandExecutionService;
private final ActionSerializer actionSerializer; private final ActionSerializer actionSerializer;
private final OriginExecutionService originExecutionService; private final OriginExecutionService originExecutionService;
private final McpActionExecutor mcpActionExecutor; private final McpActionExecutor mcpActionExecutor;
@@ -62,9 +60,8 @@ public class LocalRunnerClient extends RunnerClient implements AutoCloseable {
McpClientRegistry clientRegistry = new McpClientRegistry(); McpClientRegistry clientRegistry = new McpClientRegistry();
McpTransportFactory transportFactory = new McpTransportFactory(); McpTransportFactory transportFactory = new McpTransportFactory();
CommandExecutionService commandService = new CommandExecutionService();
ActionSerializer serializer = new ActionSerializer(tmpActionPath, dynamicActionPath); ActionSerializer serializer = new ActionSerializer(tmpActionPath, dynamicActionPath);
OriginExecutionService originService = new OriginExecutionService(commandService); OriginExecutionService originService = new OriginExecutionService();
McpActionExecutor actionExecutor = new McpActionExecutor(clientRegistry); McpActionExecutor actionExecutor = new McpActionExecutor(clientRegistry);
McpMetaRegistry metaRegistry = null; McpMetaRegistry metaRegistry = null;
@@ -86,8 +83,7 @@ public class LocalRunnerClient extends RunnerClient implements AutoCloseable {
dynamicManager = new DynamicActionMcpManager( dynamicManager = new DynamicActionMcpManager(
Path.of(dynamicActionPath), Path.of(dynamicActionPath),
existedMetaActions, existedMetaActions,
executor, executor
commandService
); );
registerMcpClient(clientRegistry, transportFactory, MCP_NAME_DYNAMIC, dynamicManager.clientConfig(10)); registerMcpClient(clientRegistry, transportFactory, MCP_NAME_DYNAMIC, dynamicManager.clientConfig(10));
log.info("DynamicActionMcp 注册完毕"); log.info("DynamicActionMcp 注册完毕");
@@ -114,7 +110,6 @@ public class LocalRunnerClient extends RunnerClient implements AutoCloseable {
this.mcpClientRegistry = clientRegistry; this.mcpClientRegistry = clientRegistry;
this.mcpTransportFactory = transportFactory; this.mcpTransportFactory = transportFactory;
this.commandExecutionService = commandService;
this.actionSerializer = serializer; this.actionSerializer = serializer;
this.originExecutionService = originService; this.originExecutionService = originService;
this.mcpActionExecutor = actionExecutor; this.mcpActionExecutor = actionExecutor;

View File

@@ -13,7 +13,12 @@ import java.util.concurrent.atomic.AtomicInteger;
public class CommandExecutionService { public class CommandExecutionService {
private ExecutorService readerExecutor = Executors.newVirtualThreadPerTaskExecutor(); public static final CommandExecutionService INSTANCE = new CommandExecutionService();
private final ExecutorService readerExecutor = Executors.newVirtualThreadPerTaskExecutor();
private CommandExecutionService() {
}
public String[] buildFileExecutionCommands(String launcher, Map<String, Object> params, String absolutePath) { public String[] buildFileExecutionCommands(String launcher, Map<String, Object> params, String absolutePath) {
int paramSize = params == null ? 0 : params.size(); int paramSize = params == null ? 0 : params.size();

View File

@@ -12,21 +12,18 @@ import java.util.List;
public class OriginExecutionService { public class OriginExecutionService {
private final CommandExecutionService commandExecutionService; public OriginExecutionService() {
public OriginExecutionService(CommandExecutionService commandExecutionService) {
this.commandExecutionService = commandExecutionService;
} }
public RunnerClient.RunnerResponse run(MetaAction metaAction) { public RunnerClient.RunnerResponse run(MetaAction metaAction) {
RunnerClient.RunnerResponse response = new RunnerClient.RunnerResponse(); RunnerClient.RunnerResponse response = new RunnerClient.RunnerResponse();
File file = new File(metaAction.getLocation()); File file = new File(metaAction.getLocation());
String[] commands = commandExecutionService.buildFileExecutionCommands(metaAction.getLauncher(), metaAction.getParams(), file.getAbsolutePath()); String[] commands = CommandExecutionService.INSTANCE.buildFileExecutionCommands(metaAction.getLauncher(), metaAction.getParams(), file.getAbsolutePath());
WrappedLaunchSpec wrapped = ExecutionPolicyRegistry.INSTANCE.prepare(Arrays.stream(commands).toList()); WrappedLaunchSpec wrapped = ExecutionPolicyRegistry.INSTANCE.prepare(Arrays.stream(commands).toList());
List<String> wrappedCommands = new ArrayList<>(); List<String> wrappedCommands = new ArrayList<>();
wrappedCommands.add(wrapped.getCommand()); wrappedCommands.add(wrapped.getCommand());
wrappedCommands.addAll(wrapped.getArgs()); wrappedCommands.addAll(wrapped.getArgs());
CommandExecutionService.Result execResult = commandExecutionService.exec(wrappedCommands); CommandExecutionService.Result execResult = CommandExecutionService.INSTANCE.exec(wrappedCommands);
response.setOk(execResult.isOk()); response.setOk(execResult.isOk());
response.setData(execResult.getTotal()); response.setData(execResult.getTotal());
return response; return response;

View File

@@ -45,11 +45,10 @@ public class DynamicActionMcpManager implements AutoCloseable {
public DynamicActionMcpManager(Path root, public DynamicActionMcpManager(Path root,
ConcurrentHashMap<String, MetaActionInfo> existedMetaActions, ConcurrentHashMap<String, MetaActionInfo> existedMetaActions,
ExecutorService executor, ExecutorService executor) throws IOException {
CommandExecutionService commandExecutionService) throws IOException {
this.root = root; this.root = root;
this.existedMetaActions = existedMetaActions; this.existedMetaActions = existedMetaActions;
this.commandExecutionService = commandExecutionService; this.commandExecutionService = CommandExecutionService.INSTANCE;
InProcessMcpTransport.Pair pair = InProcessMcpTransport.pair(); InProcessMcpTransport.Pair pair = InProcessMcpTransport.pair();
this.clientTransport = pair.clientSide(); this.clientTransport = pair.clientSide();
McpSchema.ServerCapabilities serverCapabilities = McpSchema.ServerCapabilities.builder() McpSchema.ServerCapabilities serverCapabilities = McpSchema.ServerCapabilities.builder()

View File

@@ -890,7 +890,7 @@ public class LocalRunnerClientTest {
LocalRunnerClient client = new LocalRunnerClient(existedMetaActions, executor, tempDir.toString()); LocalRunnerClient client = new LocalRunnerClient(existedMetaActions, executor, tempDir.toString());
BuiltinActionRegistry registry = new BuiltinActionRegistry(); BuiltinActionRegistry registry = new BuiltinActionRegistry();
client.setBuiltinActionRegistry(registry); client.setBuiltinActionRegistry(registry);
registry.defineBuiltinAction("echo", buildMetaActionInfo("echo"), params -> params.get("value")); registry.defineBuiltinAction("echo", buildMetaActionInfo("echo"), params -> params.get("value").toString());
try { try {
MetaAction metaAction = buildMetaAction(MetaAction.Type.BUILTIN, "builtin", "echo", Map.of("value", "ok")); MetaAction metaAction = buildMetaAction(MetaAction.Type.BUILTIN, "builtin", "echo", Map.of("value", "ok"));

View File

@@ -12,7 +12,7 @@ import java.util.Map;
class CommandExecutionServiceTest { class CommandExecutionServiceTest {
private final CommandExecutionService service = new CommandExecutionService(); private final CommandExecutionService service = CommandExecutionService.INSTANCE;
@Test @Test
void testBuildFileExecutionCommandsWithOrderedParams() { void testBuildFileExecutionCommandsWithOrderedParams() {