From 1268adcbdafe715e156cad3b8e6d7eb920d20128 Mon Sep 17 00:00:00 2001 From: xueqingkun Date: Fri, 3 Nov 2023 09:22:33 +0800 Subject: [PATCH] =?UTF-8?q?rasa=EF=BC=9A=E6=8A=8Ashell=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E9=9B=86=E6=88=90=E5=88=B0resources=E7=9B=AE=E5=BD=95=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rasa/service/impl/RasaCmdServiceImpl.java | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/virtual-patient-rasa/src/main/java/com/supervision/rasa/service/impl/RasaCmdServiceImpl.java b/virtual-patient-rasa/src/main/java/com/supervision/rasa/service/impl/RasaCmdServiceImpl.java index 4cf23c97..cc29449d 100644 --- a/virtual-patient-rasa/src/main/java/com/supervision/rasa/service/impl/RasaCmdServiceImpl.java +++ b/virtual-patient-rasa/src/main/java/com/supervision/rasa/service/impl/RasaCmdServiceImpl.java @@ -53,6 +53,9 @@ public class RasaCmdServiceImpl implements RasaCmdService { private final RasaModeService rasaModeService; + private final ConcurrentHashMap shellPathCache = new ConcurrentHashMap<>(); + + @Override @Transactional public String trainExec(RasaCmdArgumentVo argument) throws ExecutionException, InterruptedException, TimeoutException { @@ -234,17 +237,37 @@ public class RasaCmdServiceImpl implements RasaCmdService { if (StrUtil.isEmpty(shell)){ throw new BusinessException("shell 不能为空..."); } - // 获取 shell 脚本的路径 + + // 1. 先从缓存中获取shell脚本路径 String shellPath = "shellScript/" + shell; + String cachePath = shellPathCache.get(shell); + if (StrUtil.isNotEmpty(cachePath)){ + if (new File(cachePath).exists()){ + return cachePath; + }else { + shellPathCache.remove(shell); + } + } + + // 2. 如果缓存中不存在shell脚本路径,从resources目录下加载 ClassPathResource shellScript = new ClassPathResource(shellPath); try { + // 2.1 加载文件,写入临时目录中 InputStream inputStream = shellScript.getInputStream(); File tempFile = File.createTempFile(shell, ".sh"); tempFile.deleteOnExit(); FileUtil.writeFromStream(inputStream, tempFile); + // 2.2 给文件赋可执行权限 + ProcessBuilder chmod = new ProcessBuilder("chmod", "+x", shellPath); + Process chmodProcess = chmod.start(); + chmodProcess.waitFor(); + + // 2.3 把文件路径放到缓存信息中 + shellPathCache.put(shell,tempFile.getAbsolutePath()); + return tempFile.getAbsolutePath(); - } catch (IOException e) { + } catch (IOException | InterruptedException e) { throw new RuntimeException(e); } }