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

View File

@@ -13,7 +13,12 @@ import java.util.concurrent.atomic.AtomicInteger;
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) {
int paramSize = params == null ? 0 : params.size();

View File

@@ -12,21 +12,18 @@ import java.util.List;
public class OriginExecutionService {
private final CommandExecutionService commandExecutionService;
public OriginExecutionService(CommandExecutionService commandExecutionService) {
this.commandExecutionService = commandExecutionService;
public OriginExecutionService() {
}
public RunnerClient.RunnerResponse run(MetaAction metaAction) {
RunnerClient.RunnerResponse response = new RunnerClient.RunnerResponse();
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());
List<String> wrappedCommands = new ArrayList<>();
wrappedCommands.add(wrapped.getCommand());
wrappedCommands.addAll(wrapped.getArgs());
CommandExecutionService.Result execResult = commandExecutionService.exec(wrappedCommands);
CommandExecutionService.Result execResult = CommandExecutionService.INSTANCE.exec(wrappedCommands);
response.setOk(execResult.isOk());
response.setData(execResult.getTotal());
return response;

View File

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