rasa:移除 fixedModelName 必填项目

dev_v1.0.1
xueqingkun 2 years ago
parent c38c926a5b
commit 2744c4957c

@ -26,9 +26,6 @@ public class RasaCmdController {
@PostMapping("/trainExec")
public String trainExec(@RequestBody RasaCmdArgumentVo argument) throws IOException, ExecutionException, InterruptedException, TimeoutException {
if (StrUtil.isEmpty(argument.getFixedModelName())){
throw new BusinessException("fixedModelName参数不能为空");
}
if (StrUtil.isEmpty(argument.getModelId())){
throw new BusinessException("modelId参数不能为空! ");
@ -42,9 +39,6 @@ public class RasaCmdController {
@PostMapping("/runExec")
public String runExec(@RequestBody RasaCmdArgumentVo argument) throws ExecutionException, InterruptedException, TimeoutException {
if (StrUtil.isEmpty(argument.getFixedModelName())){
throw new BusinessException("fixedModelName参数不能为空");
}
if (StrUtil.isEmpty(argument.getModelId())){
throw new BusinessException("modelId参数不能为空! ");
}

@ -1,7 +1,10 @@
package com.supervision.rasa.pojo.vo;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import java.util.Date;
@Data
public class RasaCmdArgumentVo {
@ -9,4 +12,10 @@ public class RasaCmdArgumentVo {
private String modelId;
public void setFixedModelNameIfAbsent(){
if (StrUtil.isEmpty(fixedModelName)){
fixedModelName = String.valueOf(new Date().getTime()/1000);
}
}
}

@ -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);
}
}

@ -18,6 +18,7 @@ rasa:
config: /rasa/config-local.yml # 启动rasa需要的配置文件在配置文件中配置
train-shell: /data/vp/rasa_shell/train.sh
run-shell: /data/vp/rasa_shell/run.sh
kill-shell: /data/vp/rasa_shell/kill.sh
url: 127.0.0.1:{}/webhooks/rest/webhook
wakeup:
cron: 0 */10 * * * ? #每十分钟执行一次

Loading…
Cancel
Save