|
|
|
@ -17,8 +17,7 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
import java.util.function.Predicate;
|
|
|
|
|
|
|
|
|
@ -50,6 +49,9 @@ public class RasaCmdServiceImpl implements RasaCmdService {
|
|
|
|
|
@Value("${rasa.run-shell}")
|
|
|
|
|
private String runShell;
|
|
|
|
|
|
|
|
|
|
@Value("${rasa.kill-shell}")
|
|
|
|
|
private String killShell;
|
|
|
|
|
|
|
|
|
|
@Value("${rasa.shell.work:/data/vp/rasa_shell/}")
|
|
|
|
|
private String shellWork;
|
|
|
|
|
|
|
|
|
@ -60,6 +62,8 @@ public class RasaCmdServiceImpl implements RasaCmdService {
|
|
|
|
|
@Transactional
|
|
|
|
|
public String trainExec(RasaCmdArgumentVo argument) throws ExecutionException, InterruptedException, TimeoutException {
|
|
|
|
|
|
|
|
|
|
argument.setFixedModelNameIfAbsent();
|
|
|
|
|
|
|
|
|
|
// /rasa/v3_jiazhuangxian/domain.yml domain的路径,应该是从zip文件中加压出来的文件的路径后面拼上/domain.yml
|
|
|
|
|
String domain = replaceDuplicateSeparator(String.join(File.separator,dataPath,argument.getModelId(),"domain.yml"));
|
|
|
|
|
|
|
|
|
@ -103,21 +107,43 @@ public class RasaCmdServiceImpl implements RasaCmdService {
|
|
|
|
|
// 2. 运行模型
|
|
|
|
|
// 2.1 -m 参数对应的值 /rasa/models/aaa1111.tar.gz 指run的模型的路径,/rasa/models应该来自于配置文件,和训练时的--out是同一配置项;
|
|
|
|
|
// aaa1111.tar.gz这个,前面的文件名应该是--fixed-model-name指定的,.tar.gz是文件后缀,代码拼接
|
|
|
|
|
String localPath = replaceDuplicateSeparator(String.join(File.separator,modelsPath,argument.getModelId(),argument.getFixedModelName()+".tar.gz"));
|
|
|
|
|
|
|
|
|
|
List<String> cmds = ListUtil.toList(shellEnv, runShell,localPath,endpoints,String.valueOf(port));
|
|
|
|
|
String fixedModePath;
|
|
|
|
|
String modeParentPath = replaceDuplicateSeparator(String.join(File.separator, modelsPath, argument.getModelId()));
|
|
|
|
|
if (StrUtil.isEmpty(argument.getFixedModelName())){
|
|
|
|
|
fixedModePath = listLastFilePath(modeParentPath, f -> f.getName().matches("-?\\d+(\\.\\d+)?.tar.gz"));
|
|
|
|
|
}else {
|
|
|
|
|
String fixedModelName = argument.getFixedModelName()+".tar.gz";
|
|
|
|
|
fixedModePath = String.join(modeParentPath,fixedModelName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<String> cmds = ListUtil.toList(shellEnv, runShell,fixedModePath,endpoints,String.valueOf(port));
|
|
|
|
|
|
|
|
|
|
log.info("runExec cmd : {}",StrUtil.join(" ",cmds));
|
|
|
|
|
|
|
|
|
|
List<String> outMessageList = execCmd(cmds, s -> StrUtil.isNotBlank(s) && s.contains(RasaConstant.RUN_SUCCESS_MESSAGE), 300);
|
|
|
|
|
|
|
|
|
|
String outMessageString = String.join("\r\n", outMessageList);
|
|
|
|
|
// 3. 更新模型信息
|
|
|
|
|
|
|
|
|
|
// 3. 尝试停止原先的程序
|
|
|
|
|
boolean runIsSuccess = runIsSuccess(outMessageList);
|
|
|
|
|
RasaModelInfo dbRasaModelInfo = rasaModeService.queryByModelId(argument.getModelId());
|
|
|
|
|
if (null != dbRasaModelInfo.getPort() && runIsSuccess){
|
|
|
|
|
log.info("runExec server is up and dbRasaModelInfo is: {}, start kill old server ....",dbRasaModelInfo.getPort());
|
|
|
|
|
// 3.1 如果原本的程序已经运行且本次成功启动,杀掉原本的进程
|
|
|
|
|
execCmd(ListUtil.toList(runShell, killShell, String.valueOf(dbRasaModelInfo.getPort())), s -> false, 20);
|
|
|
|
|
}
|
|
|
|
|
if (null != dbRasaModelInfo.getPort() && !runIsSuccess){
|
|
|
|
|
// 3.2 如果原本的程序已经运行,但本次程序未正常启动,不对端口号进行变动
|
|
|
|
|
log.info("runExec server start fail and dbRasaModelInfo is: {}, user old server ....",dbRasaModelInfo.getPort());
|
|
|
|
|
port = dbRasaModelInfo.getPort();
|
|
|
|
|
}
|
|
|
|
|
// 4. 更新模型信息
|
|
|
|
|
RasaModelInfo rasaModelInfo = new RasaModelInfo();
|
|
|
|
|
rasaModelInfo.setModelId(argument.getModelId());
|
|
|
|
|
rasaModelInfo.setPort(port);
|
|
|
|
|
rasaModelInfo.setServerStatus(runIsSuccess(outMessageList)?1:0);
|
|
|
|
|
rasaModelInfo.setRunCmd(ListUtil.toList(shellEnv, runShell,localPath,endpoints));
|
|
|
|
|
rasaModelInfo.setServerStatus( runIsSuccess ? 1:0);
|
|
|
|
|
rasaModelInfo.setRunCmd(ListUtil.toList(shellEnv, runShell,fixedModePath,endpoints));
|
|
|
|
|
rasaModelInfo.setRunLog(outMessageString);
|
|
|
|
|
rasaModeService.saveOrUpdateByModelId(rasaModelInfo);
|
|
|
|
|
|
|
|
|
@ -188,4 +214,24 @@ public class RasaCmdServiceImpl implements RasaCmdService {
|
|
|
|
|
return path.replace(File.separator + File.separator, File.separator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String listLastFilePath(String path, FileFilter filter){
|
|
|
|
|
File file = listLastFile(path, filter);
|
|
|
|
|
if (null == file){
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return file.getPath();
|
|
|
|
|
}
|
|
|
|
|
private File listLastFile(String path,FileFilter filter){
|
|
|
|
|
File file = new File(path);
|
|
|
|
|
File[] files = file.listFiles(filter);
|
|
|
|
|
if (null == files){
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Arrays.stream(files).max(Comparator.comparing(File::getName)).orElse(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|