rasa:把shell脚本集成到resources目录下

dev_v1.0.1
xueqingkun 2 years ago
parent 3ac9b8c132
commit 1268adcbda

@ -53,6 +53,9 @@ public class RasaCmdServiceImpl implements RasaCmdService {
private final RasaModeService rasaModeService; private final RasaModeService rasaModeService;
private final ConcurrentHashMap<String,String> shellPathCache = new ConcurrentHashMap<>();
@Override @Override
@Transactional @Transactional
public String trainExec(RasaCmdArgumentVo argument) throws ExecutionException, InterruptedException, TimeoutException { public String trainExec(RasaCmdArgumentVo argument) throws ExecutionException, InterruptedException, TimeoutException {
@ -234,17 +237,37 @@ public class RasaCmdServiceImpl implements RasaCmdService {
if (StrUtil.isEmpty(shell)){ if (StrUtil.isEmpty(shell)){
throw new BusinessException("shell 不能为空..."); throw new BusinessException("shell 不能为空...");
} }
// 获取 shell 脚本的路径
// 1. 先从缓存中获取shell脚本路径
String shellPath = "shellScript/" + 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); ClassPathResource shellScript = new ClassPathResource(shellPath);
try { try {
// 2.1 加载文件,写入临时目录中
InputStream inputStream = shellScript.getInputStream(); InputStream inputStream = shellScript.getInputStream();
File tempFile = File.createTempFile(shell, ".sh"); File tempFile = File.createTempFile(shell, ".sh");
tempFile.deleteOnExit(); tempFile.deleteOnExit();
FileUtil.writeFromStream(inputStream, tempFile); 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(); return tempFile.getAbsolutePath();
} catch (IOException e) { } catch (IOException | InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }

Loading…
Cancel
Save